国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

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

2020-02-23 00:11:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

解決之前版本的問(wèn)題,下面為最新版

#!/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ā)送郵件測(cè)試,包含附件'   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()             
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武功县| 淄博市| 盘锦市| 吉安市| 高唐县| 平谷区| 乌鲁木齐市| 平江县| 安达市| 莱阳市| 鄂温| 浦北县| 库尔勒市| 临沭县| 于都县| 盐亭县| 临潭县| 孟连| 盱眙县| 淮滨县| 长岛县| 荣昌县| 涟源市| 柯坪县| 光山县| 育儿| 安仁县| 博野县| 遂平县| 咸阳市| 鄂伦春自治旗| 安义县| 东莞市| 堆龙德庆县| 育儿| 佛坪县| 宁远县| 多伦县| 二手房| 石城县| 绿春县|