python sendmail

 

https://www.cnblogs.com/shenh/p/14267345.html

 

https://blog.csdn.net/wzh191920/article/details/78945354

 

 

from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
# 包含附件的
def sendMail_1(message,Subject,sender_show,recipient_show,to_addrs,cc_show=''):
    '''
    :param message: str 邮件内容
    :param Subject: str 邮件主题描述
    :param sender_show: str 发件人显示,不起实际作用如:"xxx"
    :param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
    :param to_addrs: str 实际收件人
    :param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
    '''
    # 填写真实的发邮件服务器用户名、密码
    user = ''
    password = ''
    # 邮件内容
    msg = MIMEMultipart()
    msg.attach(MIMEText(message, 'html', _charset="utf-8"))
    # 构造附件1,传送当前目录下的 test.txt 文件
    att = MIMEText(open(r'D:\下载\20220921_diffResult (1).txt', 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    # 附件名称为中文时的写法
    # att.add_header("Content-Disposition", "attachment", filename=("gbk", "", filename))
    att.add_header("Content-Disposition", "attachment", filename=("gbk", "", '20220921_diffResult (1).txt'))
    # 附件名称非中文时的写法,这里的filename可以任意写,写什么名字,邮件中显示什么名字
    # att["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)
    msg.attach(att)
    # 邮件主题描述
    msg["Subject"] = Subject
    # 发件人显示,不起实际作用
    # msg["from"] = sender_show
    msg["from"]=Header(user)
    # 收件人显示,不起实际作用
    msg["to"] = recipient_show
    # 抄送人显示,不起实际作用
    msg["Cc"] = cc_show
    with SMTP_SSL(host="smtp.sina.com",port=465) as smtp:
        # 登录发送邮件服务器
        smtp.login(user = user, password = password)
        # 实际发送、接收邮件配置
        smtp.sendmail(from_addr = user, to_addrs=to_addrs.split(','), msg=msg.as_string())


def sendMail(message, Subject, sender_show, recipient_show, to_addrs, cc_show=''):
    '''
    :param message: str 邮件内容
    :param Subject: str 邮件主题描述
    :param sender_show: str 发件人显示,不起实际作用如:"xxx"
    :param recipient_show: str 收件人显示,不起实际作用 多个收件人用','隔开如:"xxx,xxxx"
    :param to_addrs: str 实际收件人
    :param cc_show: str 抄送人显示,不起实际作用,多个抄送人用','隔开如:"xxx,xxxx"
    '''
    # 填写真实的发邮件服务器用户名、密码
    user = ''
    password = ''
    # 邮件内容 (所以msg[‘From’]的内容要与发件人保持一致,可能新浪在检查一致性时,判断过于简单,msg[‘From’]的Header不能添加第2个参数”utf-8”,否则检查不能通过)
    # msg = MIMEText(message, 'html', _charset="utf-8")
    msg = MIMEText(message, 'html')
    # 邮件主题描述
    msg["Subject"] = Subject
    # 发件人显示,不起实际作用
    # msg["from"] = sender_show
    msg["from"]=Header(user)
    # 收件人显示,不起实际作用
    msg["to"] = recipient_show
    # 抄送人显示,不起实际作用
    msg["Cc"] = cc_show
    with SMTP_SSL(host="smtp.sina.com", port=465) as smtp:
        # 登录发送邮件服务器
        smtp.login(user=user, password=password)
        # 实际发送、接收邮件配置
        smtp.sendmail(from_addr=user, to_addrs=to_addrs.split(','), msg=msg.as_string())






if __name__ == '__main__':
    print("我们都是中国人!")
    print("*"*100)
    message = '''
    <p>Python 邮件发送测试...</p>
    <p><a href="https://www.cnblogs.com/linbo3168">纵里寻她千百度</a></p>
    '''
    print(message)
    Subject = '主题测试'
    # 显示发送人
    sender_show = '@sian.com'
    # 显示收件人
    recipient_show = '@qq.com'
    # 实际发给的收件人
    to_addrs = '********@qq.com,linbo_yang3168@sina.com'
    sendMail_1(message, Subject, sender_show, recipient_show, to_addrs)
    print("*"*100)

 

posted @ 2022-09-25 21:19  linbo.yang  阅读(126)  评论(0编辑  收藏  举报