Created
March 25, 2015 04:32
-
-
Save alfasin/7fec352d62c9852d73b3 to your computer and use it in GitHub Desktop.
Send email from shell (Python)
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
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.