SMTP发送邮件——Python实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhchs2012/article/details/80306644

有的时候我们的脚本需要发送个邮件提醒我们事情干的怎么样了,所以需要代码来实现自动发送邮件。
请看:

def email_module(output):
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    mailto_list = [xxxxxxxxx@qq.com]
    msg = MIMEText(output, 'plain', 'utf-8')  #这里填正文
    from_addr = '[email protected]'
    from_pwd = 'xxxxxx'
    port = 25
    smtp_server = 'smtp.sinafenqi.com'
    to_addr = mailto_list
    msg['from'] = Header('牛逼的数据分析组', 'utf-8')
    #msg['to'] = Header('更加牛逼的收件人们', 'utf-8')
    msg['subject'] = Header('上市公司爬虫脚本反馈邮件%s' % round(time.time()), 'utf-8')
    try:
        smtpObj = smtplib.SMTP(smtp_server, port)
        smtpObj.set_debuglevel(1)
        smtpObj.login(from_addr, from_pwd)
        smtpObj.sendmail(from_addr, to_addr, msg.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件", e)

记得替换代码中xxxx的部分就可以啦,应该来说是很好看懂的。

猜你喜欢

转载自blog.csdn.net/zhchs2012/article/details/80306644