记录博客 ZH-BLOG

Python 发送邮件

时间:2018-07-25 10:21:44分类:python 基础

1.发送只包含文本的邮件

import smtplib
from email.mime.text import MIMEText
from email.header import Header
# qq授权码:****** (充当密码登录使用)
gmail_stmp=smtplib.SMTP('smtp.qq.com:587')
gmail_stmp.ehlo()
gmail_stmp.starttls()
gmail_stmp.login('769031839@qq.com','******')
mail_con=MIMEText('我爱你!!!','plain','utf-8') # 邮件内容
mail_con['Subject']=Header('这是一封测试邮件','utf-8')  # 邮件标题
mail_con['From']='769031839@qq.com' # 邮件发送人
mail_con['To']='2468810057@qq.com'  # 邮件接收人
gmail_stmp.sendmail('From:769031839@qq.com','To:2468810057@qq.com',mail_con.as_string())
gmail_stmp.quit()
print('Done.')

通过 excel 人员名单,给其中会员发送邮件

import openpyxl,os,smtplib
from email.header import Header
from email.mime.text import MIMEText

def send_email(mail):
    qq_mail=smtplib.SMTP('smtp.qq.com:587')
    qq_mail.ehlo()
    qq_mail.starttls()
    qq_mail.login('769031839@qq.com','******')
    mail_con=MIMEText('注意:\n 这个会员邮件\r\n Thanks!!!','plain','utf-8')
    mail_con['Subject']='尊敬的会员用户'
    mail_con['From']='769031839@qq.com'
    mail_con['To']=mail
    qq_mail.sendmail('769031839@qq.com',mail,mail_con.as_string())
    qq_mail.quit()
    print('%s邮件发送成功'%mail)

wb=openpyxl.load_workbook(os.path.join('E:\\','path','user.xlsx'))
sheet=wb.active
for row in sheet.iter_rows():
    mail=row[0].value
    flag=row[1].value
    if flag==1: # flag 为 1,表示为会员,需要发送邮件
        send_email(mail)
wb.close()

如果要发送包含图片或者添加附件等邮件,需要 MIMEMultipart,它是 MIME 消息的中间基类,可以携带各种 MIME 消息对象

2.使用 MIMEMultipart 发送纯文本消息

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header
from email.mime.multipart import MIMEMultipart

mail_smtp = smtplib.SMTP('smtp.qq.com:587')
mail_smtp.ehlo()
mail_smtp.starttls()
mail_smtp.login('769031839@qq.com','授权码')

multipart = MIMEMultipart()
multipart['From'] = '769031839@qq.com'
multipart['To'] = '2468810057@qq.com'
multipart['Subject'] = Header('Python 测试纯文本邮件', 'utf-8')

text_mime = MIMEText('Python 测试纯文本邮件', 'plain', 'utf-8')
multipart.attach(text_mime)

mail_smtp.sendmail('From:769031839@qq.com','To:2468810057@qq.com',multipart.as_string())
mail_smtp.quit()
print('发送成功!')

3.使用 MIMEMultipart 发送带图片消息

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header

mail_smtp = smtplib.SMTP('smtp.qq.com:587')
mail_smtp.ehlo()
mail_smtp.starttls()
mail_smtp.login('769031839@qq.com','授权码')

multipart = MIMEMultipart()
multipart['From'] = '769031839@qq.com'
multipart['To'] = '2468810057@qq.com'
multipart['Subject'] = 'Python 测试文本+图片邮件'

text = """这是Python测试文本+图片邮件<img src="cid:img">"""
text_mime = MIMEText(text, 'html', 'utf-8')

with open(r'e:\path\6.jpg', 'rb') as fp:
image_data = fp.read()
image_mime = MIMEImage(image_data)
image_mime.add_header('Content-ID', '<img>')

multipart.attach(text_mime)
multipart.attach(image_mime)

mail_smtp.sendmail('From:769031839@qq.com','To:2468810057@qq.com',multipart.as_string())
mail_smtp.quit()
print('发送成功!')

4.使用 MIMEmultipart 发送带有图片并添加附件文件的消息

import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email import encoders
import base64

email_smtp = smtplib.SMTP('smtp.qq.com:587')
email_smtp.ehlo()
email_smtp.starttls()
email_smtp.login('769031839@qq.com','授权码')

multipart = MIMEMultipart()
multipart['From'] = '769031839@qq.com'
multipart['To'] = '2468810057@qq.com'
multipart['Subject'] = '这个一封Python测试附件邮件'

text_mime = MIMEText('Python 测试附件邮件<img src="cid:img">', 'html', 'utf-8')

with open(r'e:\path\6.jpg', 'rb') as fp:
image_data = fp.read()
image_mime = MIMEImage(image_data)
image_mime.add_header('Content-ID', '<img>')

with open(r'e:\path\abc.txt', 'rb') as fp:
txt_data = fp.read()
attchment_txt = MIMEText(txt_data, 'base64', 'utf-8')
attchment_txt['Content-Type'] = 'application/octet-stream'
# attchment_txt['Content-Disposition'] = 'attachment; filename="abc.txt"' # 中文乱码
attchment_txt.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '测试txt.txt'))

with open(r'e:\path\abc.xlsx', 'rb') as fp:
excel_data = fp.read()
attchment_excel = MIMEText(excel_data, 'base64', 'utf-8')
attchment_excel['Content-Type'] = 'application/octet-stream'
# attchment_excel['Content-Disposition'] = 'attachment; filename="abc.xlsx"' # 中文乱码
attchment_excel.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '测试xlsx.xlsx'))

multipart.attach(text_mime)
multipart.attach(image_mime)
multipart.attach(attchment_txt)
multipart.attach(attchment_excel)

email_smtp.sendmail('From:769031839@qq.com','To:2468810057@qq.com',multipart.as_string())
email_smtp.quit()
print('发送成功!')