美文网首页
python email邮件发送

python email邮件发送

作者: 呦丶耍脾气 | 来源:发表于2022-09-23 10:43 被阅读0次

该类支持图片,附件、html和纯文本

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart


class SendEMail(object):
    """封装发送邮件类"""

    def __init__(self, host, port, username, pwd):
        self.username = username
        self.password = pwd

        # 邮箱服务器地址和端口
        self.smtp_s = smtplib.SMTP_SSL(host=host, port=port)

        # 发送方邮箱账号和授权码
        self.smtp_s.login(user=username, password=pwd)

    def send_text(self, to_user, content, subject, content_type='plain'):
        """
        发送文本邮件
        :param to_user: 对方邮箱
        :param content: 邮件正文
        :param subject: 邮件主题
        :param content_type: 内容格式:'plain' or 'html'
        :return:
        """
        msg = MIMEText(content, _subtype=content_type, _charset="utf8")

        msg["From"] = self.username
        msg["To"] = to_user
        msg["subject"] = subject

        self.smtp_s.send_message(msg, from_addr=self.username, to_addrs=to_user)

    def send_file(self, to_user, content, subject, reports_path, filename, content_type='plain'):
        """
        发送带文件的邮件
        :param to_user: 对方邮箱
        :param content: 邮件正文
        :param subject: 邮件主题
        :param reports_path: 文件路径
        :param filename: 邮件中显示的文件名称
        :param content_type: 内容格式
        """

        file_content = open(reports_path, "rb").read()

        msg = MIMEMultipart()

        text_msg = MIMEText(content, _subtype=content_type, _charset="utf8")
        msg.attach(text_msg)

        file_msg = MIMEApplication(file_content)
        file_msg.add_header('content-disposition', 'attachment', filename=filename)
        msg.attach(file_msg)

        msg["From"] = self.username
        msg["To"] = to_user
        msg["subject"] = subject

        self.smtp_s.send_message(msg, from_addr=self.username, to_addrs=to_user)

    def send_img(self, to_user, subject, content, filenames,imagemapping, content_type='html'):
        '''
        发送带图片的邮件
        :param to_user: 对方邮箱
        :param subject: 邮件主题
        :param content: 邮件正文
        :param filenames: 图片路径
        :param imagemapping: 图片路径
        :param content_type: 内容格式
        '''
        subject = subject
        msg = MIMEMultipart('related')
        # Html正文必须包含<img src="cid:imageid" alt="imageid" width="100%" height="100%>
        content = MIMEText(content, _subtype=content_type, _charset="utf8")
        msg.attach(content)
        msg['Subject'] = subject
        msg['From'] = self.username
        msg['To'] = to_user
        for index,filename in enumerate(filenames):
            if ((index+1)>len(imagemapping)):
                break
            with open(filename, "rb") as file:
                img_data = file.read()
            img = MIMEImage(img_data)
            img.add_header('Content-ID', imagemapping[index])
            msg.attach(img)

        self.smtp_s.sendmail(self.username, to_user, msg.as_string())

config = {'host':'smtp.163.com',"port":465,'username':'xxx@163.com','pwd':'123456'}
toemail = '123456@qq.com' #接收人多个用逗号分隔
emailObj = SendEMail(config['host'],config['port'],config['username'],config['pwd'])
'''
#发送文本
emailObj.send_text(toemail,'我是正文','我是标题') 
#发送html
message = '<p>Python 邮件发送测试...</p><p><a href="https://www.baidu.com">纵里寻她千百度</a></p>'
emailObj.send_text(toemail,message,'我是标题','html')
'''
'''
#发送文件
reports_path = 'D:\\zgjsks\\test.txt'
emailObj.send_file(toemail,'我是正文','我要发送文件',reports_path,'人员名单')
'''
#发送带图片
imagemapping = ['imageid1','imageid2']#要和正文中图片src中`cid:`后面的一一对应
message = '''
<p>Python 邮件发送测试...</p><p><img src="cid:imageid1"></p><p><a href="https://www.baidu.com">纵里寻她千百度</a></p><p><img src="cid:imageid2"></p>
'''
img_path = ['C:\\Users\\Administrator\\Downloads\\114.jpg','C:\\Users\\Administrator\\Downloads\\2.png']
emailObj.send_img(toemail,'我要发送图片的',message,img_path,imagemapping)


相关文章

  • python收发邮件

    Python发送邮件的两个包: smtplib 用来发送邮件。 email 用来构建邮件。 Python 的 em...

  • python -- Email , send(smtp), re

    python Email功能: 发送普通文本邮件 发送带有html格式的邮件 发送带有附件的邮件 发送插入图片到正...

  • python email邮件发送

    该类支持图片,附件、html和纯文本

  • 使用python写邮件

    python发邮件只需要用到两个模块,smtplib和email。smtplib模块主要负责发送邮件,email模...

  • Python使用smtplib,email模块发邮件

    摘要:Python 模块依赖 Python发邮件需要依赖两个模块,smtplib主要负责发送邮件,和email主要...

  • python发送邮件

    python发送邮件 准备 python中发送邮件主要用的是smtplib和email两个模块,下面主要对这两个模...

  • Python邮件正文及附件解析

    email邮件解析作为比较基础的模块,用来收取邮件、发送邮件。python的mail模块调用几行代码就能写一个发送...

  • 邮件发送

    python书写的邮件发送程序,使用到了email和smtplib两个库,email需要pip insta...

  • Python || 发送邮件

    使用Python的stmplib和email发送邮件: 一、纯文本邮件 二、HTML邮件 只需要在构造MIMETe...

  • 2018-07-02

    发送邮件 //发送邮件 @ResponseBody @RequestMapping("email") public...

网友评论

      本文标题:python email邮件发送

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