Skip to content

Instantly share code, notes, and snippets.

@vgoklani
Created December 7, 2011 16:39
Show Gist options
  • Save vgoklani/1443509 to your computer and use it in GitHub Desktop.
Save vgoklani/1443509 to your computer and use it in GitHub Desktop.
send emails via Gmail
#!/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