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