Skip to content

Instantly share code, notes, and snippets.

@denny0223
Last active September 5, 2017 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denny0223/e99b17cf9ef78b0566edc8be3f9a6342 to your computer and use it in GitHub Desktop.
Save denny0223/e99b17cf9ef78b0566edc8be3f9a6342 to your computer and use it in GitHub Desktop.
Send mail
# -*- coding: UTF-8 -*-
def send_email(recipient):
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr
gmail_user = ''
gmail_pwd = ''
FROM = formataddr((str(Header(u'SITCON 夏令營籌備團隊', 'utf-8')), "ask@sitcon.org"))
TO = recipient # recipient if type(recipient) is list else [recipient]
REPLY_TO_ADDRESS = formataddr((str(Header(u'SITCON 夏令營籌備團隊', 'utf-8')), "ask@sitcon.camp"))
msg = MIMEMultipart('alternative')
text = "Hi 歡迎來到 SITCON, HTML 版 看網頁啦"
fp = open('mail.html', 'r', encoding='UTF-8')
html = fp.read()
fp.close()
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
msg['Subject'] = '這是拿年會行前信來發的測試信 %s' % 'html file'
msg['From'] = FROM
msg['To'] = TO
msg.add_header('reply-to', REPLY_TO_ADDRESS)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, [TO], msg.as_string())
server.close()
print('successfully sent the mail')
except:
print("failed to send mail")
send_email('username <user@gmail.com>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment