Last active
February 1, 2016 13:49
-
-
Save fbidu/f299e2d3f5d9072c0c55 to your computer and use it in GitHub Desktop.
script that sends an HTML formatted email using sendmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Module that provides an API for interacting with sendmail using Python | |
""" | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
from subprocess import Popen, PIPE | |
def send_email(sender, receiver, subject, body): | |
""" Function that sends an HTML email through sendmail and returns a tuple | |
containing the output and possible errors""" | |
email = MIMEMultipart('alternative') # Header for HTML | |
email["From"] = sender # Setting the sender... | |
email["To"] = receiver # the receiver... | |
email["Subject"] = subject # and the subject! | |
email_body = MIMEText(body, 'html') # Formatting the body as HTML | |
email.attach(email_body) # Attaching the message to the email | |
# Openning a process for sendmail | |
process = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) | |
# Pipeing the data and getting the output | |
output, error = process.communicate(email.as_string()) | |
# Reconstructing the tuple. I separated the parameters for clarity. | |
return (output, error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment