summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/controller/reporters/mail/sendmail.py
blob: ecc6fe93c33e62177865bc814ddea936c9cd46f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python
# -*- coding: utf8 -*-
# author: wly
# date: 2015-09-07
# see license for license details
__version__ = ''' '''

import logging
from vstf.controller.reporters.mail.mail import Mail
from vstf.controller.settings.mail_settings import MailSettings
LOG = logging.getLogger(__name__)


class SendMail(object):
    def __init__(self, mail_info):
        self._mail_info = mail_info

    def send(self):
        send = Mail(self._mail_info['server']['host'],
                    self._mail_info['server']['username'],
                    self._mail_info['server']['password']
                    )
        send.attach_addr(self._mail_info['body']['from'], send.FROM)
        send.attach_addr(self._mail_info['body']['to'], send.TO)
        send.attach_addr(self._mail_info['body']['cc'], send.CC)
        send.attach_addr(self._mail_info['body']['bcc'], send.CC)

        LOG.info(self._mail_info['body'])

        if 'attach' in self._mail_info['body']:
            send.attach_files(self._mail_info['body']['attach'])
        send.attach_text(self._mail_info['body']['content'], self._mail_info['body']['subtype'])
        send.attach_title(self._mail_info['body']['subject'])
        send.send()


def unit_test():
    mail_settings = MailSettings()
    mail = SendMail(mail_settings.settings)

    attach_list = ['1', '2']
    mail_settings.set_attach(attach_list)

    context = """
        <!DOCTYPE html>
        <html>
        <head>
        <title>vstf</title>
        </head>
        
        <body>
            hello vstf
        </body>
        
        </html>
    """
    mail_settings.set_subtype('html')
    mail_settings.set_content(context)

    mail.send()


if __name__ == '__main__':
    unit_test()