Skip to content

Instantly share code, notes, and snippets.

@alfasin
Created March 25, 2015 04:32
Show Gist options
  • Save alfasin/7fec352d62c9852d73b3 to your computer and use it in GitHub Desktop.
Save alfasin/7fec352d62c9852d73b3 to your computer and use it in GitHub Desktop.
Send email from shell (Python)
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from subprocess import Popen, PIPE
def send_email(msg):
"""
send an email
:param msg: message to be sent in the email's body
:return: None
"""
content = MIMEText(msg, "plain/text")
msg = MIMEMultipart("alternative")
msg["From"] = "alfasin@gmail.com"
msg["To"] = "alfasin@gmail.com"
msg["Subject"] = "Test Email"
msg.attach(content)
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
@alfasin
Copy link
Author

alfasin commented Mar 25, 2015

This is a small utility that I use whenever I'm on a remote shell and can't copy data (no mouse etc).
It's easy to paste the content of this gist into vim, save with execute permissions and call it with the requested content.

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