Created
December 7, 2011 16:39
-
-
Save vgoklani/1443509 to your computer and use it in GitHub Desktop.
send emails via Gmail
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
__links__ = r'https://gist.github.com/916362' | |
import smtplib | |
from datetime import datetime | |
import email.utils | |
from email.mime.text import MIMEText | |
def send_email(username, password, from_email, to_email, subject, message): | |
""" | |
Sends an email message via Gmail | |
""" | |
server=smtplib.SMTP('smtp.gmail.com:587') # Initialize SMTP server | |
server.starttls() | |
server.login(username, password) | |
msg = MIMEText(message) | |
msg['To'] = email.utils.formataddr((to_email.split('@')[0], to_email)) | |
msg['From'] = email.utils.formataddr((from_email.split('@')[0], from_email)) | |
msg['Subject'] = subject | |
server.sendmail(from_email, to_email, msg.as_string()) | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment