'''
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()
[((37, 6, 37, 21), 'email.mime.multipart.MIMEMultipart', 'MIMEMultipart', ({}, {}), '()', False, 'from email.mime.multipart import MIMEMultipart\n'), ((64, 9, 64, 38), 'smtplib.SMTP', 'smtplib.SMTP', ({(64, 22, 64, 33): 'smtp_server', (64, 35, 64, 37): '25'}, {}), '(smtp_server, 25)', False, 'import smtplib\n'), ((15, 17, 15, 29), 'email.utils.parseaddr', 'parseaddr', ({(15, 27, 15, 28): 's'}, {}), '(s)', False, 'from email.utils import parseaddr, formataddr\n'), ((43, 11, 43, 58), 'email.mime.text.MIMEText', 'MIMEText', ({(43, 20, 43, 39): '"""send with file..."""', (43, 41, 43, 48): '"""plain"""', (43, 50, 43, 57): '"""utf-8"""'}, {}), "('send with file...', 'plain', 'utf-8')", False, 'from email.mime.text import MIMEText\n'), ((48, 11, 48, 56), 'email.mime.multipart.MIMEBase', 'MIMEBase', (), '', False, 'from email.mime.multipart import MIMEBase\n'), ((56, 4, 56, 32), 'email.encoders.encode_base64', 'encoders.encode_base64', ({(56, 27, 56, 31): 'mime'}, {}), '(mime)', False, 'from email import encoders\n'), ((40, 17, 40, 61), 'email.header.Header', 'Header', ({(40, 24, 40, 51): '"""来自SMTP的问候……"""', (40, 53, 40, 60): '"""utf-8"""'}, {}), "('来自SMTP的问候……', 'utf-8')", False, 'from email.header import Header\n'), ((62, 17, 62, 61), 'email.header.Header', 'Header', ({(62, 24, 62, 51): '"""来自SMTP的问候……"""', (62, 53, 62, 60): '"""utf-8"""'}, {}), "('来自SMTP的问候……', 'utf-8')", False, 'from email.header import Header\n'), ((16, 23, 16, 44), 'email.header.Header', 'Header', ({(16, 30, 16, 34): 'name', (16, 36, 16, 43): '"""utf-8"""'}, {}), "(name, 'utf-8')", False, 'from email.header import Header\n')]