python 发送邮件示例

#!/usr/bin/python
#-*- coding: utf-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr, formatdate
 
def sendEmail():
    smtpHost = "smtp.mxhichina.com"
    #smtpPort = 25
    sslPort = 465
    username = "abc@example.com"
    password = "123456"
    fromTuple = (u"发送用户", "abc@example.com")
    toTuples = [(u"接收用户1", "111@example.com"), (u"接收用户2","222@example.com")]
 
    encoding = 'utf-8'
 
    fromAddr = fromTuple[1]
    fromHeader = formataddr((Header(fromTuple[0], encoding).encode(), fromTuple[1].encode(encoding)))
 
    toAddr = []
    toHeader = []
    for addrPair in toTuples:
        toAddr.append(addrPair[1])
        h = formataddr((Header(addrPair[0], encoding).encode(), addrPair[1].encode(encoding)))
        toHeader.append(h)
 
    msg = MIMEText(u"这里是正文。", "plain", encoding)
    msg['Subject'] = Header(u'这是主题', encoding).encode()
    msg['From'] = fromHeader
    msg['To'] = ','.join(toHeader)
    msg['Date'] = formatdate()
    #print (toAddr)
    #print (toHeader)
    #print (msg.as_string())
 
    #三种方式: 明文/TLS/SSL
    #1.普通方式,通信过程不加密 (不推荐)
    #smtp = smtplib.SMTP(smtpHost, smtpPort)
    #smtp.ehlo()
    #smtp.login(username, password)
 
    #2.TLS加密方式,正常smtp端口,通信过程加密
    #smtp = smtplib.SMTP(smtpHost, smtpPort)
    #smtp.ehlo()
    #smtp.starttls()
    #smtp.ehlo()
    #smtp.login(username, password)
 
    #3.SSL加密方式,使用ssl端口,通信过程加密 (推荐)
    smtp = smtplib.SMTP_SSL(smtpHost, sslPort, "example.com")
    smtp.set_debuglevel(True)
    smtp.login(username, password)
 
    try:
        smtp.sendmail(fromAddr, toAddr, msg.as_string())
    finally:
        smtp.quit()
 
 
if __name__ == '__main__':
    sendEmail()

2020/02/07 增加 python3 格式:

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr, formatdate
 
def sendEmail():
    smtpHost = "smtp.mxhichina.com"
    #smtpPort = 25
    sslPort = 465
    username = "abc@example.com"
    password = "123456"
    fromAddr = ("发送用户", "abc@example.com")
    toAddr = [("接收用户1", "111@example.com"), ("接收用户2","222@example.com")]
 
    encoding = 'utf-8'
 
    msg = MIMEText("这里是正文。", "plain", encoding)
    msg['Subject'] = Header('这是主题', encoding)
    msg['From'] = formataddr(fromAddr)
    msg['To'] = ','.join([formataddr(addr) for addr in toAddr])
    msg['Date'] = formatdate()
    #print (msg.as_string())
 
    #三种方式: 明文/TLS/SSL
    #1.普通方式,通信过程不加密 (不推荐)
    #smtp = smtplib.SMTP(smtpHost, smtpPort)
    #smtp.ehlo()
    #smtp.login(username, password)
 
    #2.TLS加密方式,正常smtp端口,通信过程加密
    #smtp = smtplib.SMTP(smtpHost, smtpPort)
    #smtp.ehlo()
    #smtp.starttls()
    #smtp.ehlo()
    #smtp.login(username, password)
 
    #3.SSL加密方式,使用ssl端口,通信过程加密 (推荐)
    smtp = smtplib.SMTP_SSL(smtpHost, sslPort, "example.com")
    #smtp.set_debuglevel(True)
    smtp.login(username, password)
 
    try:
        smtp.sendmail(fromAddr[1], [addr[1] for addr in toAddr], msg.as_string())
    finally:
        smtp.quit()
 
 
if __name__ == '__main__':
    sendEmail()