Wednesday, May 22, 2013

Send email from Python

Save as Module "email.py". Import into your code and call with email.send_email(params)
import smtplib

smtp_host = 'your smtp host'
server = smtplib.SMTP(smtp_host)

###############################################################################

def send_email(_to, _from, _subject, _body):

    mime_fmt_body = (
    "From: {email_from}\r\n"
    "To: {email_to}\r\n"
    "Subject: {subject_text}\r\n"
    "\r\n"
    "{body_text}\r\n"
    ).format(email_from=_from, email_to=_to,subject_text=_subject,body_text=_body)

    server.sendmail(_from, _to, mime_fmt_body)
    server.quit

No comments:

Post a Comment