By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,286 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,286 IT Pros & Developers. It's quick & easy.

SMTP via GMAIL

P: n/a
mmm
After reading about and using the smtplib module, I thought code such
as below would ignore the 'Cc: ' body line below when sending messages
and instead simply use the RECEIVERS list

session = smtplib.SMTP(SMTPserver,port)
session.set_debuglevel(1)
session.ehlo(SMTPuser) # say hello
session.starttls() # TLS needed
session.ehlo(SMTPuser) # say hello again
session.login(SMTPuser, pw)

FROM=SENDER
RECEIVERS= (TO,CC)

BODY= MakeBody(FROM,TO,CC,SUBJECT,MESSAGE)
SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)

Here the MakeBody() creates text like below

From: FROM
To: TO
Cc: CC
Subject: SUBJECT

MESSAGE

But when using smtp.gmail.com as the server I learned that any
@gmail.com address in the Cc: text block would
receive mail even if I changed the code to have the RECEIVERS list to
ignore the CC addresses or not include the gmail address in the CC
list as below

RECEIVERS= (TO,)
BODY= MakeBody(FROM,TO,CC,subject,message)
SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)

Other @zzz.com CC addresses need to be in the RECEIVERS list however.

Also the gmail server changes the 'From: ' text to be the same as
SENDER even if this is modified (a case not using FROM=SENDER. I
found other servers send mail that displays the BODY specified From:
address.

Is this gmail specific or a quirk of the smtplib functions?
I understand how Google might not want to send mail with FROM not =
SENDER, but the CC behavior baffles me.

And does anyone have a general routine that lets one also have Bcc:
addresses usign SMTP?
Aug 2 '08 #1
Share this Question
Share on Google+
9 Replies

P: n/a
i have a lot of experience in gmail. i use it to store 100GB's of
server backup on it.

the form: field will be equal to the gmail acc u login with.

you are not clear with ur cc: so i cant offer any help on it. but u
can include multiple addresses in the To: and use Bcc:

since python doesnt include bcc in sendmail but there is a hacky
method to do that

to ='*****@email.com \n\rBcc: em****@gmail.com'
snedmail(from,to,mail)

this hack is also known as header injection attack

On Aug 3, 2:36 am, mmm <mdbol...@gmail.comwrote:
After reading about and using the smtplib module, I thought code such
as below would ignore the 'Cc: ' body line below when sending messages
and instead simply use the RECEIVERS list

session = smtplib.SMTP(SMTPserver,port)
session.set_debuglevel(1)
session.ehlo(SMTPuser) # say hello
session.starttls() # TLS needed
session.ehlo(SMTPuser) # say hello again
session.login(SMTPuser, pw)

FROM=SENDER
RECEIVERS= (TO,CC)

BODY= MakeBody(FROM,TO,CC,SUBJECT,MESSAGE)
SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)

Here the MakeBody() creates text like below

From: FROM
To: TO
Cc: CC
Subject: SUBJECT

MESSAGE

But when using smtp.gmail.com as the server I learned that any
@gmail.com address in the Cc: text block would
receive mail even if I changed the code to have the RECEIVERS list to
ignore the CC addresses or not include the gmail address in the CC
list as below

RECEIVERS= (TO,)
BODY= MakeBody(FROM,TO,CC,subject,message)
SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)

Other @zzz.com CC addresses need to be in the RECEIVERS list however.

Also the gmail server changes the 'From: ' text to be the same as
SENDER even if this is modified (a case not using FROM=SENDER. I
found other servers send mail that displays the BODY specified From:
address.

Is this gmail specific or a quirk of the smtplib functions?
I understand how Google might not want to send mail with FROM not =
SENDER, but the CC behavior baffles me.

And does anyone have a general routine that lets one also have Bcc:
addresses usign SMTP?
Aug 3 '08 #2

P: n/a
mmm <md******@gmail.comwrote:
>
After reading about and using the smtplib module, I thought code such
as below would ignore the 'Cc: ' body line below when sending messages
and instead simply use the RECEIVERS list
Correct. It is required by the SMTP spec to behave that way.
>But when using smtp.gmail.com as the server I learned that any
@gmail.com address in the Cc: text block would
receive mail even if I changed the code to have the RECEIVERS list to
ignore the CC addresses or not include the gmail address in the CC
list as below
Interesting. If true, that is incorrect behavior.
>And does anyone have a general routine that lets one also have Bcc:
addresses usign SMTP?
To make a Bcc, all you do is include the address in the RECEIVERS list, but
don't mention it in the body at all.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 5 '08 #3

P: n/a
mmm
On Aug 5, 12:18*am, Tim Roberts <t...@probo.comwrote:
But when using smtp.gmail.com as the server I learned that any
@gmail.com address in the *Cc: text block would
receive mail even if I changed the code to have the RECEIVERS list to
ignore the CC addresses or not include the gmail address in the CC
list as below

Interesting. *If true, that is incorrect behavior.
I ran some more tests and now I am pretty sure

session.sendmail(SENDER, RECEIVERS, BODY)

is only sending to the RECEIVERS list, ignoring the Cc: field in the
body (as it should)

What fooled me is that I was using my own gmail account (i.e.,
me@gmail.com) as a Cc: field and not putting it in the RECEIVERS
list. It seems Gmail creates two links (or copies?) to the message:
(1) as it is stored in the SENT box (as it should since the message
was sent by my gmail account) and (2) another in my INBOX because the
mail reading software reads the Cc: field.

Other smtp servers such as comcast do not create the stored SENT mail
and hence behave different in terms of how they treat Cc: fields of
the same account (me@comcast.net in this case).

Most important, using another gmail account (not me@gmail.com) as a
Cc: field does not create another sent message (outside of what is in
the RECEIVERS field).

Sorry for confusion, and I do appreciate the tips as I now see how
almost anything To:, Cc:, Bcc: combination can be handled by a proper
RECEIVERS list.
Below is python code that can be used by anyone that wants to test
what I did (just fill in the SMTPuser and password variables) and
then check you gmail inbox
import sys, os, glob, datetime, time
import smtplib
## Parameters for SMTP session
port=587
SMTPserver= 'smtp.gmail.com'
SMTPuser= 'y**@gmail.com'
pw= 'fill in here'
SENDER= SMTPuser

## Message details
FROM= SENDER
TO= 'n*******@a.com'
CC=FROM
##RECEIVERS= (TO, CC) ##proper way to send to both TO and CC
RECEIVERS= (TO,) ## ignore the CC address

subject= 'Test 1a'
message='*** Email test *** '

print 'Starting SMTP mail session on %s as %s ' %
(SMTPserver,SMTPuser)
session = smtplib.SMTP(SMTPserver,port)
session.set_debuglevel(0) # set debug level to 1 to see details
session.ehlo(SMTPuser) # say hello
session.starttls() # TLS needed
session.ehlo(SMTPuser) # say hello again, not sure why
session.login(SMTPuser, pw)

##Create HEADER + MESSAGE
HEADER= 'From: %s\r\n' % FROM
HEADER= HEADER + 'To: %s\r\n' % TO
HEADER= HEADER + 'Cc: %s\r\n' % CC
HEADER= HEADER + 'Subject: %s\r\n' % subject
BODY= HEADER + '\r\n' + message
print BODY

SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY) ## send email

session.close()



Aug 6 '08 #4

P: n/a
sui
On Aug 7, 12:40 am, mmm <mdbol...@gmail.comwrote:
On Aug 5, 12:18 am, Tim Roberts <t...@probo.comwrote:
>But when using smtp.gmail.com as the server I learned that any
>@gmail.com address in the Cc: text block would
>receive mail even if I changed the code to have the RECEIVERS list to
>ignore the CC addresses or not include the gmail address in the CC
>list as below
Interesting. If true, that is incorrect behavior.

I ran some more tests and now I am pretty sure

session.sendmail(SENDER, RECEIVERS, BODY)

is only sending to the RECEIVERS list, ignoring the Cc: field in the
body (as it should)

What fooled me is that I was using my own gmail account (i.e.,
m...@gmail.com) as a Cc: field and not putting it in the RECEIVERS
list. It seems Gmail creates two links (or copies?) to the message:
(1) as it is stored in the SENT box (as it should since the message
was sent by my gmail account) and (2) another in my INBOX because the
mail reading software reads the Cc: field.

Other smtp servers such as comcast do not create the stored SENT mail
and hence behave different in terms of how they treat Cc: fields of
the same account (m...@comcast.net in this case).

Most important, using another gmail account (not m...@gmail.com) as a
Cc: field does not create another sent message (outside of what is in
the RECEIVERS field).

