最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python smtplib實現(xiàn)發(fā)送郵件功能

 更新時間:2018年05月22日 10:39:14   作者:Johnny  
這篇文章主要為大家詳細(xì)介紹了Python smtplib實現(xiàn)發(fā)送郵件功能,包含文本、附件、圖片等,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Python smtplib發(fā)送郵件功能的具體代碼,供大家參考,具體內(nèi)容如下

解決之前版本的問題,下面為最新版

#!/usr/bin/env python 
# coding:gbk 
 
""" 
FuncName: sendemail.py 
Desc: sendemail with text,image,audio,application... 
Date: 2016-06-20 10:30 
Home: http://blog.csdn.net/z_johnny 
Author: johnny 
""" 
 
from email.mime.multipart import MIMEMultipart 
from email.mime.application import MIMEApplication 
from email.utils import COMMASPACE 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
from email.mime.audio import MIMEAudio 
import ConfigParser 
import smtplib 
import os 
 
class MyEmail: 
 def __init__(self, email_config_path, email_attachment_path): 
  """ 
  init config 
  """ 
  config = ConfigParser.ConfigParser() 
  config.read(email_config_path) 
  self.attachment_path = email_attachment_path 
 
  self.smtp = smtplib.SMTP() 
  self.login_username = config.get('SMTP', 'login_username') 
  self.login_password = config.get('SMTP', 'login_password') 
  self.sender = config.get('SMTP', 'login_username') # same as login_username 
  self.receiver = config.get('SMTP', 'receiver') 
  self.host = config.get('SMTP', 'host') 
  #self.port = config.get('SMTP', 'port')  發(fā)現(xiàn)加入端口后有時候發(fā)郵件出現(xiàn)延遲,故暫時取消 
 
 def connect(self): 
  """ 
  connect server 
  """ 
  #self.smtp.connect(self.host, self.port) 
  self.smtp.connect(self.host) 
 
 def login(self): 
  """ 
  login email 
  """ 
  try: 
   self.smtp.login(self.login_username, self.login_password) 
  except: 
   raise AttributeError('Can not login smtp!!!') 
 
 def send(self, email_title, email_content): 
  """ 
  send email 
  """ 
  msg = MIMEMultipart()     # create MIMEMultipart 
  msg['From'] = self.sender    # sender 
  receiver = self.receiver.split(",")  # split receiver to send more user 
  msg['To'] = COMMASPACE.join(receiver) 
  msg['Subject'] = email_title   # email Subject 
  content = MIMEText(email_content, _charset='gbk') # add email content ,coding is gbk, becasue chinese exist 
  msg.attach(content) 
 
  for attachment_name in os.listdir(self.attachment_path): 
   attachment_file = os.path.join(self.attachment_path,attachment_name) 
 
   with open(attachment_file, 'rb') as attachment: 
    if 'application' == 'text': 
     attachment = MIMEText(attachment.read(), _subtype='octet-stream', _charset='GB2312') 
    elif 'application' == 'image': 
     attachment = MIMEImage(attachment.read(), _subtype='octet-stream') 
    elif 'application' == 'audio': 
     attachment = MIMEAudio(attachment.read(), _subtype='octet-stream') 
    else: 
     attachment = MIMEApplication(attachment.read(), _subtype='octet-stream') 
 
   attachment.add_header('Content-Disposition', 'attachment', filename = ('gbk', '', attachment_name)) 
   # make sure "attachment_name is chinese" right 
   msg.attach(attachment) 
 
  self.smtp.sendmail(self.sender, receiver, msg.as_string()) # format msg.as_string() 
 
 def quit(self): 
  self.smtp.quit() 
 
def send(): 
 import time 
 ISOTIMEFORMAT='_%Y-%m-%d_%A' 
 current_time =str(time.strftime(ISOTIMEFORMAT)) 
 
 email_config_path = './config/emailConfig.ini' # config path 
 email_attachment_path = './result'    # attachment path 
 email_tiltle = 'johnny test'+'%s'%current_time # as johnny test_2016-06-20_Monday ,it can choose only file when add time 
 email_content = 'python發(fā)送郵件測試,包含附件' 
 
 myemail = MyEmail(email_config_path,email_attachment_path) 
 myemail.connect() 
 myemail.login() 
 myemail.send(email_tiltle, email_content) 
 myemail.quit() 
 
if __name__ == "__main__": 
 # from sendemail import SendEmail 
 send() 

配置文件emailConfig.ini

路徑要與程序?qū)?yīng)

;login_username : 登陸發(fā)件人用戶名 
;login_password : 登陸發(fā)件人密碼 
;host:port : 發(fā)件人郵箱對應(yīng)的host和端口,如:smtp.163.com:25 和 smtp.qq.com:465 
;receiver : 收件人,支持多方發(fā)送,格式(注意英文逗號): 123456789@163.com,zxcvbnm@qq.com 

[SMTP] 
login_username = johnny@163.com 
login_password = johnny 
host = smtp.163.com 
port = 25 
receiver = johnny1@qq.com,johnny2@163.com,johnny3@gmail.com 

之前版本出現(xiàn)的問題:

#!/usr/bin/env python 
#coding: utf-8 
 
''''' 
FuncName: smtplib_email.py 
Desc: 使用smtplib發(fā)送郵件 
Date: 2016-04-11 14:00 
Author: johnny 
''' 
 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.base import MIMEBase 
from email.utils import COMMASPACE,formatdate 
from email import encoders 
 
