diff options
Diffstat (limited to 'vstf/vstf/controller/reporters/mail')
-rwxr-xr-x | vstf/vstf/controller/reporters/mail/__init__.py | 14 | ||||
-rwxr-xr-x | vstf/vstf/controller/reporters/mail/mail.py | 117 | ||||
-rwxr-xr-x | vstf/vstf/controller/reporters/mail/sendmail.py | 64 |
3 files changed, 195 insertions, 0 deletions
diff --git a/vstf/vstf/controller/reporters/mail/__init__.py b/vstf/vstf/controller/reporters/mail/__init__.py new file mode 100755 index 00000000..89dcd4e2 --- /dev/null +++ b/vstf/vstf/controller/reporters/mail/__init__.py @@ -0,0 +1,14 @@ +# Copyright Huawei Technologies Co., Ltd. 1998-2015. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the License); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. diff --git a/vstf/vstf/controller/reporters/mail/mail.py b/vstf/vstf/controller/reporters/mail/mail.py new file mode 100755 index 00000000..42d60b1a --- /dev/null +++ b/vstf/vstf/controller/reporters/mail/mail.py @@ -0,0 +1,117 @@ +import smtplib +import logging +import os +from email.mime.application import MIMEApplication +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + +LOG = logging.getLogger(__name__) +SRV = 'localhost' +USER = None +PASSWD = None + + +class Mail(object): + def __init__(self, srv=SRV, user=USER, passwd=PASSWD): + self.srv = srv + self.user = USER + self.passwd = PASSWD + self._msg = MIMEMultipart('mixed') + + # addr type + self.TO = "To" + self.FROM = "From" + self.CC = "Cc" + self.BCC = "Bcc" + self.__addr_choice = [self.TO, self.FROM, self.CC, self.BCC] + + # text mode + self.HTML = "html" + self.PLAIN = "plain" + self.__mode = [self.HTML, self.PLAIN] + # self._charset = 'gb2312' + + # timeout + self.timeout = 10 + + def attach_addr(self, addr, addr_type): + """ + :param addr: a list of email address. + :param addr_type: must be one of [to, from, cc, bcc] + """ + if not addr or not isinstance(addr, list): + LOG.error("The addr must be a list") + return False + + if addr_type not in self.__addr_choice: + LOG.error("Not support addr type") + return False + + if not self._msg[addr_type]: + self._msg[addr_type] = ','.join(addr) + self._msg[addr_type].join(addr) + + def attach_title(self, title): + """Notice: + each time attach title, the old title will be covered. + """ + if title: + self._msg["Subject"] = str(title) + + def attach_text(self, text, mode): + if mode not in self.__mode: + LOG.error("The text mode not support.") + return False + + msg_alternative = MIMEMultipart('alternative') + msg_text = MIMEText(text, mode) + msg_alternative.attach(msg_text) + + return self._msg.attach(msg_alternative) + + def attach_files(self, files): + for _file in files: + part = MIMEApplication(open(_file, "rb").read()) + part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(_file)) + self._msg.attach(part) + + def send(self): + server = smtplib.SMTP(self.srv, timeout=self.timeout) + if self.user: + server.ehlo() + server.starttls() + server.ehlo() + server.login(self.user, self.passwd) + maillist = [] + if self._msg[self.TO]: + maillist += self._msg[self.TO].split(',') + if self._msg[self.CC]: + maillist += self._msg[self.CC].split(',') + if self._msg[self.BCC]: + maillist += self._msg[self.BCC].split(',') + ret = server.sendmail(self._msg[self.FROM].split(','), + maillist, self._msg.as_string()) + LOG.info("send mail ret:%s", ret) + server.close() + + +if __name__ == "__main__": + m = Mail() + m.attach_addr(["vstf_server@vstf.com"], m.FROM) + m.attach_addr(["wangli11@huawei.com"], m.TO) + context = """ + <!DOCTYPE html> + <html> + <head> + <title>vstf</title> + </head> + + <body> + hello vstf + </body> + + </html> + """ + m.attach_text(context, m.HTML) + m.attach_title("Email from xeson Check") + m.send() diff --git a/vstf/vstf/controller/reporters/mail/sendmail.py b/vstf/vstf/controller/reporters/mail/sendmail.py new file mode 100755 index 00000000..ecc6fe93 --- /dev/null +++ b/vstf/vstf/controller/reporters/mail/sendmail.py @@ -0,0 +1,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() |