'''
Created on 2016年8月10日

@author: Administrator
'''
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

from_addr = 'leeo1124@163.com'#input('From: ')
password = input('Password: ')
to_addr = '450475851@qq.com'#input('To: ')
smtp_server = 'smtp.163.com'#input('SMTP server: ')

# 发送纯文本邮件
# msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')



# 发送HTML邮件
# msg = MIMEText('<html><body><h1>Hello</h1>' +
#     '<p>send by <a href="http://www.python.org">Python</a>...</p>' +
#     '</body></html>', 'html', 'utf-8')



# 发送带附件的邮件
# 邮件对象:
msg = MIMEMultipart()
msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()
 
# 邮件正文是MIMEText:
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
 
# 添加附件就是加上一个MIMEBase，从本地读取一个图片:
with open('D:/pythonWorkspace/pthonDemo/src/com/python/email/test.jpg', 'rb') as f:
    # 设置附件的MIME和文件名，这里是png类型:
    mime = MIMEBase('image', 'png', filename='test.png')
    # 加上必要的头信息:
    mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    mime.set_payload(f.read())
    # 用Base64编码:
    encoders.encode_base64(mime)
    # 添加到MIMEMultipart:
    msg.attach(mime)
 
msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', '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()
[((864, 879), 'email.mime.multipart.MIMEMultipart', 'MIMEMultipart', ([], {}), '()\n', (877, 879), False, 'from email.mime.multipart import MIMEMultipart\n'), ((1820, 1849), 'smtplib.SMTP', 'smtplib.SMTP', (['smtp_server', '(25)'], {}), '(smtp_server, 25)\n', (1832, 1849), False, 'import smtplib\n'), ((339, 351), 'email.utils.parseaddr', 'parseaddr', (['s'], {}), '(s)\n', (348, 351), False, 'from email.utils import parseaddr, formataddr\n'), ((1071, 1118), 'email.mime.text.MIMEText', 'MIMEText', (['"""send with file..."""', '"""plain"""', '"""utf-8"""'], {}), "('send with file...', 'plain', 'utf-8')\n", (1079, 1118), False, 'from email.mime.text import MIMEText\n'), ((1279, 1324), 'email.mime.multipart.MIMEBase', 'MIMEBase', (['"""image"""', '"""png"""'], {'filename': '"""test.png"""'}), "('image', 'png', filename='test.png')\n", (1287, 1324), False, 'from email.mime.multipart import MIMEBase\n'), ((1573, 1601), 'email.encoders.encode_base64', 'encoders.encode_base64', (['mime'], {}), '(mime)\n', (1595, 1601), False, 'from email import encoders\n'), ((1001, 1031), 'email.header.Header', 'Header', (['"""来自SMTP的问候……"""', '"""utf-8"""'], {}), "('来自SMTP的问候……', 'utf-8')\n", (1007, 1031), False, 'from email.header import Header\n'), ((1770, 1800), 'email.header.Header', 'Header', (['"""来自SMTP的问候……"""', '"""utf-8"""'], {}), "('来自SMTP的问候……', 'utf-8')\n", (1776, 1800), False, 'from email.header import Header\n'), ((375, 396), 'email.header.Header', 'Header', (['name', '"""utf-8"""'], {}), "(name, 'utf-8')\n", (381, 396), False, 'from email.header import Header\n')]