SMTP via GMAIL

M

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?
 
B

binaryjesus

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 ='(e-mail address removed) \n\rBcc: (e-mail address removed)'
snedmail(from,to,mail)

this hack is also known as header injection attack
 
T

Tim Roberts

mmm said:
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.
 
M

mmm

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.,
(e-mail address removed)) 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 ([email protected] in this case).

Most important, using another gmail account (not (e-mail address removed)) 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= '(e-mail address removed)'
pw= 'fill in here'
SENDER= SMTPuser

## Message details
FROM= SENDER
TO= '(e-mail address removed)'
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()
 
S

sui

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.,
(e-mail address removed)) 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 ([email protected] in this case).

Most important, using another gmail account (not (e-mail address removed)) 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= '(e-mail address removed)'
pw= 'fill in here'
SENDER= SMTPuser

## Message details
FROM= SENDER
TO= '(e-mail address removed)'
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
 
L

Lawrence D'Oliveiro

In message
sui said:
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?
 
S

Steve Holden

Grant said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top