程序人員對於郵件自動化
的平常需求仍是很高的。可是入過了Linux的命令行郵件客戶端如Sendmail
,Mutt
,Alpine
等坑以後,發現現代其實不多人真的在用它們實現郵件自動化,根據搜索引擎裏相關文章的數量就可知一二。取而代之的是,現代都在用Python或PHP等編程語言直接實現。Python更是自帶一套模塊實現郵件發送。
先上示例代碼,以後再詳解。html
注:所有代碼在Python3環境下測試經過,正常使用,正常顯示,無需任何外置模塊。python
參考:菜鳥教程 - Python SMTP發送郵件
參考:簡單三步,用 Python 發郵件git
import smtplib from email.mime.text import MIMEText # Settings of sender's server host = 'smtp.aliyun.com' sender = 'Jason@aliyun.com' user = 'Jason@aliyun.com' password = input('Please type your password: ') to = ['Jason@outlook.com'] # 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 = 'Jason@aliyun.com' user = 'Jason@aliyun.com' password = input('Please type your password: ') to = ['Jason@outlook.com'] # 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正是爲了實現這個而生的,一句話就能夠完成全部的登陸、發送文字、HTML、附件等功能。
參考Github:yagmail -- Yet Another GMAIL/SMTP clientgithub
一句話發送郵件:編程
yagmail.SMTP('username').send('to@a.com', 'Subject', 'This is the body')
正常一點的發送郵件:app
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('solomonxie@outlook.com', 'yagmail tst', contents)