美文网首页Python新世界
python 发送带各种附件的邮件示例!

python 发送带各种附件的邮件示例!

作者: 919b0c54458f | 来源:发表于2018-12-30 13:59 被阅读4次

简述下如何使用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:

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')

相关文章

  • python发送邮件附件

    python发送邮件附件 完整示例:

  • python 发送带各种附件的邮件示例

    简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等 如...

  • python 发送带各种附件的邮件示例!

    简述下如何使用python发送各种附件的邮件,比如word、excel、pdf、txt,以及在正文插入图片等等 如...

  • Python之发送邮件

    Python之发送邮件 使用SMTP模块发送邮件 发送HTML文件 发送带附件的文件 Django发送文件 各大邮...

  • 使用Python发送邮件, 抄送多个收信人以及添加附件

    在豆瓣主题 "python发送带附件的gmail邮件" 看到的可以发送附件的Python代码.自己根据需求增加了抄...

  • SMTP 发送邮件

    SMTP 是发送邮件的协议,Python 内置对 SMTP 的支持,可以发送纯文本邮件、HTML 邮件以及带附件的...

  • Python发送邮件

    Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 Python对SMTP支持有...

  • 61. SMTP发送邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 P...

  • 邮件发送

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。 S...

  • python群发邮件

    python发送邮件 发送一个普通文本邮件 参数化--读取yaml配置文件 yaml配置文件 打包发送带附件的邮件

网友评论

    本文标题:python 发送带各种附件的邮件示例!

    本文链接:https://www.haomeiwen.com/subject/pijvlqtx.html