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.\nContent:%s\n' % (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.\nContent:%s\n' % (e.__class__.__name__, e.message)

if __name__ == '__main__':
    send_email(sys.argv[3])        # 邮件内容

 

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

 

posted @ 2017-11-06 09:30  Chrdai  阅读(386)  评论(0编辑  收藏  举报