Sending e-mail

Vistro vistro at vistro.net
Thu Aug 28 15:09:10 EDT 2008


I used that code before, and it did not do what it needed to do. If you are
just sending text, this usually works for me.

def sendMail():

    ## Parameters for SMTP session
    port=587
    SMTPserver=  'smtp.gmail.com'
    SMTPuser= 'gmail username, which is your gmail address'
    pw= 'your gmail password'
    SENDER= SMTPuser


    ## Message details
    FROM=  SENDER
    TO= 'whoever you want to send it to'
    CC=FROM
    ##RECEIVERS= (TO, CC)  ##proper way to send to both TO and CC
    RECEIVERS= (TO,)  ## ignore the CC address


    subject= 'subject here'
    message= 'message here'


    print 'Please wait...'
    (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



    SMTPresult = session.sendmail(SENDER, RECEIVERS, BODY)  ## send email


    session.close()

On Thu, Aug 28, 2008 at 1:52 PM, <peter.jones.rpi at gmail.com> 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/<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 <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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080828/20e598c9/attachment-0001.html>


More information about the Python-list mailing list