程序人員對於郵件自動化
的日常需求還是很高的。但是入過了Linux的命令行郵件客戶端如Sendmail
,Mutt
,Alpine
等坑之後,發現現代其實很少人真的在用它們實現郵件自動化,根據搜索引擎裏相關文章的數量就可知一二。取而代之的是,現代都在用Python或PHP等編程語言直接實現。Python更是自帶一套模塊實現郵件發送。
先上示例代碼,之後再詳解。
注:全部代碼在Python3環境下測試通過,正常使用,正常顯示,無需任何外置模塊。
參考:菜鳥教程 - Python SMTP發送郵件
參考:簡單三步,用 Python 發郵件
發送HTML格式的漂亮郵件
import smtplib
from email.mime.text import MIMEText
# Settings of sender's server
host = 'smtp.aliyun.com'
sender = '[email protected]'
user = '[email protected]'
password = input('Please type your password: ')
to = ['[email protected]']
# Content of email
subject = 'Python send html email test'
with open('./test.html', 'r') as f:
content = f.read()
# Settings of the email string
email = MIMEText(content,'html','utf-8')
email['Subject'] = subject
email['From'] = sender
email['To'] = to[0]
msg = email.as_string()
# Login the sender's server
print('Logging with server...')
smtpObj = smtplib.SMTP()
smtpObj.connect(host, 25)
smtpObj.login(user, password)
print('Login successful.')
# Send email
smtpObj.sendmail(sender, to, msg)
smtpObj.quit()
print('Email has been sent')
發送帶附件的郵件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
# Settings of sender's server
host = 'smtp.aliyun.com'
sender = '[email protected]'
user = '[email protected]'
password = input('Please type your password: ')
to = ['[email protected]']
# Make content of email
subject = 'Python send email with attachments'
with open('./test.html', 'r') as f:
content = MIMEText(f.read(),'html','utf-8')
content['Content-Type'] = 'text/html'
print('Loaded content.')
# Make txt attachment
with open('./txt.md', 'r') as f:
txt = MIMEText(f.read(),'plain','utf-8')
txt['Content-Type'] = 'application/octet-stream'
txt['Content-Disposition'] = 'attachment;filename="txt.md"'
print('Loaded txt attachment file.')
# Make image attachment
with open('./pic.png', 'rb') as f:
img = MIMEImage(f.read())
img['Content-Type'] = 'application/octet-stream'
img['Content-Disposition'] = 'attachment;filename="pic.png"'
print('Loaded image attachment file.')
# Attach content & attachments to email
email = MIMEMultipart()
email.attach(content)
email.attach(txt)
email.attach(img)
# Settings of the email string
email['Subject'] = subject
email['From'] = sender
email['To'] = to[0]
msg = email.as_string()
# Login the sender's server
print('Logging with server...')
smtpObj = smtplib.SMTP()
smtpObj.connect(host, 25)
smtpObj.login(user, password)
print('Login successful.')
# Send email
smtpObj.sendmail(sender, to, msg)
smtpObj.quit()
print('Email has been sent')
發送郵件大殺器:Yagmail
之所以放在最後,是相襯托出傳統的發送郵件是多繁瑣多麻煩,實際上我們需要的只是超級簡單的東西。Yagmail正是爲了實現這個而生的,一句話就可以完成所有的登錄、發送文字、HTML、附件等功能。
參考Github:yagmail -- Yet Another GMAIL/SMTP client
一句話發送郵件:
yagmail.SMTP('username').send('[email protected]', 'Subject', 'This is the body')
正常一點的發送郵件:
import yagmail
yag = yagmail.SMTP('user', 'password', host='server.com', port='123')
contents = [
'This is the body, and here is just text http://somedomain/image.png',
'You can find a file attached.',
'./dataset/pic.jpg'
]
yag.send('[email protected]', 'yagmail tst', contents)