python學習筆記-day9-1【發送郵件模塊 yagmail】

python的發送郵件使用。python

 

1、python內置的模塊smtplib爲發送郵件模,不太好用。安全

# import smtplib #內置模塊,很差用

 

2、使用 yagmail模塊spa

一、使用pip install yagmail,安裝的模塊時,附件名爲中文的時候會是亂碼。code

二、使用修改過的模塊 .whl的安裝包, 安裝方式爲blog

pip install xxxxx.whlip

 

三、發送郵件的步驟ssl

 一、在你的郵箱設置裏面打開smtp服務
 二、開啓郵箱受權碼,記住這個受權碼

四、發送郵件示例,用qq郵箱服務pip

import yagmail


username = '12345678@qq.com'
passwd = 'wewwwwwwwwwww' #受權碼

mail = yagmail.SMTP(user=username, password=passwd, host='smtp.qq.com', smtp_ssl=True) #host根據不一樣的郵箱不一樣
#若是是qq的郵箱或是大家公司郵箱使用的是安全協議的話,要寫smtp_ssl=True
#port=25或是port=465

mail.send(to=['123456789@qq.com','23456789877@qq.com'],
          cc='6666666666@qq.com', #抄送
          subject='發送郵件,啦啦',  #郵件標題
          contents='你好,今天開心嗎?',#郵件的正文
          attachments=[r'C:\Users\Administrator\Desktop\testsendmail.txt',r'C:\Users\Administrator\Desktop\test1.txt']
          )
# mail.send(to=['11111111@qq.com'], cc=['2222222222@qq.com'])

 

注:若是郵件給多個的人時候,to=[]爲列表,若是爲單個用戶的時候,能夠使用to=指定郵箱class

相關文章
相關標籤/搜索