简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等
如下所示,
加群:960410445 即可获取数十套PDF!
# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.encoders import encode_base64
import os
import traceback
def send_mail(mail_title,
mail_content=None,
attachment_img=None,
attachment_txt=None,
attachment_pdf=None,
attachment_excel=None,
attachment_word=None):
# qq邮箱smtp服务器
host_server = 'smtp.qq.com'
# sender_qq为发件人的qq号码
sender_qq = '947118251'
# pwd为qq邮箱的授权码
pwd = 'tvjl******zpbebb'
# 发件人的邮箱
sender_qq_mail = '947118251@qq.com'
# 收件人邮箱
# receiver = 'znwindy@gmail.com'
receiver = '947118251@qq.com'
try:
# ssl登录
smtp = SMTP_SSL(host_server)
# set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)
# msg = MIMEText(mail_content, "plain", 'utf-8')
msg = MIMEMultipart('related')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)
# image attach
if attachment_img:
mail_body = '%s
' % (mail_content, attachment_img)
msgText = (MIMEText(mail_body, 'html', 'utf-8'))
msgAlternative.attach(msgText)
with open(attachment_img, 'rb') as fp:
msgImage = MIMEImage(fp.read())
msgImage.add_header('Content-ID', ''.format(attachment_img))
msg.attach(msgImage)
# txt attach
if attachment_txt:
file_name = os.path.split(attachment_txt)[1]
att1 = MIMEText(open(attachment_txt, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = f'attachment; filename="{file_name}"'
msg.attach(att1)
# pdf attach
if attachment_pdf:
with open(attachment_pdf, "rb") as fp:
fileMsg = MIMEBase('application', 'pdf')
fileMsg.set_payload(fp.read())
encode_base64(fileMsg)
fileMsg.add_header('Content-Disposition', f'attachment;filename={os.path.split(attachment_pdf)[1]}')
msg.attach(fileMsg)
# excel attach
if attachment_excel:
part = MIMEBase('application', "vnd.ms-excel")
with open(attachment_excel, "rb") as fp:
part.set_payload(fp.read())
encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_excel)[1]}"')
msg.attach(part)
# word attach
if attachment_word:
with open(attachment_word, "rb") as fp:
part = MIMEApplication(fp.read())
part.add_header('Content-Disposition', f'attachment; filename="{os.path.split(attachment_word)[1]}"')
part.set_charset('utf-8')
msg.attach(part)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
print('Success!')
except:
print('Error!')
traceback.print_exc()
if __name__ == '__main__':
send_mail(mail_title='爬虫结束了,正常退出!',
mail_content='你好,这是使用python登录qq邮箱发邮件的测试',
attachment_img='../data/test.jpg',
attachment_txt='../data/start_urls.txt',
attachment_pdf='../data/Gmail - How to add images in the product description_.pdf',
attachment_excel='../data/shops.xlsx',
attachment_word='../data/asdasd.docx')