关于python:Python37-发送邮件完整版和简单版

31次阅读

共计 3406 个字符,预计需要花费 9 分钟才能阅读完成。

指标:126/163 邮箱

import smtplib
import time

from email.header import Header
from email import encoders
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

def SendMailSimple(sender, receivers, mail_pass, content, file, image):
    mail_host = "smtp.126.com"  # 设置服务器
    # message = MIMEText(content, 'plain', 'utf-8')# 注释内容   plain 代表纯文本
    message = MIMEMultipart()
    message.attach(MIMEText(content)) # 邮件注释
    message['From'] = sender
    message['To'] = ','.join(receivers) 
    subject = 'Python 主动简略邮件 -%s' % time.ctime()
    message['Subject'] = subject  # 邮件题目

    try:
        # 增加文件到附件
        with open(file, 'rb') as f:
            # MIMEBase 示意附件的对象
            mime = MIMEBase('text', 'txt', filename=file)
            # filename 是显示附件名字
            mime.add_header('Content-Disposition', 'attachment', filename=file)
            # 获取附件内容
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
            # 作为附件增加到邮件
            message.attach(mime)
    except FileNotFoundError as e:
        print(e)

    try:
        with open(image, 'rb') as f:
            # 图片增加到附件
            mime = MIMEBase('image', 'image', filename=image)
            mime.add_header('Content-Disposition', 'attachment', filename=image)
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
            message.attach(mime)
    except FileNotFoundError as e:
        print(e)

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(sender, mail_pass)
        smtpObj.sendmail(sender, receivers , str(message))  # message.as_string()
        smtpObj.quit()
        print("邮件发送胜利")
    except smtplib.SMTPException as e:
        print(e)


def SendMail(sender, receivers, cc_mail, mail_pass, content, file, image):
    # 第三方 SMTP 服务
    mail_host = "smtp.126.com"  # 设置服务器

    # 结构一个 MIMEMultipart 对象代表邮件自身
    message = MIMEMultipart()
    message.attach(MIMEText(content, 'html', 'utf-8'))  # 注释内容   plain 代表纯文本,html 代表反对 html 文本

    message['From'] = sender
    message['To'] = ','.join(receivers)  # 与真正的收件人的邮箱不是一回事
    message['Cc'] = ','.join(cc_mail)

    subject = 'Python 主动邮件 -%s' % time.ctime()
    message['Subject'] = subject  # 邮件题目

    # 增加文件到附件
    with open(file, 'rb') as f:
        # MIMEBase 示意附件的对象
        mime = MIMEBase('text', 'txt', filename=file)
        # filename 是显示附件名字
        mime.add_header('Content-Disposition', 'attachment', filename=file)
        # 获取附件内容
        mime.set_payload(f.read())
        encoders.encode_base64(mime)
        # 作为附件增加到邮件
        message.attach(mime)

    with open(image, 'rb') as f:
        # 图片增加到附件
        mime = MIMEBase('image', 'image', filename=image)
        mime.add_header('Content-Disposition', 'attachment', filename=image)
        mime.set_payload(f.read())
        encoders.encode_base64(mime)
        message.attach(mime)

    # 将图片显示在注释
    with open(image, 'rb') as f:
        # 图片增加到注释
        msgImage = MIMEImage(f.read())
        # 定义图片 ID
    msgImage.add_header('Content-ID', '<image1>')
    message.attach(msgImage)

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(sender, mail_pass)
        smtpObj.sendmail(sender, receivers + cc_mail, str(message))  # message.as_string()
        smtpObj.quit()
        print("邮件发送胜利")
    except smtplib.SMTPException as e:
        print(e)



if __name__ == "__main__":
    sender = 'aaaaaa@126.com'  # 邮件发送方
    receivers = ['aaaaaa@126.com', 'ZZZZZZ@139.com']  # 接管邮件的邮箱
    receivers_single = ['aaaaaa@126.com']  # 接管邮件的邮箱
    cc_mail = ['PPPPPP@qq.com']  # 抄送人


    # 口令受权码,不含空格
    mail_pass = "xxxxxxxxxx"

    content_text = "邮件注释测试"

    content = ''' 连忙加油往前冲
            <h1> 测试 test</h1>
            <h2 style="color:red"> 看看这里显示什么 </h1>
            <a href="http://www.runoob.com/python/python-email.html"> 测试超链接 </a><br>
            <p> 图片显示测试:</p>
            <p><img src="cid:image1"></p>
          '''file ='Fx12.txt'image ='language2.jpg'
    #SendMail(sender, receivers, cc_mail, mail_pass, content, file, image)
    SendMailSimple(sender, receivers_single, mail_pass, content_text, file, image)

本机运行后果:
D:\development\python_test_proj_2022\venv\Scripts\python.exe D:/development/python_test_proj_2022/venv/pyRun.py
[Errno 2] No such file or directory: ‘Fx12.txt’
[Errno 2] No such file or directory: ‘language2.jpg’
邮件发送胜利

正文完
 0