Sorry for confusion, and I do appreciate the tips as I now see how
almost anything To:, Cc:, Bcc: combination can be handled by a proper
RECEIVERS list.

Below is python code that can be used by anyone that wants to test
what I did (just fill in the SMTPuser and password variables) and
then check you gmail inbox

import sys, os, glob, datetime, time
import smtplib
## Parameters for SMTP session
port=587
SMTPserver= 'smtp.gmail.com'
SMTPuser= '...@gmail.com'
pw= 'fill in here'
SENDER= SMTPuser

## Message details
FROM= SENDER
TO= 'notgm...@a.com'
CC=FROM
##RECEIVERS= (TO, CC) ##proper way to send to both TO and CC
RECEIVERS= (TO,) ## ignore the CC address

subject= 'Test 1a'
message='*** Email test *** '

print 'Starting SMTP mail session on %s as %s ' %
(SMTPserver,SMTPuser)
session = smtplib.SMTP(SMTPserver,port)
session.set_debuglevel(0) # set debug level to 1 to see details
session.ehlo(SMTPuser) # say hello
session.starttls() # TLS needed
session.ehlo(SMTPuser) # say hello again, not sure why
session.login(SMTPuser, pw)

##Create HEADER + MESSAGE
HEADER= 'From: %s\r\n' % FROM
HEADER= HEADER + 'To: %s\r\n' % TO
HEADER= HEADER + 'Cc: %s\r\n' % CC
HEADER= HEADER + 'Subject: %s\r\n' % subject
BODY= HEADER + '\r\n' + message
print BODY

SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY) ## send email

session.close()
i tried to run this code...but it didnt work
it shows that message like starting smtp session
then it doesnt show anything after very long time it shows
Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....
can u tell me wats the prob ...plz tell me solun
Sep 17 '08 #5

P: n/a
In message
<97**********************************@v16g2000prc. googlegroups.com>, sui
wrote:
Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....
Could it be your ISP is blocking outgoing connections to port 25?
Sep 18 '08 #6

P: n/a
On 2008-09-18, Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
In message
<97**********************************@v16g2000prc .googlegroups.com>, sui
wrote:
>Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....

Could it be your ISP is blocking outgoing connections to port
25?
gmail doesn't accept mail via SMTP on port 25.

--
Grant

Sep 18 '08 #7

P: n/a
Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....
Could it be your ISP is blocking outgoing connections to port
25?

gmail doesn't accept mail via SMTP on port 25.
So what is the value of "port" when you are running this program?

Chris
Sep 18 '08 #8

P: n/a
Grant Edwards wrote:
On 2008-09-18, Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
>In message
<97**********************************@v16g2000prc .googlegroups.com>, sui
wrote:
>>Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....
Could it be your ISP is blocking outgoing connections to port
25?

gmail doesn't accept mail via SMTP on port 25.
I was going to say that's boloney until I checked my settings - it's a
year or more since I set gmail up with Thunderbird.

smtp.gmail.com accepts TLS connections on port 587.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 19 '08 #9

P: n/a
On 2008-09-19, Steve Holden <st***@holdenweb.comwrote:
Grant Edwards wrote:
>On 2008-09-18, Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
>>In message
<97**********************************@v16g2000pr c.googlegroups.com>, sui
wrote:

Traceback (most recent call last):
File "mail5.py", line 21, in <module>
session = smtplib.SMTP(SMTPserver,port)
File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
self.sock.connect(sa)
File "<string>", line 1, in connect
then conncetion time out.....
Could it be your ISP is blocking outgoing connections to port
25?

gmail doesn't accept mail via SMTP on port 25.
I was going to say that's boloney until I checked my settings - it's a
year or more since I set gmail up with Thunderbird.

smtp.gmail.com accepts TLS connections on port 587.
I should have been a bit more specific and said that the
relay/smarthosts at smtp.gmail.com don't accept SMTP mail via
port 25.

The normal incoming SMTP servers pointed to by gmail.com's MX
records do accept non-relay e-mail on port 25. They are,
however, picky about IP addresses from which they'll accept
connections (trying to connect via a commercial VPN server
fails, but connecting via other machines works).

--
Grant Edwards grante Yow! WHO sees a BEACH BUNNY
at sobbing on a SHAG RUG?!
visi.com
Sep 19 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.