zoukankan      html  css  js  c++  java
  • python 发邮件

    # coding:utf-8   #强制使用utf-8编码格式
    import smtplib # 加载smtplib模块
    from email.mime.text import MIMEText
    from email.utils import formataddr
    my_sender = 'xxxxx@xxxxxx' # 发送者邮箱
    my_user = 'xxx@xxxxx' # 接受者邮箱


    def mail(error_script, error_message):
    ret = True
    try:
    msg = MIMEText(error_message, 'plain', 'utf-8')
    msg['From'] = formataddr(["脚本运行监测", my_sender])
    msg['To'] = formataddr(["监测者", my_user])
    msg['Subject'] = "%s脚本异常" % error_script

    server = smtplib.SMTP("smtp.163.com", 25)
    server.login(my_sender, "xxxxxxxxx")
    server.sendmail(my_sender, [my_user, ], msg.as_string())
    server.quit()
    except Exception as e:
    ret = False
    print e
    return ret


    error_script = "docter_record.py"
    error_message = "今天天气很好"
    res = mail(error_script, error_message)
    if res:
    print("ok") # 如果发送成功则会返回ok,稍等20秒左右就可以收到邮件
    else:
    print("filed") # 如果发送失败则会返回filed
  • 相关阅读:
    shell eval命令
    嘟嘟嘟
    07 linkextractor的基本用法
    rabbitmq消息队列
    5. 哨兵集群
    4.主从同步
    3. redis持久化存储
    2. redis 安全
    1.redis基础
    06. scrapy的Request对象
  • 原文地址:https://www.cnblogs.com/yuzhaoblog/p/9466245.html
Copyright © 2011-2022 走看看