send mail

原创 2012年03月27日 14:19:22
#!/usr/bin/env python
# -*- coding: cp936 -*-


#-------------------------------------------------------------
#author: Jack_wind
#purpose: The summary of three ways of send mails
#time: 13th Dec 2011
#-------------------------------------------------------------


#-------------------------------------------------------------
#SMTP.set_debuglevel(level)  default: False
#SMTP.docmd(cdm[,argstring]) send command to the server
#-------------------------------------------------------------


import smtplib, time


class SmtpWind:
    def __init__(self, fromaddr, toaddrs):
        self._fromaddr = fromaddr
        self._toaddrs = toaddrs
        self._label = 'potential'
        self._subject = '['+self._label+'] automatic send test'
        self._content = ''
        self._html_header = ''
        self._html_footer = ''
        self._time = ''
        self._issend = True
        self._smtp = 0
        
    def send_by_general(self):
        if((not self._fromaddr) and (not self._toaddrs)):
            print('The address of the send and the receive is empty')
        
        self._label = 'general'
        self._subject = '['+self._label+'] automatic send test'
        self._content = ('Subject: %s\r\nFrom: %s\r\nTo: %s\r\n'
            % (self._subject, self._fromaddr, ', '.join(self._toaddrs)))
        
        self._time = time.strftime("%H:%M:%S", time.localtime())
        self._content += "Hi, how's it going!\n\n"
        self._content += self._time
        
        for toaddr in self._toaddrs:
            self._smtp = smtplib.SMTP()
            self._smtp.connect("smtp.ea-angel.com", "25")
            self._smtp.login('wanglf@ea-angel.com', 'w359f953l359')
            self._smtp.sendmail(self._fromaddr, toaddr, self._content)
            self._smtp.quit()
        print('send mail sucess')
        
    def send_by_html(self):
        if((not self._fromaddr) and (not self._toaddrs)):
            print('The address of the send and the receive is empty')
            
        self._label = 'html'
        self._subject = '['+self._label+'] automatic send test'
        self._content = ('Subject: %s\r\nFrom: %s\r\nTo: %s\r\nContent-Type: text/html; charset=ISO-8859-1\r\n\r\n'
            % (self._subject, self._fromaddr, ', '.join(self._toaddrs)))
        
        self._html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
        <title></title>
        </head>
        <body>
        """
        
        self._html_footer = """</body>
        </html>
        """
        
        self._content += "details of executed test: <br>"
        self._content += self._html_header
        s1 = "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\" width=\"800\"><tbody>"
        s2 = "<tr><td valign=\"top\">" + str(1) + "<br></td>"
        s2 += "<td bgcolor=\"#33ff33\" valign=\"top\">OK<br></td>"
        s2 += "<td valign=\"top\">my pleasure<br></td></tr>"
        s1 += s2 + '\n'
        s1 += "</tbody></table>"
        s1 += "<br>Best Wishes"
        self._content += s1
        self._content += self._html_footer
        
        self._time = time.strftime("%H:%M:%S", time.localtime())
        self._content += self._time
        
        for toaddr in self._toaddrs:
            self._smtp = smtplib.SMTP("smtp.ea-angel.com", "25")
            self._smtp.login('wanglf@ea-angel.com', 'w359f953l359')
            self._smtp.sendmail(self._fromaddr, toaddr, self._content)
            self._smtp.quit()
        print('send mail sucess')
        
    def send_by_email(self):
        import mimetypes
        from email.mime.text import MIMEText
        from email.mime.multipart import MIMEMultipart
        from email.mime.image import MIMEImage
        
        self._label = 'email'
        self._subject = '['+self._label+'] automatic send test'
            
        content = MIMEMultipart()
        content['From'] = self._fromaddr
        content['To'] = ', '.join(self._toaddrs)
        content['Subject'] = self._subject
        
        self._time = time.strftime("%H:%M:%S", time.localtime())
        text = MIMEText("Hi, how's it going!\n\n%s" % self._time)
        content.attach(text)
        
        filename = r'e:/test.py'
        ctype, encoding = mimetypes.guess_type(filename)
        if ctype is None or encoding is not None:
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)
        att = MIMEImage((lambda f: (f.read(), f.close()))(open(filename, 'rb'))[0], _subtype=subtype)
        att.add_header('Content-Disposition', 'attachment', filename=filename.split('/')[1])
        content.attach(att)
        
        for toaddr in self._toaddrs:
            self._smtp = smtplib.SMTP("smtp.ea-angel.com", "25")
            self._smtp.login('wanglf@ea-angel.com', 'w359f953l359')
            self._smtp.sendmail(self._fromaddr, toaddr, content.as_string())
            self._smtp.quit()
        print('send mail sucess')
        
        
        
        
if __name__ == "__main__":
    fromaddr = 'wanglf@ea-angel.com'
    toaddrs = []
    toaddrs.append('wanglf@ea-angel.com')
    #toaddrs.append('wanglf359@sina.com')
    
    #smtp_general = SmtpWind(fromaddr, toaddrs)
    #smtp_general.send_by_general()
    
    #smtp_html = SmtpWind(fromaddr, toaddrs)
    #smtp_html.send_by_html()
    
    smtp_email = SmtpWind(fromaddr, toaddrs)
    smtp_email.send_by_email()

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Send Mail2000(绝对源代码)

  • 2008-04-17 19:26
  • 601KB
  • 下载

C# Send Mail

How to Send an Email Using UTL_SMTP with Authenticated Mail Server

In this Document   Goal   Solution   References ...

官方VB.NET的例子 send E-mail

  • 2012-09-10 18:25
  • 147KB
  • 下载

spring send mail

  • 2013-07-02 11:39
  • 2.97MB
  • 下载

Send mail----PHP

mail (PHP 4, PHP 5) mail — Send mail Report a bug Description bool mail ( string $to , string ...

Send Mail实例类

VCL mail(send/recieve)

  • 2006-02-23 09:05
  • 269KB
  • 下载

IBM supervessel power云平台 之 send mail 篇

朝弟,咱们今天有个新的任务,在你的机器上运行一个测试实例,然后将结果通过mail发送到我的邮箱,方便后期查阅!先通过文本模式发送,后期改为html格式的,增加美观。

java send mail

  • 2014-06-25 11:20
  • 509KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)