Created
January 5, 2011 21:33
-
-
Save jongyeol/767057 to your computer and use it in GitHub Desktop.
Sample code to send mail with GMail by smtp, 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
#!/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