成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Python 發(fā)送郵件

tuomao / 1145人閱讀

摘要:程序人員對(duì)于郵件自動(dòng)化的日常需求還是很高的。更是自帶一套模塊實(shí)現(xiàn)郵件發(fā)送。正是為了實(shí)現(xiàn)這個(gè)而生的,一句話就可以完成所有的登錄發(fā)送文字附件等功能。參考一句話發(fā)送郵件正常一點(diǎn)的發(fā)送郵件

程序人員對(duì)于郵件自動(dòng)化的日常需求還是很高的。但是入過了Linux的命令行郵件客戶端如Sendmail, Mutt, Alpine等坑之后,發(fā)現(xiàn)現(xiàn)代其實(shí)很少人真的在用它們實(shí)現(xiàn)郵件自動(dòng)化,根據(jù)搜索引擎里相關(guān)文章的數(shù)量就可知一二。取而代之的是,現(xiàn)代都在用Python或PHP等編程語言直接實(shí)現(xiàn)。Python更是自帶一套模塊實(shí)現(xiàn)郵件發(fā)送。

先上示例代碼,之后再詳解。

注:全部代碼在Python3環(huán)境下測(cè)試通過,正常使用,正常顯示,無需任何外置模塊。

參考:菜鳥教程 - Python SMTP發(fā)送郵件
參考:簡(jiǎn)單三步,用 Python 發(fā)郵件

發(fā)送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")
發(fā)送帶附件的郵件
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")
發(fā)送郵件大殺器:Yagmail
之所以放在最后,是相襯托出傳統(tǒng)的發(fā)送郵件是多繁瑣多麻煩,實(shí)際上我們需要的只是超級(jí)簡(jiǎn)單的東西。Yagmail正是為了實(shí)現(xiàn)這個(gè)而生的,一句話就可以完成所有的登錄、發(fā)送文字、HTML、附件等功能。

參考Github:yagmail -- Yet Another GMAIL/SMTP client

一句話發(fā)送郵件:

yagmail.SMTP("username").send("[email protected]", "Subject", "This is the body")

正常一點(diǎn)的發(fā)送郵件:

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)

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/43080.html

相關(guān)文章

  • 簡(jiǎn)單三步,用 Python 發(fā)郵件

    摘要:使用腳本發(fā)送郵件并不復(fù)雜。以下為思路導(dǎo)圖模塊與發(fā)送郵件相關(guān)的模塊是關(guān)于簡(jiǎn)單郵件傳輸協(xié)議的操作模塊,在發(fā)送郵件的過程中起到服務(wù)器之間互相通信的作用。 0. 前言 發(fā)送電子郵件是個(gè)很常見的開發(fā)需求。比如你寫了個(gè)監(jiān)控天氣的腳本,發(fā)現(xiàn)第二天要下雨,或者網(wǎng)站上關(guān)注的某個(gè)商品降價(jià)了,就可以發(fā)個(gè)郵件到郵箱來提醒自己。 使用 Python 腳本發(fā)送郵件并不復(fù)雜。不過由于各家郵件的發(fā)送機(jī)制和安全策略不同...

    haobowd 評(píng)論0 收藏0
  • 利用Python自動(dòng)發(fā)送郵件

    摘要:自動(dòng)發(fā)送郵件我們把報(bào)表做出來以后一般都是需要發(fā)給別人查看,對(duì)于一些每天需要發(fā)的報(bào)表或者是需要一次發(fā)送多份的報(bào)表,這個(gè)時(shí)候可以考慮借助來自動(dòng)發(fā)送郵件。一份郵件的組成下圖是中發(fā)送一份郵件的界面,主要包含發(fā)件人收件人抄送人主題正文附件這幾部分。 ...

    leo108 評(píng)論0 收藏0
  • Python發(fā)送電子郵件

    摘要:是發(fā)送郵件的協(xié)議,內(nèi)置對(duì)的支持模塊和模塊可以發(fā)送純文本郵件郵件以及帶附件的郵件簡(jiǎn)單郵件傳輸協(xié)議,是從源地址到目的地址傳送郵件的規(guī)則,由該協(xié)議控制信件的中轉(zhuǎn)方式的提供了一種很方便的途徑傳遞電子郵件,對(duì)進(jìn)行了簡(jiǎn)單的封裝發(fā)送純文本郵件導(dǎo)入模塊 SMTP是發(fā)送郵件的協(xié)議,Python內(nèi)置對(duì)SMTP的支持(smtplib模塊和email模塊),可以發(fā)送純文本郵件、HTML郵件以及帶附件的郵件 S...

    李世贊 評(píng)論0 收藏0
  • Python 發(fā)送 email 的三種方式

    摘要:本米撲博客先介紹幾個(gè)最簡(jiǎn)單的發(fā)送郵件方式記錄下,像郵件,附件等也是支持的,需要時(shí)查文檔即可。特別注意命令發(fā)送郵件,默認(rèn)用端口號(hào),由于阿里云騰訊云等封禁了端口號(hào),因此本示例需在開通端口機(jī)器上測(cè)試執(zhí)行命令收件結(jié)果 Python發(fā)送email的三種方式,分別為使用登錄郵件服務(wù)器、使用smtp服務(wù)、調(diào)用sendmail命令來發(fā)送三種方法 本文原文自米撲博客:Python 發(fā)送 email 的三...

    kun_jian 評(píng)論0 收藏0
  • python發(fā)送郵件

    摘要:郵箱傳輸協(xié)議簡(jiǎn)單郵件傳輸協(xié)議由源地址到目的地址的傳輸規(guī)則郵箱服務(wù)器默認(rèn)端口生成第三方登錄郵箱的密鑰,這樣從第三方登錄郵箱,不能輸入密碼,只需要輸入第三方密鑰就行需要使用到的庫主要是負(fù)責(zé)發(fā)送郵件,連接郵箱服務(wù)器,登錄郵箱構(gòu)造郵件,郵件顯示的內(nèi) QQ郵箱傳輸協(xié)議 SMTP:簡(jiǎn)單郵件傳輸協(xié)議(由源地址到目的地址的傳輸規(guī)則) smtp.qq.com :QQ郵箱服務(wù)器 默認(rèn)端口:25 生成第三...

    wyk1184 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<