Sending e-mail

Discussion in 'Python' started by peter.jones.rpi@gmail.com, Aug 28, 2008.

  1. Guest

    I work at a training center and I would like to use Python to generate
    a number of certificates and then e-mail them. The certificates are a
    problem for another day - right now I just want to figure out how to
    send an e-mail.

    I confess I don't know much about the protocol(s) for e-mail. In PHP
    using CodeIgniter, the same task was amazingly easy. I think this is a
    bit harder because I'm not executing code on a remote machine that has
    its own SMTP server. According to (http://www.devshed.com/c/a/Python/
    Python-Email-Libraries-SMTP-and-Email-Parsing/), I need to use the
    SMTP object found in the smtplib module to initiate a connection to a
    server before I can send. I want to use Gmail, but on the following
    input it stalls and then raises smtplib.SMTPServerDisconnected:

    server = SMTP("smtp.gmail.com")

    I also pinged smtp.gmail.com and tried it with the dotted quad IP as a
    string. Am I on the right track? Is this a problem with gmail, or have
    I gotten an assumption wrong?

    Here's a thought: Could I use SMTPServer (http://docs.python.org/lib/
    node620.html) to obviate the need to have anything to do with Gmail?
    What would be the limitations on that? Could I successfully do this
    and wrap the whole thing up in a black box? What modules would I need?

    Thanks.
     
    , Aug 28, 2008
    #1
    1. Advertising

  2. On Aug 28, 12:52 pm, wrote:
    > I work at a training center and I would like to use Python to generate
    > a number of certificates and then e-mail them. The certificates are a
    > problem for another day - right now I just want to figure out how to
    > send an e-mail.
    >
    > I confess I don't know much about the protocol(s) for e-mail. In PHP
    > using CodeIgniter, the same task was amazingly easy. I think this is a
    > bit harder because I'm not executing code on a remote machine that has
    > its own SMTP server. According to (http://www.devshed.com/c/a/Python/
    > Python-Email-Libraries-SMTP-and-Email-Parsing/), I need to use the
    > SMTP object found in the smtplib module to initiate a connection to a
    > server before I can send. I want to use Gmail, but on the following
    > input it stalls and then raises smtplib.SMTPServerDisconnected:
    >
    > server = SMTP("smtp.gmail.com")
    >
    > I also pinged smtp.gmail.com and tried it with the dotted quad IP as a
    > string. Am I on the right track? Is this a problem with gmail, or have
    > I gotten an assumption wrong?
    >
    > Here's a thought: Could I use SMTPServer (http://docs.python.org/lib/
    > node620.html) to obviate the need to have anything to do with Gmail?
    > What would be the limitations on that? Could I successfully do this
    > and wrap the whole thing up in a black box? What modules would I need?
    >
    > Thanks.


    Gmail SMTP server needs authentication.
    I little googling found this example.
    I did not test it if it works but it could be starting point.

    http://codecomments.wordpress.com/2008/01/04/python-gmail-smtp-example/
     
    Waldemar Osuch, Aug 28, 2008
    #2
    1. Advertising

  3. gordyt Guest

    Peter here is an example. I just tried it and it works fine.

    from smtplib import SMTP
    HOST = "smtp.gmail.com"
    PORT = 587
    ACCOUNT = "" # put your gmail email account name here
    PASSWORD = "" # put your gmail email account password here

    def send_email(to_addrs, subject, msg):
    server = SMTP(HOST,PORT)
    server.set_debuglevel(1) # you don't need this
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(ACCOUNT, PASSWORD)
    server.sendmail(ACCOUNT, to_addrs,
    """From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
    ACCOUNT, ",".join(to_addrs), subject, msg
    )
    )
    server.quit()

    if __name__ == "__main__":
    send_email( [''], 'this is just a test',
    "hello world!" )
     
    gordyt, Aug 28, 2008
    #3
  4. Guest

    On Aug 28, 3:23 pm, gordyt <> wrote:
    > Peter here is an example. I just tried it and it works fine.
    >
    > from smtplib import SMTP
    > HOST = "smtp.gmail.com"
    > PORT = 587
    > ACCOUNT = "" # put your gmail email account name here
    > PASSWORD = "" # put your gmail email account password here
    >
    > def send_email(to_addrs, subject, msg):
    > server = SMTP(HOST,PORT)
    > server.set_debuglevel(1) # you don't need this
    > server.ehlo()
    > server.starttls()
    > server.ehlo()
    > server.login(ACCOUNT, PASSWORD)
    > server.sendmail(ACCOUNT, to_addrs,
    > """From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
    > ACCOUNT, ",".join(to_addrs), subject, msg
    > )
    > )
    > server.quit()
    >
    > if __name__ == "__main__":
    > send_email( [''], 'this is just a test',
    > "hello world!" )


    Thanks to everyone who's replied. gordyt, I didn't dare dream anyone
    would hand me fully functional source code, so thank you very much for
    that. Unfortunately, it doesn't work for me, likely because of some
    complication from my company's firewall.

    All things considered, going through Gmail is an unnecessary step if I
    can run a server on my own PC. Is there any hope of this working? Can
    it be done easily? Is there anything I should know about the
    SMTPServer object, and are there any other modules I'd need?

    Thanks again for all the help.
     
    , Aug 29, 2008
    #4
  5. On Aug 29, 11:44 am, wrote:
    > On Aug 28, 3:23 pm, gordyt <> wrote:
    >
    >
    >
    > > Peter here is an example.  I just tried it and it works fine.

    >
    > > from smtplib import SMTP
    > > HOST = "smtp.gmail.com"
    > > PORT = 587
    > > ACCOUNT = ""  # put your gmail email account name here
    > > PASSWORD = ""  # put your gmail email account password here

    >
    > > def send_email(to_addrs, subject, msg):
    > >     server = SMTP(HOST,PORT)
    > >     server.set_debuglevel(1)    # you don't need this
    > >     server.ehlo()
    > >     server.starttls()
    > >     server.ehlo()
    > >     server.login(ACCOUNT, PASSWORD)
    > >     server.sendmail(ACCOUNT, to_addrs,
    > >         """From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n.\r\n""" % (
    > >             ACCOUNT, ",".join(to_addrs), subject, msg
    > >         )
    > >     )
    > >     server.quit()

    >
    > > if __name__ == "__main__":
    > >     send_email( [''], 'this is just a test',
    > >         "hello world!" )

    >
    > Thanks to everyone who's replied. gordyt, I didn't dare dream anyone
    > would hand me fully functional source code, so thank you very much for
    > that. Unfortunately, it doesn't work for me, likely because of some
    > complication from my company's firewall.
    >
    > All things considered, going through Gmail is an unnecessary step if I
    > can run a server on my own PC. Is there any hope of this working? Can
    > it be done easily? Is there anything I should know about the
    > SMTPServer object, and are there any other modules I'd need?
    >
    > Thanks again for all the help.


    I would recommend looking at the email module too as it is a little
    bit more flexible:

    http://docs.python.org/lib/module-email.html

    You could also see how I do it in wxPython: http://www.blog.pythonlibrary.org/?p=38

    My script is Windows only at the moment.

    Mike
     
    Mike Driscoll, Aug 29, 2008
    #5
    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:
    4
    Views:
    55,565
  2. =?Utf-8?B?T3Bh?=

    Sending Mail via System.Net.Mail problem

    =?Utf-8?B?T3Bh?=, Jan 25, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,627
    sachya
    Jan 31, 2006
  3. =?ISO-8859-1?Q?Andr=E9s_Berenguer?=

    Sending e-mail with attachments with the default e-mail client

    =?ISO-8859-1?Q?Andr=E9s_Berenguer?=, Aug 29, 2003, in forum: Java
    Replies:
    0
    Views:
    520
    =?ISO-8859-1?Q?Andr=E9s_Berenguer?=
    Aug 29, 2003
  4. Replies:
    0
    Views:
    1,440
  5. Luke
    Replies:
    2
    Views:
    4,778
    Nigel Wade
    Mar 15, 2007
Loading...

Share This Page