Skip to content

Instantly share code, notes, and snippets.

@initbrain
Last active August 29, 2015 14:02
Show Gist options
  • Save initbrain/038c40df7a19ae50bcf1 to your computer and use it in GitHub Desktop.
Save initbrain/038c40df7a19ae50bcf1 to your computer and use it in GitHub Desktop.
Send HTTP mail with Python
#!/usr/bin/env python
import sys
import traceback
import smtplib
from socket import gaierror
from email import MIMEMultipart
from email import MIMEText
from email.utils import formatdate
from time import *
from my_app.config import my_app_cfg #TODO change me
# Configuration SMTP
smtpServer = my_app_cfg.emails['server'] #TODO change me
smtpPort = my_app_cfg.emails['port'] #TODO change me
fromAddr = my_app_cfg.emails['from'] #TODO change me
smtpUser = my_app_cfg.emails['user'] #TODO change me
smtpPwd = my_app_cfg.emails['pwd'] #TODO change me
def sendEmail(destAddr, mailSubject, mailBody, mailHtmlBody=None):
print "[+] Sending to : %s" % ', '.join(destAddr)
# Start time
startTime = time()
# Create message container - the correct MIME type is multipart/alternative.
if mailHtmlBody is not None:
msg = MIMEMultipart.MIMEMultipart('alternative')
else:
msg = MIMEMultipart.MIMEMultipart()
#msg = MIMEMultipart.MIMEMultipart()
msg['From'] = EMAILS_FROM
msg['To'] = ', '.join(destAddr)
msg['Date'] = formatdate(localtime=True) # date
msg['Subject'] = mailSubject
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText.MIMEText(mailBody, 'plain')
if mailHtmlBody is not None:
part2 = MIMEText.MIMEText(mailHtmlBody, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
if mailHtmlBody is not None:
msg.attach(part2)
#msg.attach(MIMEText.MIMEText(mailBody, 'plain'))
try:
server = smtplib.SMTP(EMAILS_SERVER, EMAILS_PORT)
server.ehlo()
server.starttls()
server.ehlo()
server.login(EMAILS_USER, EMAILS_PWD)
server.set_debuglevel(0)
server.sendmail(EMAILS_FROM, destAddr, msg.as_string())
server.quit()
except gaierror:
print "[!] Connexion error"
return 0
except Exception as e:
print "[!] Error\n%s" % e
sys.stderr.write("[%s] Error: %s\n" % (strftime('%Y-%m-%d %H:%M:%S', localtime()), str(e)))
traceback.print_exc()
sys.stderr.write("\n")
return 0
else:
# Traitement terminé
print "[+] Message sent (in " + strftime('%H:%M:%S',
gmtime(int(time() - startTime))) + ")"
return 1
if __name__ == "__main__":
# Example
destAddr = ["[email protected]", "[email protected]"] # Recipient(s)
mailSubject = "Link" # Subject
# Create the body of the message (a plain-text and an HTML version).
mailBody = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
mailHtmlBody = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
sendEmail(destAddr, mailSubject, mailBody, mailHtmlBody)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment