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

    一、该脚本适合在 linux 中做邮件发送测试用,只需要填写好 发送账号密码以及发送人即可,然后使用  python ./filename.py (当前目录下)即可。如果发送出错,会将错误详情抛出来。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    __author__ = 'Apollo'
    
    import time
    import smtplib
    from email.mime.text import MIMEText
    _user = ""        # 发送账号
    _pwd  = ""        # 账号密码
    _to   = ""        # 发送人
    
    def send_email(content):
        text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    
        try:
            msg = MIMEText(content)
            msg["Subject"] = text
            msg["From"]    = _user
            msg["To"]      = _to
    
            #s = smtplib.SMTP("smtp.dc.mydorma.com", timeout=30)        # 使用 25 号端口(普通邮件发送)
            s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)    # 使用 465 号端口(SSL加密发送)
            s.set_debuglevel(1)
            s.login(_user, _pwd)
            s.sendmail(_user, _to, msg.as_string())
            s.quit()
        except (smtplib.SMTPAuthenticationError,
                smtplib.SMTPConnectError,
                smtplib.SMTPDataError,
                smtplib.SMTPException,
                smtplib.SMTPHeloError,
                smtplib.SMTPRecipientsRefused,
                smtplib.SMTPResponseException,
                smtplib.SMTPSenderRefused,
                smtplib.SMTPServerDisconnected) as e:
            print 'Warning: %s was caught while trying to send email.
    Content:%s
    ' % (e.__class__.__name__, e.message)
    
    if __name__ == '__main__':
        send_email("Prepare to work:")    # 邮件内容

    二、该脚本适合使用其它语言(例如PHP)外部执行改 python 脚本来实际发送电子邮件,需要填写好 发送账号和密码即可,其它的参数从 外部传进来,例如php这样调用:

    exec("/data/programdir/filename.py $to $subject $content $cc",$out,$result)

    $result == 0 则发送成功$result == 1 则发送失败

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    __author__ = 'Apollo'
    
    import time
    import smtplib
    import sys                # 使用外部传参,必须引入 sys 类库
    from email.mime.text import MIMEText
    _user = "xxxxx@xx.com"    # 发件账号
    _pwd  = ""                # 密码
    _to   = sys.argv[1]        # 发送人
    _cc   = sys.argv[4]        # 转呈人
    
    if _cc.strip()=='1':
        rcpt = _to
    else:
        rcpt = [_to] + _cc.split(",")
    
    def send_email(content):
        text = '[%s] Reporting:' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    
        try:
            msg = MIMEText(content)
            msg["Subject"] = sys.argv[2]
            msg["From"]    = _user
            msg["To"]      = _to
            msg["Cc"]      = _cc
    
    
            s = smtplib.SMTP("xxxx@xx.com", timeout=30)
            #s = smtplib.SMTP_SSL(host='smtp.qq.com', port=465)
            s.set_debuglevel(1)
            #s.login(_user, _pwd)    # 当不需要做身份认证的时候,可以屏蔽该行
            s.sendmail(_user,rcpt, msg.as_string())
            s.quit()
        except (smtplib.SMTPAuthenticationError,
                smtplib.SMTPConnectError,
                smtplib.SMTPDataError,
                smtplib.SMTPException,
                smtplib.SMTPHeloError,
                smtplib.SMTPRecipientsRefused,
                smtplib.SMTPResponseException,
                smtplib.SMTPSenderRefused,
                smtplib.SMTPServerDisconnected) as e:
            print 'Warning: %s was caught while trying to send email.
    Content:%s
    ' % (e.__class__.__name__, e.message)
    
    if __name__ == '__main__':
        send_email(sys.argv[3])        # 邮件内容

    如有转载,请注明出处:http://www.cnblogs.com/chrdai/p/7791693.html

  • 相关阅读:
    Codeforces Round #562题解
    Codeforces Round #561题解
    CF1107E Vasya and Binary String(区间dp)
    NC110113 Summer Earnings(bitset)
    NC112798 XOR-pyramid(dp)
    NC23051 华华和月月种树(离线+树状数组)
    py.path模块
    stat模块
    pwd模块
    PrettyTable模块
  • 原文地址:https://www.cnblogs.com/chrdai/p/7791693.html
Copyright © 2011-2022 走看看