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):

    #convert the list to a comma separated string
    if type(to)==type([]): 
    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.'

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

    if defs.IS_DEBUG: print "msgPlain",msgPlain 

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

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

    if defs.IS_DEBUG: print msgRoot

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

        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())
        err="Unexpected error using mailhost: %s"%defs.EMAIL_SERVER

    if err and defs.IS_DEBUG: print err

    if defs.IS_DEBUG: print "The mailserver has been disconnected"
    return err

Comments (0)

Hosting graciously provided by:
Python Anywhere