JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / reporters / mail / sendmail.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
11 import logging
12 from vstf.controller.reporters.mail.mail import Mail
13 from vstf.controller.settings.mail_settings import MailSettings
14 LOG = logging.getLogger(__name__)
15
16
17 class SendMail(object):
18     def __init__(self, mail_info):
19         self._mail_info = mail_info
20
21     def send(self):
22         send = Mail(self._mail_info['server']['host'],
23                     self._mail_info['server']['username'],
24                     self._mail_info['server']['password']
25                     )
26         send.attach_addr(self._mail_info['body']['from'], send.FROM)
27         send.attach_addr(self._mail_info['body']['to'], send.TO)
28         send.attach_addr(self._mail_info['body']['cc'], send.CC)
29         send.attach_addr(self._mail_info['body']['bcc'], send.CC)
30
31         LOG.info(self._mail_info['body'])
32
33         if 'attach' in self._mail_info['body']:
34             send.attach_files(self._mail_info['body']['attach'])
35         send.attach_text(self._mail_info['body']['content'], self._mail_info['body']['subtype'])
36         send.attach_title(self._mail_info['body']['subject'])
37         send.send()
38
39
40 def unit_test():
41     mail_settings = MailSettings()
42     mail = SendMail(mail_settings.settings)
43
44     attach_list = ['1', '2']
45     mail_settings.set_attach(attach_list)
46
47     context = """
48         <!DOCTYPE html>
49         <html>
50         <head>
51         <title>vstf</title>
52         </head>
53         
54         <body>
55             hello vstf
56         </body>
57         
58         </html>
59     """
60     mail_settings.set_subtype('html')
61     mail_settings.set_content(context)
62
63     mail.send()
64
65
66 if __name__ == '__main__':
67     unit_test()