Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / controller / reporters / mail / mail.py
1 import smtplib
2 import logging
3 import os
4 from email.mime.application import MIMEApplication
5 from email.mime.text import MIMEText
6 from email.mime.multipart import MIMEMultipart
7
8 LOG = logging.getLogger(__name__)
9 SRV = 'localhost'
10 USER = None
11 PASSWD = None
12
13
14 class Mail(object):
15     def __init__(self, srv=SRV, user=USER, passwd=PASSWD):
16         self.srv = srv
17         self.user = USER
18         self.passwd = PASSWD
19         self._msg = MIMEMultipart('mixed')
20
21         # addr type
22         self.TO = "To"
23         self.FROM = "From"
24         self.CC = "Cc"
25         self.BCC = "Bcc"
26         self.__addr_choice = [self.TO, self.FROM, self.CC, self.BCC]
27
28         # text mode
29         self.HTML = "html"
30         self.PLAIN = "plain"
31         self.__mode = [self.HTML, self.PLAIN]
32         # self._charset = 'gb2312'
33
34         # timeout
35         self.timeout = 10
36
37     def attach_addr(self, addr, addr_type):
38         """
39         :param addr: a list of email address.
40         :param addr_type: must be one of [to, from, cc, bcc]
41         """
42         if not addr or not isinstance(addr, list):
43             LOG.error("The addr must be a list")
44             return False
45
46         if addr_type not in self.__addr_choice:
47             LOG.error("Not support addr type")
48             return False
49
50         if not self._msg[addr_type]:
51             self._msg[addr_type] = ','.join(addr)
52         self._msg[addr_type].join(addr)
53
54     def attach_title(self, title):
55         """Notice:
56         each time attach title, the old title will be covered.
57         """
58         if title:
59             self._msg["Subject"] = str(title)
60
61     def attach_text(self, text, mode):
62         if mode not in self.__mode:
63             LOG.error("The text mode not support.")
64             return False
65
66         msg_alternative = MIMEMultipart('alternative')
67         msg_text = MIMEText(text, mode)
68         msg_alternative.attach(msg_text)
69
70         return self._msg.attach(msg_alternative)
71
72     def attach_files(self, files):
73         for _file in files:
74             part = MIMEApplication(open(_file, "rb").read())
75             part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(_file))
76             self._msg.attach(part)
77
78     def send(self):
79         server = smtplib.SMTP(self.srv, timeout=self.timeout)
80         if self.user:
81             server.ehlo()
82             server.starttls()
83             server.ehlo()
84             server.login(self.user, self.passwd)
85         maillist = []
86         if self._msg[self.TO]:
87             maillist += self._msg[self.TO].split(',')
88         if self._msg[self.CC]:
89             maillist += self._msg[self.CC].split(',')
90         if self._msg[self.BCC]:
91             maillist += self._msg[self.BCC].split(',')
92         ret = server.sendmail(self._msg[self.FROM].split(','),
93                               maillist, self._msg.as_string())
94         LOG.info("send mail ret:%s", ret)
95         server.close()
96
97
98 if __name__ == "__main__":
99     m = Mail()
100     m.attach_addr(["vstf_server@vstf.com"], m.FROM)
101     m.attach_addr(["wangli11@huawei.com"], m.TO)
102     context = """
103         <!DOCTYPE html>
104         <html>
105         <head>
106         <title>vstf</title>
107         </head>
108         
109         <body>
110             hello vstf
111         </body>
112         
113         </html>
114     """
115     m.attach_text(context, m.HTML)
116     m.attach_title("Email from xeson Check")
117     m.send()