這篇文章帶給大家的內容是關於php如何使用SwooleTaskWorker實現非同步操作Mysql(程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
Python發送email的三種方式,分別為使用登入郵件伺服器、使用smtp服務、呼叫sendmail指令來傳送三種方法
Python傳送email比較簡單,可以透過登入郵件服務來發送,linux下也可以使用呼叫sendmail指令來傳送,也可以使用本地或是遠端的smtp服務來傳送郵件,不管是單個,群發,還是抄送都比較容易實作。本米撲博客先介紹幾個最簡單的發送郵件方式記錄下,像html郵件,附件等也是支援的,需要時查文檔即可。
一、登入郵件伺服器
透過smtp登入第三方smtp郵件信箱傳送郵件,支援25 與465連接埠
vim python_email_1.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 import smtplib from email.mime.text import MIMEText smtpHost = 'smtp.exmail.qq.com' sender = 'robot@mimvp.com' password = "mimvp-password" receiver = 'yanggang@mimvp.com' content = 'hello mimvp.com' msg = MIMEText(content) msg['Subject'] = 'email-subject' msg['From'] = sender msg['To'] = receiver ## smtp port 25 smtpServer = smtplib.SMTP(smtpHost, 25) # SMTP smtpServer.login(sender, password) smtpServer.sendmail(sender, receiver, msg.as_string()) smtpServer.quit() print 'send success by port 25' ## smtp ssl port 465 smtpServer = smtplib.SMTP_SSL(smtpHost, 465) # SMTP_SSL smtpServer.login(sender, password) smtpServer.sendmail(sender, receiver, msg.as_string()) smtpServer.quit() print 'send success by port 465'
執行指令:
$ python python_email_1.py send success by port 25 send success by port 465
傳送結果,會收到兩封郵件,截圖其中一份郵件如下圖:
二、使用smtp服務
測試失敗,略過或留言指正
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 import smtplib from email.mime.text import MIMEText import subprocess smtpHost = 'smtp.exmail.qq.com' sender = 'robot@mimvp.com' password = "mimvp-password" receiver = 'yanggang@mimvp.com' content = 'hello mimvp.com' msg = MIMEText(content) if __name__ == "__main__": p = subprocess.Popen(['/usr/sbin/sendmail', '-t'], stdout=subprocess.PIPE) print(str(p.communicate())) p_res = str(p.communicate()[0]) msg = MIMEText(p_res) msg["From"] = sender msg["To"] = receiver msg["Subject"] = "hello mimvp.com" s = smtplib.SMTP(smtpHost) s.login(sender, password) s.sendmail(sender, receiver, msg.as_string()) s.quit() print 'send success'
三、呼叫sendmail指令
#呼叫本機linux自身sendmail服務發送郵件,不需要啟動sendmail後台進程,不需要發送者登錄,郵件發送者可以是任意名字,沒有限制。
特別注意:sendmail 指令發送郵件,預設用25埠號,由於阿里雲、騰訊雲等封鎖了25埠號,因此本範例需在開通25埠機器上測試
# vim python_email_3.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # # author: mimvp.com # 2015.10.05 from email.mime.text import MIMEText from subprocess import Popen, PIPE import commands import sys reload(sys) sys.setdefaultencoding('utf-8') def send_mail(sender, recevier, subject, html_content): msg = MIMEText(html_content, 'html', 'utf-8') msg["From"] = sender msg["To"] = recevier msg["Subject"] = subject p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE) p.communicate(msg.as_string()) sender = 'robot@mimvp.com' recevier = 'yanggang@mimvp.com' subject = 'sendmail-subject' html_content = 'hello mimvp.com' send_mail(sender, recevier, subject, html_content)
執行指令:
python python_email_3.py
收件結果:
##
以上是Python如何傳送? python發送email的三種方式介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!