If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

HTML mail

Sending mail from Web2Py can be done using the standard python modules smtplib and email. The code could be improved. When sending HTML mail it is necesssary to include a clear-text version. In the code I simply strip the html-tags... Just put in in your models directory. I use a global defs object containing IS_DEBUG and MAIL_SERVER with the smpt-server ip-address.

import smtplib
from email.MIMEMultipart import MIMEMultipart #@UnresolvedImport
from email.MIMEText import MIMEText #@UnresolvedImport
#from email.MIMEImage import MIMEImage #@UnresolvedImport

def send_email(sender,to,subject,msgHTML):

    fromaddr=sender
    #convert the list to a comma separated string
    if type(to)==type([]): 
        toaddrs=to
    else: 
        toaddrs=[to]
    toaddrs=", ".join(toaddrs)

    if defs.IS_DEBUG:  print "toaddres=",toaddrs

    # Create the root message and fill in the from, to, and subject headers
    # Encapsulate the plain and HTML versions of the message body in an
    # 'alternative' part, so message agents can decide which they want to display.
    msgRoot = MIMEMultipart('alternative')
    # (using 'related' gives the second part as an attachment)
    # Now fill in the from, to, and subject headers
    msgRoot['Subject'] = subject
    msgRoot['From'] = fromaddr
    msgRoot['To'] = toaddrs
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    msgPlain=msgHTML
    # remove used HTML tags
    for tag in ( "<table>","<tr>","<td>","</td>","<th>","</th>","<div>","<i>",\
                 "</i>","<p>","<b>","</b>","<h1>","<h2>","<h3>" ):
        msgPlain=msgPlain.replace(tag,"")
    for src in ( r"</p>",r"</h1>",r"</h2>","</h3>","</table>","</tr>" ):
        msgPlain=msgPlain.replace(src,chr(13))

    if defs.IS_DEBUG: print "msgPlain",msgPlain 

    # create and attach the plain version
    msgAlternative = MIMEText(msgPlain)
    msgRoot.attach(msgAlternative)

    # create and attach the HTML version
    msgText = MIMEText(msgHTML,'html')
    msgRoot.attach(msgText)

    if defs.IS_DEBUG: print msgRoot

    err=""  
    try:
        server = smtplib.SMTP() 
        server.connect(defs.EMAIL_SERVER)
        if defs.IS_DEBUG: print "The mailserver has been connected"
    except:
        err="SMTPconnect error while accessing mailhost:%s"%defs.EMAIL_SERVER
        return err

    try:
        server.sendmail(fromaddr, toaddrs, msgRoot.as_string())
        if defs.IS_DEBUG: print "ok, mail has been send"
    except smtplib.SMTPRecipientsRefused ,x:
        err="SMTPRecipientsRefused by %s %s using from %s to %s msg %s"%(defs.EMAIL_SERVER,x,fromaddr, toaddrs, msgRoot.as_string())
    except smtplib.SMTPHeloError,x:
        #    The server didn't reply properly to the "HELO" greeting.
        err="SMTPHeloError by %s %s using from %s to %s msg %s"%(defs.EMAIL_SERVER,x,fromaddr, toaddrs, msgRoot.as_string())
    except smtplib.SMTPSenderRefused,x:
        #The server didn't accept the from_addr.
        err="SMTP Sender refused by %s %s using from %s to %s msg %s"%(defs.EMAIL_SERVER,x,fromaddr, toaddrs, msgRoot.as_string())
    except smtplib.SMTPDataError,x:
        #    The server replied with an unexpected error code (other than a refusal of a recipient).     
        err="SMTPDATAerror by %s %s using from %s to %s msg %s"%(defs.EMAIL_SERVER,x,fromaddr, toaddrs, msgRoot.as_string())
    except:
        err="Unexpected error using mailhost: %s"%defs.EMAIL_SERVER

    if err and defs.IS_DEBUG: print err

    server.quit()
    if defs.IS_DEBUG: print "The mailserver has been disconnected"
    return err

Comments (0)


Hosting graciously provided by:
Python Anywhere