欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python发送Email方法实例

时间:2021-01-05 13:47:07|栏目:Python代码|点击:

本文以实例形式展示了Python发送Email功能的实现方法,有不错的实用价值的技巧,且功能较为完善。具体实现方法如下:

主要功能代码如下:

#/usr/bin/env python
# -*- encoding=utf-8 -*-

import base64
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class CCSendMail:
  def __init__(self,host="your.mailhost.com",username='fromuser@xxx.com',password='passwd'):
    self.__smtp=smtplib.SMTP(host)
    self.__subject=None
    self.__content=None
    self.__from=None
    self.__to=[]
    self.__style='html'
    self.__charset='gb2312'
    self.username = username
    self.password = password
    self.fromAlias='fromuser' #发件人别名
    self.from2=''
    
  def close(self):
    try:
      self.__smtp.quit()
    except Exception ,e:
      pass  
  def setFromAlias(self,alias):
    self.fromAlias=alias
  def setStyle(self,style):
    self.__style = style
  def setFrom2(self,from2):
    self.from2=from2
    
  def setSubject(self,subject):
    self.__subject=subject
    
  def setContent(self,content):
    self.__content=content
    
  def setFrom(self,address):
    self.__from=address
    
  def addTo(self,address):
    self.__to.append(address)
    
  def setCharset(self,charset):
    self.__charset=charset
    
  def send(self):
    try:
      self.__smtp.set_debuglevel(1)
      
      #login if necessary
      if self.username and self.password:
        self.__smtp.login(self.username,self.password)
        
      msgRoot = MIMEMultipart('related')
      msgRoot['Subject'] = self.__subject
      aliasB6=base64.encodestring(self.fromAlias.encode(self.__charset))
      if len(self.from2)==0:
        msgRoot['From'] = "=?%s?B?%s?=%s"%(self.__charset,aliasB6.strip(),self.__from)
      else:
        msgRoot['From'] = "%s"%(self.from2)
      msgRoot['To'] = ";".join(self.__to)
      
      msgAlternative = MIMEMultipart('alternative')
      msgRoot.attach(msgAlternative)
      
      msgText = MIMEText(self.__content, self.__style,self.__charset)
      msgAlternative.attach(msgText)

      self.__smtp.sendmail(self.__from,self.__to,msgRoot.as_string())
      return True
    except Exception,e:
      print "Error ",e
      return False
    
  def clearRecipient(self):
    self.__to = []
  
  #给单个人发送邮件
  def sendHtml(self,address,title,content):
    self.setStyle('html')
    self.setFrom("<%s>"%self.username)
    if not isinstance(content,str):
      content = content.encode('gb18030')
    self.addTo(address)
    self.setSubject(title)
    self.setContent(content)
    ret = self.send()
    self.close()
    return ret
  
  #群发邮件
  def sendMoreHtml(self,addressList,title,content):
    self.setStyle('html')
    self.setFrom("<%s>"%self.username)
    if not isinstance(content,str):
      content = content.encode('gb18030')
    for address in addressList:
      self.addTo(address)
    self.setSubject(title)
    self.setContent(content)
    ret = self.send()
    self.close()
    return ret

#测试
def main():
  send=CCSendMail()
  send.sendHtml('touser@xxx.com',u'邮件标题',u'邮件内容')
  #send.sendMoreHtml([touser1@xx.com,touser2@xx.com],u'邮件标题',u'邮件内容')
 
if __name__=='__main__':
  main()

希望本文所述实例对大家的Python程序设计有一定的帮助。

上一篇:Python集合操作方法详解

栏    目:Python代码

下一篇:python socket网络编程步骤详解(socket套接字使用)

本文标题:Python发送Email方法实例

本文地址:http://www.codeinn.net/misctech/40904.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有