国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解Python發送email的三種方式

2020-01-04 14:18:52
字體:
來源:轉載
供稿:網友

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 25smtpServer = smtplib.SMTP(smtpHost, 25)     # SMTPsmtpServer.login(sender, password) smtpServer.sendmail(sender, receiver, msg.as_string()) smtpServer.quit() print 'send success by port 25'  ## smtp ssl port 465smtpServer = smtplib.SMTP_SSL(smtpHost, 465)  # SMTP_SSLsmtpServer.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 25send success by port 465

發送結果,會收到兩封郵件,截圖其中一份郵件如下圖:

Python,email

二、使用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 MIMETextfrom subprocess import Popen, PIPEimport 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,email

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望都县| 上虞市| 望城县| 甘肃省| 沁阳市| 尉犁县| 乌拉特前旗| 秭归县| 东乌珠穆沁旗| 襄城县| 蓝山县| 承德县| 龙门县| 衡东县| 张家口市| 平江县| 昭觉县| 凤凰县| 梁河县| 榆林市| 两当县| 聂拉木县| 翁牛特旗| 温泉县| 永济市| 兴国县| 册亨县| 蒲城县| 闻喜县| 沂水县| 平顺县| 故城县| 香港| 滕州市| 万荣县| 苍南县| 厦门市| 怀来县| 莱西市| 万荣县| 曲水县|