def send_email_text(sender,receiver,host,subject,text,filename): 
  
 assert type(receiver) == list 
 
 msg = MIMEMultipart() 
 msg['From'] = sender 
 msg['To'] = COMMASPACE.join(receiver) 
 msg['Subject'] = subject 
 msg['Date'] = formatdate(localtime=True) 
 msg.attach(MIMEText(text))     #郵件正文內(nèi)容 
 
 for file in filename:      #郵件附件 
  att = MIMEBase('application', 'octet-stream') 
  att.set_payload(open(file, 'rb').read()) 
  encoders.encode_base64(att) 
  if file.endswith('.html'):    # 若不加限定,jpg、html等格式附件是bin格式的 
   att.add_header('Content-Disposition', 'attachment; filename="今日測試結(jié)果.html"')   # 強制命名,名稱未成功格式化,如果可以解決請聯(lián)系我 
  elif file.endswith('.jpg') or file.endswith('.png') : 
   att.add_header('Content-Disposition', 'attachment; filename="pic.jpg"') 
  else: 
   att.add_header('Content-Disposition', 'attachment; filename="%s"' % file) 
  msg.attach(att) 
 
 smtp = smtplib.SMTP(host)   
 smtp.ehlo() 
 smtp.starttls() 
 smtp.ehlo() 
 smtp.login(username,password) 
 smtp.sendmail(sender,receiver, msg.as_string()) 
 smtp.close() 
 
if __name__=="__main__": 
 sender = 'qqq@163.com' 
 receiver = ['www@qq.com'] 
 subject = "測試" 
 text = "johnny'lab test" 
 filename = [u'測試報告.html','123.txt',u'獲取的信息.jpg'] 
 host = 'smtp.163.com' 
 username = 'qqq@163.com' 
 password = 'qqq' 
 send_email_text(sender,receiver,host,subject,text,filename) 

已測試通過,使用Header并沒有變成“頭”,故未使用

若能解決附件格式為(html、jpg、png)在郵箱附件中格式不為“bin”的請聯(lián)系我,希望此對大家有所幫助,謝謝(已解決,見上面最新版

點擊查看:Python 郵件smtplib發(fā)送示例 

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

相關(guān)文章

  • python中如何使用xml.dom.minidom模塊讀取解析xml文件

    python中如何使用xml.dom.minidom模塊讀取解析xml文件

    xml.dom.minidom模塊應(yīng)該是內(nèi)置模塊不用下載安裝,本文給大家介紹python中如何使用xml.dom.minidom模塊讀取解析xml文件,感興趣的朋友一起看看吧
    2023-10-10
  • Python加載文件內(nèi)容的兩種實現(xiàn)方式

    Python加載文件內(nèi)容的兩種實現(xiàn)方式

    這篇文章主要介紹了Python加載文件內(nèi)容的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Python讀取HDFS目錄下的所有文件的實現(xiàn)示例

    Python讀取HDFS目錄下的所有文件的實現(xiàn)示例

    HDFS是Apache Hadoop的分布式文件系統(tǒng),本文主要介紹了Python讀取HDFS目錄下的所有文件的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Python新手教程之while循環(huán)20例

    Python新手教程之while循環(huán)20例

    循環(huán)的作用就是讓指定的代碼重復(fù)的執(zhí)行,while循環(huán)最常用的應(yīng)用場景就是讓執(zhí)行的代碼按照指定的次數(shù)重復(fù)執(zhí)行,這篇文章主要給大家介紹了關(guān)于Python新手教程之while循環(huán)20例的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • Pytes正確的配置使用日志功能

    Pytes正確的配置使用日志功能

    在pytest自動化測試中,如果只是簡單的從應(yīng)用的角度來說,完全可以不去了解pytest中的顯示信息的部分以及原理,可以通過使用推薦的pytest.ini配置,從而可以做到相對來說比較通用的日志配置,這篇文章主要介紹了Pytes如何正確的配置使用日志功能,需要的朋友可以參考下
    2022-12-12
  • 在Django的視圖(View)外使用Session的方法

    在Django的視圖(View)外使用Session的方法

    這篇文章主要介紹了在Django的視圖(View)外使用Session的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下
    2015-07-07
  • pytorch_detach 切斷網(wǎng)絡(luò)反傳方式

    pytorch_detach 切斷網(wǎng)絡(luò)反傳方式

    這篇文章主要介紹了pytorch_detach 切斷網(wǎng)絡(luò)反傳方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • 基于Python的文件類型和字符串詳解

    基于Python的文件類型和字符串詳解

    下面小編就為大家分享一篇基于Python的文件類型和字符串詳解,具有很的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 基于python實現(xiàn)KNN分類算法

    基于python實現(xiàn)KNN分類算法

    這篇文章主要為大家詳細(xì)介紹了基于python實現(xiàn)KNN分類算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • opencv-python 提取sift特征并匹配的實例

    opencv-python 提取sift特征并匹配的實例

    今天小編就為大家分享一篇opencv-python 提取sift特征并匹配的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評論

沽源县| 南乐县| 浮山县| 宣化县| 修水县| 徐闻县| 富蕴县| 新巴尔虎右旗| 瓦房店市| 淳安县| 井冈山市| 大竹县| 星子县| 招远市| 龙口市| 秀山| 鲁山县| 金平| 永春县| 丁青县| 修武县| 子长县| 永丰县| 富宁县| 潼南县| 涞水县| 雅江县| 阜平县| 安平县| 运城市| 碌曲县| 玉门市| 乌什县| 定南县| 昆山市| 宜兴市| 祁门县| 安达市| 门源| 中山市| 安吉县|