1. 程式人生 > >python 發送郵件腳本

python 發送郵件腳本

prepare env 郵件 如果 get sre esp pwd targe

一、該腳本適合在 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 = "[email protected]"    # 發件賬號
_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("[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 發送郵件腳本