Skip to content

Instantly share code, notes, and snippets.

@denny0223 denny0223/send_email.py
Last active Sep 5, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.