国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 增城市| 桃江县| 芒康县| 泗水县| 章丘市| 澳门| 垫江县| 台东市| 桂东县| 乌海市| 遂溪县| 堆龙德庆县| 巴彦淖尔市| 乌鲁木齐市| 涡阳县| 祁阳县| 河源市| 崇明县| 浑源县| 来宾市| 蕉岭县| 隆德县| 连城县| 达州市| 麻阳| 青岛市| 浙江省| 肇东市| 客服| 石阡县| 江城| 中超| 凤山县| 泰安市| 绥棱县| 武隆县| 南投县| 翁牛特旗| 潮安县| 永登县| 手机|