help using smtplib to work ..

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

    #1

    help using smtplib to work ..

    I'm using the following code to send e-mail through python:

    import smtplib

    fromaddr = 'myMail@gmail.c om'
    toaddrs = 'myOtherMail@gm ail.com'
    subject = 'someting for subject !'

    msg = 'This is body of the mail.'

    msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject:' + subject
    + '\n\n' + msg
    print "Message length is " + repr(len(msg))

    server = smtplib.SMTP('s mtp.gmail.com')
    server.set_debu glevel(1)
    server.sendmail (fromaddr, toaddrs, msg)
    server.quit()

    I have replaced the sender and receiver's mail address with the
    original ones but executing this code I get the error (ip is hided for
    obvious reason):

    send: 'ehlo computer.domain _not_set.invali d\r\n'
    reply: '250-mx.google.com at your service, [xxx.xx.xxx.xxx]\r\n'
    reply: '250-SIZE 20971520\r\n'
    reply: '250-8BITMIME\r\n'
    reply: '250-STARTTLS\r\n'
    reply: '250 ENHANCEDSTATUSC ODES\r\n'
    reply: retcode (250); Msg: mx.google.com at your service,
    [xxx.xx.xxx.xxx]
    SIZE 20971520
    8BITMIME
    STARTTLS
    ENHANCEDSTATUSC ODES
    send: 'mail FROM:<myMail@gm ail.comsize=106 \r\n'
    reply: '530 5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge\ r\n'
    reply: retcode (530); Msg: 5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge
    send: 'rset\r\n'
    reply: '250 2.1.0 Flushed s1sm7666914uge\ r\n'
    reply: retcode (250); Msg: 2.1.0 Flushed s1sm7666914uge
    Traceback (most recent call last):
    File "C:\Documen ts and Settings\Nicola sG\Desktop\smtp example.py",
    line 17, in <module>
    server.sendmail (fromaddr, toaddrs, msg)
    File "C:\Python25\li b\smtplib.py", line 684, in sendmail
    raise SMTPSenderRefus ed(code, resp, from_addr)
    SMTPSenderRefus ed: (530, '5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge' , 'myMail@gmail.c om')

    What am I doing wrong ?

  • Larry Bates

    #2
    Re: help using smtplib to work ..

    NicolasG wrote:
    I'm using the following code to send e-mail through python:
    >
    import smtplib
    >
    fromaddr = 'myMail@gmail.c om'
    toaddrs = 'myOtherMail@gm ail.com'
    subject = 'someting for subject !'
    >
    msg = 'This is body of the mail.'
    >
    msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject:' + subject
    + '\n\n' + msg
    print "Message length is " + repr(len(msg))
    >
    server = smtplib.SMTP('s mtp.gmail.com')
    server.set_debu glevel(1)
    server.sendmail (fromaddr, toaddrs, msg)
    server.quit()
    >
    I have replaced the sender and receiver's mail address with the
    original ones but executing this code I get the error (ip is hided for
    obvious reason):
    >
    send: 'ehlo computer.domain _not_set.invali d\r\n'
    reply: '250-mx.google.com at your service, [xxx.xx.xxx.xxx]\r\n'
    reply: '250-SIZE 20971520\r\n'
    reply: '250-8BITMIME\r\n'
    reply: '250-STARTTLS\r\n'
    reply: '250 ENHANCEDSTATUSC ODES\r\n'
    reply: retcode (250); Msg: mx.google.com at your service,
    [xxx.xx.xxx.xxx]
    SIZE 20971520
    8BITMIME
    STARTTLS
    ENHANCEDSTATUSC ODES
    send: 'mail FROM:<myMail@gm ail.comsize=106 \r\n'
    reply: '530 5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge\ r\n'
    reply: retcode (530); Msg: 5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge
    send: 'rset\r\n'
    reply: '250 2.1.0 Flushed s1sm7666914uge\ r\n'
    reply: retcode (250); Msg: 2.1.0 Flushed s1sm7666914uge
    Traceback (most recent call last):
    File "C:\Documen ts and Settings\Nicola sG\Desktop\smtp example.py",
    line 17, in <module>
    server.sendmail (fromaddr, toaddrs, msg)
    File "C:\Python25\li b\smtplib.py", line 684, in sendmail
    raise SMTPSenderRefus ed(code, resp, from_addr)
    SMTPSenderRefus ed: (530, '5.7.0 Must issue a STARTTLS command first
    s1sm7666914uge' , 'myMail@gmail.c om')
    >
    What am I doing wrong ?
    >
    <snip from Gmail SMTP setup instructions>

    Under outgoing mail smtp server, use smtp.gmail.com. Since it requires SMTP
    authorization, use your Gmail account as username (e.g. yourname@gmail. com) and
    your Gmail password as password. You can turn TSL on or off.

    <end snip>

    The email server requires authentication before it can send.
    Normally this can be accomplished either by calling

    server.login(us erid, password)

    The only alternative is to use an open relay email server that allows
    you to send without authenticating. Hope info at least points you in
    the right direction.

    -Larry

    Comment

    • Gabriel Genellina

      #3
      Re: help using smtplib to work ..

      At Wednesday 8/11/2006 19:23, NicolasG wrote:
      >I'm using the following code to send e-mail through python:
      >
      >import smtplib
      >
      >fromaddr = 'myMail@gmail.c om'
      >toaddrs = 'myOtherMail@gm ail.com'
      >subject = 'someting for subject !'
      >
      >msg = 'This is body of the mail.'
      >
      >msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject:' + subject
      >+ '\n\n' + msg
      >print "Message length is " + repr(len(msg))
      >
      >server = smtplib.SMTP('s mtp.gmail.com')
      >server.set_deb uglevel(1)
      >server.sendmai l(fromaddr, toaddrs, msg)
      >server.quit( )
      >
      >send: 'mail FROM:<myMail@gm ail.comsize=106 \r\n'
      >reply: '530 5.7.0 Must issue a STARTTLS command first
      >s1sm7666914uge \r\n'
      >
      >What am I doing wrong ?
      gmail.com requires authentication before sending mail. Try this
      sequence: ehlo, starttls, ehlo (again!), login, sendmail, quit
      (BTW, you need a space after Subject:, and all \n should be \r\n)


      --
      Gabriel Genellina
      Softlab SRL

      _______________ _______________ _______________ _____
      Correo Yahoo!
      Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
      ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

      Comment

      • NicolasG

        #4
        Re: help using smtplib to work ..

        <snip from Gmail SMTP setup instructions>
        >
        Under outgoing mail smtp server, use smtp.gmail.com. Since it requires SMTP
        authorization, use your Gmail account as username (e.g. yourname@gmail. com) and
        your Gmail password as password. You can turn TSL on or off.
        >
        <end snip>
        >
        The email server requires authentication before it can send.
        Normally this can be accomplished either by calling
        >
        Actually I used alt1.gmail-smtp-in.l.google.com for a server I don't
        need authentication. The problem is that for some mail addresses I can
        send mail's while for other's I can't . I don't know how to explained
        that.....
        server.login(us erid, password)
        >
        The only alternative is to use an open relay email server that allows
        you to send without authenticating. Hope info at least points you in
        the right direction.
        >
        the code bellow worked fine to me :

        import smtplib

        fromaddr = 'nicolasg@gmail .com'
        toaddrs = 'nicolasg@gmail .com'
        subject = 'someting for subject !'

        msg = 'This is body of the mail.'

        msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject: ' +
        subject + '\n\n' + msg
        print "Message length is " + repr(len(msg))

        server = smtplib.SMTP('a lt1.gmail-smtp-in.l.google.com ')
        #'smtp.gmail.co m')
        server.set_debu glevel(1)
        server.sendmail (fromaddr, toaddrs, msg)
        server.quit()

        cheers.

        Comment

        • Tim Williams

          #5
          Re: help using smtplib to work ..

          On 8 Nov 2006 15:31:27 -0800, NicolasG <nicolasg@gmail .comwrote:
          >
          <snip from Gmail SMTP setup instructions>

          Under outgoing mail smtp server, use smtp.gmail.com. Since it requires SMTP
          authorization, use your Gmail account as username (e.g. yourname@gmail. com) and
          your Gmail password as password. You can turn TSL on or off.

          <end snip>

          The email server requires authentication before it can send.
          Normally this can be accomplished either by calling
          Actually I used alt1.gmail-smtp-in.l.google.com for a server I don't
          need authentication. The problem is that for some mail addresses I can
          send mail's while for other's I can't . I don't know how to explained
          that.....
          Without authentication, you will only be able to send to the severs
          local addresses eg ...@gmail.com ...@googlemail. com
          The only alternative is to use an open relay email server that allows
          you to send without authenticating. Hope info at least points you in
          the right direction.
          or you can probably use your ISP's outgoing mail server, which usually
          won't require authentication.

          HTH :)

          Comment

          • Tim Williams

            #6
            Re: help using smtplib to work ..

            On 09/11/06, Nicolas G <nicolasg@gmail .comwrote:
            >
            >

            Usually when sending/relaying without authentication, the From is
            irrelevant, only the To is taken into account. Maybe, GMAIL do
            something different because they have to put the mail in the sender's
            mailbox as well as the recipient's. Some ISPs will only allow a
            local FROM address though.

            As we're only talking about the envelope TO & FROM, you could try no
            FROM address (None or <I forget which Smtplib needs) .
            >
            If I left From empty I don't get any error but I still don't receive any
            message.
            >
            The header FROM can still have the real FROM address in it, as its not
            (normally)
            used during SMTP relaying.
            >
            How you can explain that only for some address in the From field I can
            receive mails' and from others I can't ?
            It looks really weird to me...
            It must be down to how gmail's server(s) work.
            Other question : Suppose I use the login procedure, the password will be
            vulnerable to attacks ? If I'm right I think when no ssl is used for
            authentication some one can easy see the password just by sniffing the
            packets.
            I think the risk is low to be honest. However, smtp.gmail.com uses
            TLS (SSL) on ports 465 or 587. You can call STARTTLS from within
            smtplib. but TrevorP's TLS-lite extensions to smtplib handle it
            better



            :)

            Comment

            • Jordan

              #7
              Re: help using smtplib to work ..

              Your post was definitely the most helpful for me. For some reason,
              smtp for gmail seems to require that you call server.ehlo() a second
              time, after having called server.starttls (), otherwise, the server
              won't accept authorization. Thanks.

              -Jordan


              Gabriel Genellina wrote:
              At Wednesday 8/11/2006 19:23, NicolasG wrote:
              >
              I'm using the following code to send e-mail through python:

              import smtplib

              fromaddr = 'myMail@gmail.c om'
              toaddrs = 'myOtherMail@gm ail.com'
              subject = 'someting for subject !'

              msg = 'This is body of the mail.'

              msg = 'From: ' + fromaddr + '\nTo: ' + toaddrs + '\nSubject:' + subject
              + '\n\n' + msg
              print "Message length is " + repr(len(msg))

              server = smtplib.SMTP('s mtp.gmail.com')
              server.set_debu glevel(1)
              server.sendmail (fromaddr, toaddrs, msg)
              server.quit()

              send: 'mail FROM:<myMail@gm ail.comsize=106 \r\n'
              reply: '530 5.7.0 Must issue a STARTTLS command first
              s1sm7666914uge\ r\n'

              What am I doing wrong ?
              >
              gmail.com requires authentication before sending mail. Try this
              sequence: ehlo, starttls, ehlo (again!), login, sendmail, quit
              (BTW, you need a space after Subject:, and all \n should be \r\n)
              >
              >
              --
              Gabriel Genellina
              Softlab SRL
              >
              _______________ _______________ _______________ _____
              Correo Yahoo!
              Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
              ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

              Comment

              • Tim Williams

                #8
                Re: help using smtplib to work ..

                On 8 Nov 2006 19:45:00 -0800, Jordan <jordan.taylor2 @gmail.comwrote :
                For some reason,
                smtp for gmail seems to require that you call server.ehlo() a second
                time, after having called server.starttls (), otherwise, the server
                won't accept authorization. Thanks.
                Not just GMAIL, all (RFC Compliant) SMTP servers require a 2nd EHLO
                after a successful STARTTLS command.

                The list of returned options / available commands will usually be
                different for the 2nd EHLO. Often this will just mean the removal of
                the STARTTLS option, but sometimes there can be additional
                functionality offered.

                HTH :)

                Comment

                • Gabriel Genellina

                  #9
                  Re: help using smtplib to work ..

                  At Thursday 9/11/2006 00:45, Jordan wrote:
                  >Your post was definitely the most helpful for me. For some reason,
                  >smtp for gmail seems to require that you call server.ehlo() a second
                  >time, after having called server.starttls (), otherwise, the server
                  >won't accept authorization. Thanks.
                  That's clearly stated on the docs for the starttls method:



                  --
                  Gabriel Genellina
                  Softlab SRL

                  _______________ _______________ _______________ _____
                  Correo Yahoo!
                  Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
                  ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

                  Comment

                  Working...