我使用的腳本非常相似;我在這裏發佈它作爲如何使用電子郵件。*模塊生成MIME消息的示例;所以這個腳本可以很容易地修改爲附加圖片等。
我依靠我的ISP添加日期時間標題。
我的ISP要求我使用安全SMTP發送郵件,我靠ssmtplib模塊(在http://www1.cs.columbia.edu/~db2501/ssmtplib.py下載)
在你的腳本,用戶名和密碼,(下面給出虛值),用於在SMTP服務器上進行身份驗證,在源文件中以純文本格式。這是一個安全弱點;但最好的選擇取決於你需要多麼謹慎(想要?)來保護這些。
=======================================
#! /usr/local/bin/python
SMTPserver = 'smtp.att.yahoo.com'
sender = '[email protected]_email_domain.net'
destination = ['[email protected]_email_domain.com']
USERNAME = "USER_NAME_FOR_INTERNET_SERVICE_PROVIDER"
PASSWORD = "PASSWORD_INTERNET_SERVICE_PROVIDER"
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content="""\
Test message
"""
subject="Sent from Python"
import sys
import os
import re
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
# old version
# from email.MIMEText import MIMEText
from email.mime.text import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.quit()
except Exception, exc:
sys.exit("mail failed; %s" % str(exc)) # give a error message
確保您獲得日期/時間正確。我發現以下功能非常有用,它爲Date-Header提供了完美格式化的值: http://docs.python.org/py3k/library/email.util.html#email.utils.formatdate – BastiBen 2010-07-30 06:11:21
這裏是一個代碼示例,允許[在主題和/或正文中發送帶有Unicode文本的電子郵件](http://stackoverflow.com/a/20787826/4279) – jfs 2013-12-26 18:10:11
這裏是一個代碼示例,演示如何[發送圖像內聯電子郵件與HTML和純文本部分)](http://stackoverflow.com/a/20485764/4279)。它還展示瞭如何在舊的Python版本上配置ssl參數。 – jfs 2013-12-26 18:11:00