亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python自動(dòng)發(fā)郵件庫(kù)yagmail的示例代碼

 更新時(shí)間:2018年02月23日 10:59:31   作者:蟲(chóng)師  
本篇文章主要介紹了python自動(dòng)發(fā)郵件庫(kù)yagmail的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

之前使用的python的smtplib、email模塊發(fā)模塊的一步步驟是:

一、先導(dǎo)入smtplib模塊  導(dǎo)入MIMEText庫(kù)用來(lái)做純文本的郵件模板
二、發(fā)郵件幾個(gè)相關(guān)的參數(shù),每個(gè)郵箱的發(fā)件服務(wù)器不一樣,以126為例子百度搜索服務(wù)器是  smtp.126.com
三、寫(xiě)郵件主題和正文,這里的正文是HTML格式的
四、最后調(diào)用SMTP發(fā)件服務(wù)

一般發(fā)郵件方法

我以前在通過(guò)Python實(shí)現(xiàn)自動(dòng)化郵件功能的時(shí)候是這樣的:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 發(fā)送郵箱服務(wù)器
smtpserver = 'smtp.sina.com'
# 發(fā)送郵箱用戶(hù)/密碼
user = 'username@sina.com'
password = '123456'
# 發(fā)送郵箱
sender = 'username@sina.com'
# 接收郵箱
receiver = 'receive@126.com'
# 發(fā)送郵件主題
subject = 'Python email test'

# 編寫(xiě)HTML類(lèi)型的郵件正文
msg = MIMEText('<html><h1>你好!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')

# 連接發(fā)送郵件
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

其實(shí),這段代碼也并不復(fù)雜,只要你理解使用過(guò)郵箱發(fā)送郵件,那么以下問(wèn)題是你必須要考慮的:

  1. 你登錄的郵箱帳號(hào)/密碼
  2. 對(duì)方的郵箱帳號(hào)
  3. 郵件內(nèi)容(標(biāo)題,正文,附件)
  4. 郵箱服務(wù)器(SMTP.xxx.com/pop3.xxx.com)

yagmail 實(shí)現(xiàn)發(fā)郵件

yagmail 可以更簡(jiǎn)單的來(lái)實(shí)現(xiàn)自動(dòng)發(fā)郵件功能。

github項(xiàng)目地址: https://github.com/kootenpv/yagmail

安裝

pip install yagmail

簡(jiǎn)單例子

import yagmail
#鏈接郵箱服務(wù)器
yag = yagmail.SMTP( user="user@126.com", password="1234", host='smtp.126.com')

# 郵箱正文
contents = ['This is the body, and here is just text http://somedomain/image.png',
      'You can find an audio file attached.', '/local/path/song.mp3']

# 發(fā)送郵件
yag.send('taaa@126.com', 'subject', contents)

總共四行代碼搞定,是不是比上面的例子簡(jiǎn)單太多了。

給多個(gè)用戶(hù)發(fā)送郵件

# 發(fā)送郵件
yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'], 'subject', contents)

只需要將接收郵箱 變成一個(gè)list即可。

發(fā)送帶附件的郵件

# 發(fā)送郵件
yag.send('aaaa@126.com', '發(fā)送附件', contents, ["d://log.txt","d://baidu_img.jpg"])

只需要添加要發(fā)送的附件列表即可。

我都快感動(dòng)哭了,到哪兒去找這么良心庫(kù)去?簡(jiǎn)單的有點(diǎn)不像編程語(yǔ)言!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論