Return to Snippet

Revision: 5840
at April 7, 2008 15:23 by kangell


Initial Code
from smtplib import SMTP
DEF_FROM = 'me@example.com'
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 ) )

Initial URL

                                

Initial Description
Send an e-mail message to the given addresses.

Initial Title
Send An Email Message

Initial Tags
email

Initial Language
Python