Sending e-mail

P

peter.jones.rpi

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.
 
W

Waldemar Osuch

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/
 
G

gordyt

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( ['(e-mail address removed)'], 'this is just a test',
"hello world!" )
 
P

peter.jones.rpi

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( ['(e-mail address removed)'], '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.
 
M

Mike Driscoll

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( ['(e-mail address removed)'], '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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top