欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 邮件发送模板

程序员文章站 2023-09-29 17:01:17
文本内容: #!/usr/bin/env python # -*- coding=utf-8 -*- import smtplib from email.mime....

文本内容:

#!/usr/bin/env python
# -*- coding=utf-8 -*-

import smtplib
from email.mime.text import MIMEText

def send_mail(to_list,sub,content):
    mail_host = "***"  # 设置服务器
    mail_user = "***"  # 用户名
    mail_pass = "***"  # 口令
    mail_postfix = "***"  # 发件箱的后缀
    mail_port = ***  # 端口

    sender="Alerter"+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content,_subtype='plain',_charset='utf-8')
    msg['Subject'] = sub
    msg['From'] = sender
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host, mail_port)
        server.login(mail_user, mail_pass)
        server.sendmail(sender, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False

if __name__ == '__main__':
    subject='text_test'
    send_content = ''
    mailto_list=['***@163.com',...]

    send_mail(mailto_list, subject, send_content)

html 内容:

def send_mail(to_list,sub,content):
    mail_host = "***"  # 设置服务器
    mail_user = "***"  # 用户名
    mail_pass = "***"  # 口令
    mail_postfix = "***"  # 发件箱的后缀
    mail_port = ***  # 端口

    sender="Alerter"+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content,_subtype='html',_charset='utf-8')
    msg['Subject'] = sub
    msg['From'] = sender
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host, mail_port)
        server.login(mail_user, mail_pass)
        server.sendmail(sender, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False