python 發送郵件指令碼

來源:互聯網
上載者:User

標籤:nts   加密   back   rda   /usr   bug   郵件   它的   coding   

一、該指令碼適合在 linux 中做郵件發送測試用,只需要填寫好 發送帳號密碼以及發送人即可,然後使用  python ./filename.py (目前的目錄下)即可。如果發送出錯,會將錯誤詳情拋出來。

#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = ‘Apollo‘import timeimport smtplibfrom 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 timeimport smtplibimport sys                # 使用外部傳參,必須引入 sys 類庫from email.mime.text import MIMEText_user = "[email protected]"    # 發件帳號_pwd  = ""                # 密碼_to   = sys.argv[1]        # 發送人_cc   = sys.argv[4]        # 轉呈人if _cc.strip()==‘1‘:    rcpt = _toelse:    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("[email protected]", 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

 

python 發送郵件指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.