Skip to content

Instantly share code, notes, and snippets.

@aymanfarhat
Created January 1, 2014 10:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymanfarhat/8206766 to your computer and use it in GitHub Desktop.
Save aymanfarhat/8206766 to your computer and use it in GitHub Desktop.
Python snippet for sending email using unix sendmail
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from subprocess import Popen, PIPE
html = MIMEText("<html><head><title>Test Email</title></head><body>Some HTML</body>", "html")
msg = MIMEMultipart("alternative")
msg["From"] = "you@yourmail.com"
msg["To"] = "recipient@mail.com"
msg["Subject"] = "Python sendmail test"
msg.attach(html)
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
@davres1
Copy link

davres1 commented May 31, 2018

sendmail: fatal: oracle(501): No recipient addresses found in message header

@pydemo
Copy link

pydemo commented Jun 6, 2019

in Python 3 change last line to:
p.communicate(msg.as_string().encode())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment