Just plug in the SMTP server details and toggle AUTH if required.
Send an email to one or more recipients
Code Snippet
- import smtplib
- import email.utils
- from email.mime.text import MIMEText
- from email.utils import formataddr
- def emailer():
- # Email Settings
- MESSAGEBODY = 'Test Email body.'
- SUBJECT = 'Test Email Subject'
- FROM = ('Some User', 'donotreply@someurl.com')
- TO = ['recipient1@someurl.com', 'recipient2@someurl.com']
- # SMTP Settings
- smtpserver = 'SMTP SERVER ADDRESS'
- smtpport = 25
- AUTHREQUIRED = 0 # If you need to use SMTP AUTH set to 1
- smtpuser = 'foo' # For SMTP AUTH, set SMTP username here
- smtppass = 'bar' # For SMTP AUTH, set SMTP password here
- # Create the message
- msg = MIMEText(MESSAGEBODY)
- msg['To'] = ', '.join(TO)
- msg['From'] = email.utils.formataddr(FROM)
- msg['Subject'] = SUBJECT
- try:
- smtpObj = smtplib.SMTP(smtpserver, smtpport)
- if AUTHREQUIRED:
- session.login(smtpuser, smtppass)
- smtpObj.sendmail(msg['From'], TO, msg.as_string())
- print "Email has been sent successfully to: " + msg['To']
- except Exception, err:
- print "Error: unable to send error email notification. %s" % str(err)
- # Invoke Emailer
- emailer()
End of Code Snippet
Python Email Examples
http://docs.python.org/library/email-examples.html
No comments:
Post a Comment