关闭

send mail

标签: htmlheaderimportencodingemaillambda
340人阅读 评论(0) 收藏 举报
分类:
#!/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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6205次
    • 积分:167
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档