/ Published in: Python
Send an e-mail message to the given addresses.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
from smtplib import SMTP DEF_FROM = '[email protected]' DEF_SMTP_SERVER = 'localhost' class SENDMAILError: def __init__( self, error ): self.error = error def __repr__( self ): return self.error def sendmail( toaddrs, msg, subject=None, fromaddr=DEF_FROM, smtpServer=DEF_SMTP_SERVER ): """Send an email to the given addresses""" if smtpServer is None: smtpServer = 'localhost' if type( toaddrs ) is StringType: toaddrs = toaddrs.split() elif not type( toaddrs ) is ListType: raise SENDMAILError( "toaddrs must be a string or a list" ) try: headers = ("From: %s\nTo: %s\n\n" % ( fromaddr, ", ".join( toaddrs ) ) ) if not subject is None: headers = "Subject: %s\n" % subject + headers server = SMTP( smtpServer ) server.sendmail( fromaddr, toaddrs, headers + msg ) server.quit() except Exception, e: raise SENDMAILError( str( e ) )