SMTP via GMAIL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mmm

    SMTP via GMAIL

    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(SM TPserver,port)
    session.set_deb uglevel(1)
    session.ehlo(SM TPuser) # say hello
    session.starttl s() # TLS needed
    session.ehlo(SM TPuser) # say hello again
    session.login(S MTPuser, pw)

    FROM=SENDER
    RECEIVERS= (TO,CC)

    BODY= MakeBody(FROM,T O,CC,SUBJECT,ME SSAGE)
    SMTPresult = session.sendmai l(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,T O,CC,subject,me ssage)
    SMTPresult = session.sendmai l(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

    #2
    Re: SMTP via GMAIL

    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@email.c om \n\rBcc: email2@gmail.co m'
    snedmail(from,t o,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(SM TPserver,port)
    session.set_deb uglevel(1)
    session.ehlo(SM TPuser) # say hello
    session.starttl s() # TLS needed
    session.ehlo(SM TPuser) # say hello again
    session.login(S MTPuser, pw)
    >
    FROM=SENDER
    RECEIVERS= (TO,CC)
    >
    BODY= MakeBody(FROM,T O,CC,SUBJECT,ME SSAGE)
    SMTPresult = session.sendmai l(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,T O,CC,subject,me ssage)
    SMTPresult = session.sendmai l(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?

    Comment

    • Tim Roberts

      #3
      Re: SMTP via GMAIL

      mmm <mdboldin@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, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • mmm

        #4
        Re: SMTP via GMAIL

        On Aug 5, 12:18 am, Tim Roberts <t...@probo.com 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.sendmai l(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= 'you@gmail.com'
        pw= 'fill in here'
        SENDER= SMTPuser

        ## Message details
        FROM= SENDER
        TO= 'notgmail@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,SMT Puser)
        session = smtplib.SMTP(SM TPserver,port)
        session.set_deb uglevel(0) # set debug level to 1 to see details
        session.ehlo(SM TPuser) # say hello
        session.starttl s() # TLS needed
        session.ehlo(SM TPuser) # say hello again, not sure why
        session.login(S MTPuser, 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.sendmai l(SENDER, RECEIVERS, BODY) ## send email

        session.close()







        Comment

        • sui

          #5
          Re: SMTP via GMAIL

          On Aug 7, 12:40 am, mmm <mdbol...@gmail .comwrote:
          On Aug 5, 12:18 am, Tim Roberts <t...@probo.com 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.sendmai l(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.n et 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,SMT Puser)
          session = smtplib.SMTP(SM TPserver,port)
          session.set_deb uglevel(0) # set debug level to 1 to see details
          session.ehlo(SM TPuser) # say hello
          session.starttl s() # TLS needed
          session.ehlo(SM TPuser) # say hello again, not sure why
          session.login(S MTPuser, 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.sendmai l(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(SM TPserver,port)
          File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
          (code, msg) = self.connect(ho st, port)
          File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
          self.sock.conne ct(sa)
          File "<string>", line 1, in connect
          then conncetion time out.....
          can u tell me wats the prob ...plz tell me solun

          Comment

          • Lawrence D'Oliveiro

            #6
            Re: SMTP via GMAIL

            In message
            <973f75d4-90f5-4930-9107-15fc8651196d@v1 6g2000prc.googl egroups.com>, sui
            wrote:
            Traceback (most recent call last):
            File "mail5.py", line 21, in <module>
            session = smtplib.SMTP(SM TPserver,port)
            File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
            (code, msg) = self.connect(ho st, port)
            File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
            self.sock.conne ct(sa)
            File "<string>", line 1, in connect
            then conncetion time out.....
            Could it be your ISP is blocking outgoing connections to port 25?

            Comment

            • Grant Edwards

              #7
              Re: SMTP via GMAIL

              On 2008-09-18, Lawrence D'Oliveiro <ldo@geek-central.gen.new _zealandwrote:
              In message
              ><973f75d4-90f5-4930-9107-15fc8651196d@v1 6g2000prc.googl egroups.com>, sui
              wrote:
              >
              >Traceback (most recent call last):
              > File "mail5.py", line 21, in <module>
              > session = smtplib.SMTP(SM TPserver,port)
              > File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
              > (code, msg) = self.connect(ho st, port)
              > File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
              > self.sock.conne ct(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

              Comment

              • Chris Babcock

                #8
                Re: SMTP via GMAIL

                Traceback (most recent call last):
                File "mail5.py", line 21, in <module>
                session = smtplib.SMTP(SM TPserver,port)
                File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
                (code, msg) = self.connect(ho st, port)
                File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
                self.sock.conne ct(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


                Comment

                • Steve Holden

                  #9
                  Re: SMTP via GMAIL

                  Grant Edwards wrote:
                  On 2008-09-18, Lawrence D'Oliveiro <ldo@geek-central.gen.new _zealandwrote:
                  >In message
                  ><973f75d4-90f5-4930-9107-15fc8651196d@v1 6g2000prc.googl egroups.com>, sui
                  >wrote:
                  >>
                  >>Traceback (most recent call last):
                  >> File "mail5.py", line 21, in <module>
                  >> session = smtplib.SMTP(SM TPserver,port)
                  >> File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
                  >> (code, msg) = self.connect(ho st, port)
                  >> File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
                  >> self.sock.conne ct(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/

                  Comment

                  • Grant Edwards

                    #10
                    Re: SMTP via GMAIL

                    On 2008-09-19, Steve Holden <steve@holdenwe b.comwrote:
                    Grant Edwards wrote:
                    >On 2008-09-18, Lawrence D'Oliveiro <ldo@geek-central.gen.new _zealandwrote:
                    >>In message
                    >><973f75d4-90f5-4930-9107-15fc8651196d@v1 6g2000prc.googl egroups.com>, sui
                    >>wrote:
                    >>>
                    >>>Traceback (most recent call last):
                    >>> File "mail5.py", line 21, in <module>
                    >>> session = smtplib.SMTP(SM TPserver,port)
                    >>> File "/usr/local/lib/python2.5/smtplib.py", line 244, in __init__
                    >>> (code, msg) = self.connect(ho st, port)
                    >>> File "/usr/local/lib/python2.5/smtplib.py", line 301, in connect
                    >>> self.sock.conne ct(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

                    Comment

                    Working...