`
ghh0000
  • 浏览: 23267 次
  • 来自: ...
社区版块
存档分类
最新评论

python发送邮件

阅读更多
今天在开源中国发现的,测试了下可以使用,保存一份,以后可能会有用到。


#coding=utf-8 

import smtplib 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email.mime.application import MIMEApplication 

class BaseMail: 
    def __init__( self, smtp,bSmtpAuth, sender, pwd ): 
        self.bSmtpAuth = bSmtpAuth 
        self.smtp   = smtp 
        self.sender = sender 
        self.pwd    = pwd 

    def _parserSend(self, sSubject, sContent, lsPlugin): 
        return sSubject, sContent, lsPlugin   
     
    def send ( self, sSubject, sContent, lsTo, lsCc = [], lsPlugin = [] ): 
        mit = MIMEMultipart() 
        mit['from'] = self.sender 
        mit['to'] = ','.join( lsTo ) 
        if lsCc: mit['cc'] = ','.join( lsCc ) 

        codeSubject, codeContent, codePlugin = self._parserSend(sSubject, sContent, lsPlugin) 
        mit.attach( MIMEText( codeContent, 'html', 'utf-8' ) ) 
        mit['subject'] = codeSubject 
        for plugin in codePlugin: 
            mitFile = MIMEApplication( plugin['content'], ) 
            mitFile.add_header( 'content-disposition', 'attachment', filename=plugin['subject'] ) 
            mit.attach( mitFile ) 
             
        server = smtplib.SMTP( self.smtp ) 
        server.set_debuglevel(smtplib.SMTP.debuglevel) 
        if self.bSmtpAuth: server.docmd( "EHLO server" ) 
        server.starttls() 
        server.login( self.sender, self.pwd ) 
        server.sendmail( self.sender, lsTo , mit.as_string() ) 
        server.close() 

class GMail( BaseMail ): 
    def __init__( self, sender, pwd ): 
        BaseMail.__init__( self, 'smtp.gmail.com', True, sender, pwd ) 
        self.__strcode = 'utf-8' 
         
    def _parserSend(self, sSubject, sContent, lsPlugin): 
        for i in lsPlugin: 
            i['subject'] = i['subject'].encode(self.__strcode)          
        return sSubject.encode(self.__strcode), sContent.encode(self.__strcode), lsPlugin 

class Com63Mail( BaseMail ): 
    def __init__( self, sender, pwd ): 
        BaseMail.__init__( self, 'smtp.163.com', False, sender, pwd ) 
        self.__strcode = 'utf-8' 
         
    def _parserSend(self, sSubject, sContent, lsPlugin): 
        for i in lsPlugin: 
            i['subject'] =  i['subject'].encode('gbk') 
        return sSubject, sContent.encode(self.__strcode), lsPlugin 
     

if __name__=="__main__" : 
         
    sSubject = u'python3000邮件发送测试' 
    sContent = u'<font color="#FF0066">热门评论</color>' 
    lsPlugin = [{'subject' : u'附1abc.txt', 'content' : u'内容1abc'}, {'subject' : u'附2abc.txt', 'content' : u'内容2abc'}] 
    com163 = Com63Mail( 'xxxx@163.com' , 'xxxx' ) 
    lsTo = ['xxxx@163.com'] 
    lsCc = [] 
    com163.send(sSubject, sContent, lsTo, lsCc, lsPlugin) 
    print 'com163 send'  

分享到:
评论

相关推荐

    Python发邮件代码

    Python发邮件代码

    Python发邮件源码

    这里我们关注的主题是“Python发邮件源码”,这通常涉及到使用SMTP(Simple Mail Transfer Protocol)来实现邮件的发送。下面我们将深入探讨这个话题,并提供一个实际的Python代码示例。 首先,你需要了解Python中...

    python发邮件详细实例

    详细展示了各种使用python发邮件的程序实例

    python发送邮件脚本

    python发送邮件代码,服务器配置请自行修改,脚本中的地址是内网搭建的邮件服务器。可以结合系统定时任务配置邮件定时发送。希望帮到你

    python 发送邮件简单示例

    简单的python发送邮件的示例,基于python2.7

    python发邮件程序

    python发邮件程序,发送方法:./mail.py 收件人 主题 html内容 图片文件 附件文件'

    python发送邮件的脚本

    半天时间写了个python发送邮件的脚本 作者: jeffery ( email:dungeonsnd@126.com, msn:dungeonsnd@hotmail.com, csdn blog:http://blog.csdn.net/dungeonsnd) 时间: 2011-06-19 地点: SH --------------------------...

    python_发送邮件.docx

    Python发送邮件是编程中常见的需求,特别是在自动化任务和通知系统中。本文档主要介绍了两种使用Python发送邮件的方法,涉及到了smtplib和email模块。 首先,smtplib是Python的标准库,它提供了一种与SMTP(简单...

    基于Python实现多进程的发送邮件.zip

    本课程设计的目标是利用Python实现多进程发送邮件的功能。 首先,我们需要了解Python中的`multiprocessing`模块,它是Python提供的一个标准库,用于实现多进程。通过创建子进程,每个进程可以独立地执行任务,互不...

    使用python通过qq邮箱代理发送邮件

    python发送邮件,通过qq邮箱代理,具体怎么设置请百度(获取qq邮箱授权码),可以发送邮件到邮箱,非常简洁的代码

    python3发送邮件

    使用python3发送QQ电子邮件,发送人为自己,接收人可以一个可以多个。

    python发送邮件(smtplib).docx

    在Python编程中,发送邮件是一项常见的任务,尤其在自动化测试或通知系统中。要实现这一功能,我们可以利用Python内置的`smtplib`和`email`模块。`smtplib`负责处理邮件的发送过程,而`email`则用于构建邮件内容。在...

    Python-Python实现自动发邮件支持HTML富文本

    在Python编程语言中,发送邮件是一项常见的任务,尤其在自动化脚本或系统通知中非常有用。Python通过内置的`smtplib`库提供了对简单邮件传输...解压后,通过阅读和理解代码,可以更好地掌握Python发送邮件的实践技巧。

    python发送邮件源码

    python发送邮件源码有注释

    python发邮件

    使用python发送邮件,使用的是smtplib库

    【Python发送邮件】源码,支持群发和添加附件.py

    这是python发送邮件的源码,支持群发和添加邮件, 文章中有免费下载地址,这里是土豪专用下载地址,

    使用Python发送邮件附件以定时备份MySQL的教程

    标题中的“使用Python发送邮件附件以定时备份MySQL的教程”是指使用Python编程语言编写脚本来自动备份MySQL数据库,并将备份文件作为邮件附件发送出去。这个过程通常涉及到几个关键步骤:数据库备份、文件处理以及...

    Python-python发送邮件报表

    subject = 'Python发送的报表' body = '这是使用Python自动发送的报表邮件。' msg = MIMEMultipart() msg['From'] = sender msg['To'] = receiver msg['Subject'] = subject ``` 若邮件内容包含纯文本和HTML两种...

    python发送邮件的脚本 v0.3

    python发送邮件的脚本 作者: jeffery ( email:dungeonsnd@126.com, msn:dungeonsnd@hotmail.com, csdn blog:http://blog.csdn.net/dungeonsnd) 时间: 2011-07-10 地点: GZ ----------------------------------------...

    Python实现SMTP发送邮件详细教程

    以下是一个基本的Python发送邮件的示例代码: ```python import smtplib from email.mime.text import MIMEText from email.utils import formataddr my_sender = '发件人邮箱账号' my_user = '收件人邮箱账号' ...

Global site tag (gtag.js) - Google Analytics