[花果山水帘洞] python3学习笔记(4)——smtp发送邮件

继续python。

python3学习笔记(4)——smtp发送邮件

2016-10-24 / vc12345679

基本都是看manual得到的,就不细说了,直接上代码

实现功能:从 DDD@DDD.com 向 AAA@AAA.com BBB@BBB.com CCC@CCC.com 发送指定 subject 的邮件,邮件内容由参数传递。

import smtplib
from email.mime.text import MIMEText

def send_mail(content):
    mailto_list = ['AAA@AAA.com', 'BBB@BBB.com', 'CCC@CCC.com']
    mail_host = ""smtp.DDD.com""
    mail_user = ""uuuuuuuuuuuu""
    mail_pass = ""xxxxxxxxxxxx""

    me = ""My Name <DDD@DDD.com> ""
    msg = MIMEText(content, _subtype='plain', _charset='utf-8')
    msg['Subject'] = '指定的邮件主题'
    msg['From'] = me
    msg['To'] = "";"".join(mailto_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user, mail_pass)
        server.sendmail(me, mailto_list, msg.as_string())
        server.close()
        return True
    except Exception as e:
        print(str(e))
        return False