Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / controller / reporters / mail / sendmail.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015-09-07
5 # see license for license details
6 __version__ = ''' '''
7
8 import logging
9 from vstf.controller.reporters.mail.mail import Mail
10 from vstf.controller.settings.mail_settings import MailSettings
11 LOG = logging.getLogger(__name__)
12
13
14 class SendMail(object):
15     def __init__(self, mail_info):
16         self._mail_info = mail_info
17
18     def send(self):
19         send = Mail(self._mail_info['server']['host'],
20                     self._mail_info['server']['username'],
21                     self._mail_info['server']['password']
22                     )
23         send.attach_addr(self._mail_info['body']['from'], send.FROM)
24         send.attach_addr(self._mail_info['body']['to'], send.TO)
25         send.attach_addr(self._mail_info['body']['cc'], send.CC)
26         send.attach_addr(self._mail_info['body']['bcc'], send.CC)
27
28         LOG.info(self._mail_info['body'])
29
30         if 'attach' in self._mail_info['body']:
31             send.attach_files(self._mail_info['body']['attach'])
32         send.attach_text(self._mail_info['body']['content'], self._mail_info['body']['subtype'])
33         send.attach_title(self._mail_info['body']['subject'])
34         send.send()
35
36
37 def unit_test():
38     mail_settings = MailSettings()
39     mail = SendMail(mail_settings.settings)
40
41     attach_list = ['1', '2']
42     mail_settings.set_attach(attach_list)
43
44     context = """
45         <!DOCTYPE html>
46         <html>
47         <head>
48         <title>vstf</title>
49         </head>
50         
51         <body>
52             hello vstf
53         </body>
54         
55         </html>
56     """
57     mail_settings.set_subtype('html')
58     mail_settings.set_content(context)
59
60     mail.send()
61
62
63 if __name__ == '__main__':
64     unit_test()