SMTP via GMAIL

Discussion in 'Python' started by mmm, Aug 2, 2008.

  1. mmm

    mmm Guest

    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?
    mmm, Aug 2, 2008
    #1
    1. Advertising

  2. mmm

    binaryjesus Guest

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

    this hack is also known as header injection attack



    On Aug 3, 2:36 am, mmm <> wrote:
    > 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?
    binaryjesus, Aug 3, 2008
    #2
    1. Advertising

  3. mmm

    Tim Roberts Guest

    mmm <> wrote:
    >
    >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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Aug 5, 2008
    #3
  4. mmm

    mmm Guest

    On Aug 5, 12:18 am, Tim Roberts <> wrote:

    > >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.,
    ) 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 ( in this case).

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

    ## Message details
    FROM= SENDER
    TO= ''
    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()
    mmm, Aug 6, 2008
    #4
  5. mmm

    sui Guest

    On Aug 7, 12:40 am, mmm <> wrote:
    > On Aug 5, 12:18 am, Tim Roberts <> wrote:
    >
    > > >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.,
    > ) 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 ( in this case).
    >
    > Most important, using another gmail account (not ) 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= ''
    > pw= 'fill in here'
    > SENDER= SMTPuser
    >
    > ## Message details
    > FROM= SENDER
    > TO= ''
    > 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
    sui, Sep 17, 2008
    #5
  6. In message
    <>, 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?
    Lawrence D'Oliveiro, Sep 18, 2008
    #6
  7. mmm

    Steve Holden Guest

    Grant Edwards wrote:
    > On 2008-09-18, Lawrence D'Oliveiro <_zealand> wrote:
    >> In message
    >> <>, 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/
    Steve Holden, Sep 19, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    155,649
    abhishek-technicalwork
    Jul 11, 2012
  2. Gabriel Genellina

    Re: smtplib send email by using gmail smtp server

    Gabriel Genellina, May 3, 2009, in forum: Python
    Replies:
    0
    Views:
    426
    Gabriel Genellina
    May 3, 2009
  3. online
    Replies:
    0
    Views:
    864
    online
    Sep 6, 2009
  4. Jhovarie

    java GMAIL smtp with attach

    Jhovarie, Jan 13, 2011, in forum: Java
    Replies:
    0
    Views:
    497
    Jhovarie
    Jan 13, 2011
  5. Astley Le Jasper

    Inconsistent SMTP/Gmail connection drop

    Astley Le Jasper, Aug 3, 2011, in forum: Python
    Replies:
    3
    Views:
    264
    Astley Le Jasper
    Aug 4, 2011
Loading...

Share This Page