Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
25     def __init__(self, srv=SRV, user=USER, passwd=PASSWD):
26         self.srv = srv
27         self.user = USER
28         self.passwd = PASSWD
29         self._msg = MIMEMultipart('mixed')
30
31         # addr type
32         self.TO = "To"
33         self.FROM = "From"
34         self.CC = "Cc"
35         self.BCC = "Bcc"
36         self.__addr_choice = [self.TO, self.FROM, self.CC, self.BCC]
37
38         # text mode
39         self.HTML = "html"
40         self.PLAIN = "plain"
41         self.__mode = [self.HTML, self.PLAIN]
42         # self._charset = 'gb2312'
43
44         # timeout
45         self.timeout = 10
46
47     def attach_addr(self, addr, addr_type):
48         """
49         :param addr: a list of email address.
50         :param addr_type: must be one of [to, from, cc, bcc]
51         """
52         if not addr or not isinstance(addr, list):
53             LOG.error("The addr must be a list")
54             return False
55
56         if addr_type not in self.__addr_choice:
57             LOG.error("Not support addr type")
58             return False
59
60         if not self._msg[addr_type]:
61             self._msg[addr_type] = ','.join(addr)
62         self._msg[addr_type].join(addr)
63
64     def attach_title(self, title):
65         """Notice:
66         each time attach title, the old title will be covered.
67         """
68         if title:
69             self._msg["Subject"] = str(title)
70
71     def attach_text(self, text, mode):
72         if mode not in self.__mode:
73             LOG.error("The text mode not support.")
74             return False
75
76         msg_alternative = MIMEMultipart('alternative')
77         msg_text = MIMEText(text, mode)
78         msg_alternative.attach(msg_text)
79
80         return self._msg.attach(msg_alternative)
81
82     def attach_files(self, files):
83         for _file in files:
84             part = MIMEApplication(open(_file, "rb").read())
85             part.add_header(
86                 'Content-Disposition',
87                 'attachment',
88                 filename=os.path.basename(_file))
89             self._msg.attach(part)
90
91     def send(self):
92         server = smtplib.SMTP(self.srv, timeout=self.timeout)
93         if self.user:
94             server.ehlo()
95             server.starttls()
96             server.ehlo()
97             server.login(self.user, self.passwd)
98         maillist = []
99         if self._msg[self.TO]:
100             maillist += self._msg[self.TO].split(',')
101         if self._msg[self.CC]:
102             maillist += self._msg[self.CC].split(',')
103         if self._msg[self.BCC]:
104             maillist += self._msg[self.BCC].split(',')
105         ret = server.sendmail(self._msg[self.FROM].split(','),
106                               maillist, self._msg.as_string())
107         LOG.info("send mail ret:%s", ret)
108         server.close()
109
110
111 if __name__ == "__main__":
112     m = Mail()
113     m.attach_addr(["vstf_server@vstf.com"], m.FROM)
114     m.attach_addr(["test@test.com"], m.TO)
115     context = """
116         <!DOCTYPE html>
117         <html>
118         <head>
119         <title>vstf</title>
120         </head>
121
122         <body>
123             hello vstf
124         </body>
125
126         </html>
127     """
128     m.attach_text(context, m.HTML)
129     m.attach_title("Email from xeson Check")
130     m.send()