Skip to content

Instantly share code, notes, and snippets.

@jongyeol
Created January 5, 2011 21:33
Show Gist options
  • Save jongyeol/767057 to your computer and use it in GitHub Desktop.
Save jongyeol/767057 to your computer and use it in GitHub Desktop.
Sample code to send mail with GMail by smtp, python
#!/usr/bin/python
# coding: utf-8
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import email.Charset
GMAIL_ID = '??????@gmail.com'
GMAIL_PW = 'password'
def send_gmail(to, subject, text):
msg = MIMEMultipart()
msg['From'] = u'sender name'
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text.encode('utf-8', 'replace'), 'html', 'utf-8'))
server.sendmail(GMAIL_ID, to, msg.as_string())
if __name__ == '__main__':
# setup gmail
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(GMAIL_ID, GMAIL_PW)
# send mails..
send_gmail('target@test.com', u'title', u'text')
# exit
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment