Created
July 5, 2017 06:59
-
-
Save jackyshan/00d52187ab9228e807360df412575abd to your computer and use it in GitHub Desktop.
邮件发送Python集成
This file contains hidden or 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 -*- | |
from email import encoders | |
from email.header import Header | |
from email.mime.text import MIMEText | |
from email.utils import parseaddr, formataddr | |
import smtplib | |
from_addr = '账号@163.com' | |
password = '密码' | |
smtp_server = 'smtp.163.com' | |
to_addr = '发到哪个邮箱' | |
def _format_addr(s): | |
name, addr = parseaddr(s) | |
return formataddr(( \ | |
Header(name, 'utf-8').encode(), \ | |
addr.encode('utf-8') if isinstance(addr, unicode) else addr)) | |
content = str("内容我是傻逼") | |
msg = MIMEText(content, 'plain', 'utf-8') | |
msg['From'] = _format_addr(from_addr) | |
msg['To'] = _format_addr(to_addr) | |
msg['Subject'] = Header('有新的标题', 'utf-8').encode() | |
server = smtplib.SMTP(smtp_server, 25) | |
server.set_debuglevel(1) | |
server.login(from_addr, password) | |
server.sendmail(from_addr, [to_addr], msg.as_string()) | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment