欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(shí)例為大家分享了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)加入端口后有時(shí)候發(fā)郵件出現(xiàn)延遲,故暫時(shí)取消 
 
 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ā)送,格式(注意英文逗號(hào)): 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"')   # 強(qiáng)制命名,名稱未成功格式化,如果可以解決請聯(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'測試報(bào)告.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)系我,希望此對大家有所幫助,謝謝(已解決,見上面最新版

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

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

相關(guān)文章

  • python2和python3在處理字符串上的區(qū)別詳解

    python2和python3在處理字符串上的區(qū)別詳解

    這篇文章主要介紹了python2和python3在處理字符串上的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法

    Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法

    這篇文章主要介紹了Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫中導(dǎo)出XML文件的方法,涉及Python使用MYSQLDB操作數(shù)據(jù)庫及XML文件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • python一繪制元二次方程曲線的實(shí)例分析

    python一繪制元二次方程曲線的實(shí)例分析

    在本篇文章里小編給大家整理的是一篇關(guān)于python一繪制元二次方程曲線的實(shí)例分析內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-07-07
  • 淺談Python中的函數(shù)(def)及參數(shù)傳遞操作

    淺談Python中的函數(shù)(def)及參數(shù)傳遞操作

    這篇文章主要介紹了淺談Python中的函數(shù)(def)及參數(shù)傳遞操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python DataFrame Rank詳解

    Python DataFrame Rank詳解

    在數(shù)據(jù)分析中,pandas庫的DataFrame對象的rank()方法可用于計(jì)算數(shù)據(jù)排名,處理重復(fù)值并支持多種參數(shù)定制排名規(guī)則,如ascending、axis、numeric_only等,是數(shù)據(jù)分析和競賽中的有力工具
    2024-09-09
  • 淺談Python爬取網(wǎng)頁的編碼處理

    淺談Python爬取網(wǎng)頁的編碼處理

    下面小編就為大家?guī)硪黄獪\談Python爬取網(wǎng)頁的編碼處理。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • Python加載數(shù)據(jù)的5種不同方式(收藏)

    Python加載數(shù)據(jù)的5種不同方式(收藏)

    這篇文章主要介紹了Python加載數(shù)據(jù)的5種不同方式(收藏),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python線程池thread?pool創(chuàng)建使用及實(shí)例代碼分享

    Python線程池thread?pool創(chuàng)建使用及實(shí)例代碼分享

    這篇文章主要介紹了Python線程池(thread?pool)創(chuàng)建使用及實(shí)例代碼分享,文章圍繞主題展開詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06
  • Python高階函數(shù)map()?簡介和使用詳解

    Python高階函數(shù)map()?簡介和使用詳解

    map()?函數(shù)是Python中的內(nèi)置函數(shù),這個(gè)函數(shù)又叫做映射函數(shù),其實(shí)里面具有一個(gè)迭代器的功能,會(huì)依次遍歷可迭代對象進(jìn)行相關(guān)的操作,這篇文章主要介紹了Python高階函數(shù)map()?簡介和使用詳解,需要的朋友可以參考下
    2023-03-03
  • python怎么對數(shù)字進(jìn)行過濾

    python怎么對數(shù)字進(jìn)行過濾

    在本篇內(nèi)容中小編給大家整理的是關(guān)于python怎么對數(shù)字進(jìn)行過濾的實(shí)例方法內(nèi)容,需要的朋友們可以參考下。
    2020-07-07

最新評(píng)論