send mail

#!/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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值