class Sendmail(object): def __init__(self, config, template_report=False, debug=False): super(Sendmail, self).__init__() self._config = config self._debug = debug self._report = template_report env = Environment(loader=FileSystemLoader(TEMPLATE_PATH)) if template_report: self.template = env.get_template('report.html') else: self.template = env.get_template('issue.html') self._smtp = SMTP(self._config['host'], self._config['user'], self._config['password'], ssl=True) def _send(self, rcpt, html, _subject=None, _headers=None, important=False): if _subject: _subject = '%s %s' % (self._config['subject'], _subject) else: _subject = self._config['subject'] _msg = Message(rcpt, _subject, self._config['from'], html=html, important=important, headers=MAIL_HEADERS) # print _msg.as_string() # return True return self._smtp.send(_msg) def send(self, issue, important=False): return self.send_issue(issue, important=important) def send_issue(self, issue, important=False): if self._report: return False return self._send(issue['rcpt'], self.template.render( url=self._config['url'], issue=issue), issue['subject'], issue['time_window']['mail_headers'] if 'mail_headers' in issue['time_window'] else None, important=important ) def send_report(self, rcpt, data, important=False): if not self._report: return False csv = CSV() csv.add([CSV_FIELDS[item] for item in CSV_FIELDS]) for project in data: for issue in project['issues']: csv.add([issue[item] for item in CSV_FIELDS]) _subject = self._config['subject'] _html = self.template.render(projects=data, url=self._config['url']) _msg = Message(rcpt, _subject, self._config['from'], html=_html, important=important, headers=MAIL_HEADERS) _msg.add_attachment('report.csv', csv.get(), 'text/csv') # print _msg.as_string() # return True return self._smtp.send(_msg)
def test_succeeds_on_all_200_replies(self): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: [b'a', b'b']) ]) self.assertTupleEqual((False, ["'1' sent"]), (interrupted, res))
def test_fails_on_timeout_when_connecting(self): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: None) ]) self.assertTupleEqual((True, [ "Stopped with error: 'Timed out while" " establishing connection'" ]), (interrupted, res))
def test_fails_on_error_while_connecting(self): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: None) ]) self.assertTupleEqual((True, [ "Stopped with error: 'Something went" " wrong while trying to establish" " connection ()'" ]), (interrupted, res))
def test_fails_on_timeout(self): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: None) ]) self.assertTupleEqual((True, [ "Stopped with error: 'Connection timed out" " while trying to receive a reply" " from server'" ]), (interrupted, res))
def test_fails_on_500_reply_code_when_trying_to_send_a_message(self): with mc.patch(target='ssl.SSLSocket.recv', new=self.__tricky_500): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: [b'a', b'b']) ]) self.assertTupleEqual( (False, ["'1' failed with error: 'exterminate'"]), (interrupted, res))
def test_fails_on_cut_off_connection(self): with mc.patch(target='ssl.SSLSocket.recv', new=self.__cut_off_on_reply): x = SMTP('127.0.0.1', 4000, '', '') interrupted, res = x.send([ MailMock(lambda *args, **kwargs: "1", lambda *args, **kwargs: None, lambda *args, **kwargs: None, lambda *args, **kwargs: [b'a', b'b']) ]) self.assertTupleEqual((True, [ "Stopped with error:" " 'Connection shut down" " by the other side'" ]), (interrupted, res))
to_send_parts = [] if args.auth: types = smtp.auth_types auth_type = None if not (types and ('login' in types or 'plain' in types)): print('WARNING: There is no opportunity to authenticate...') elif 'login' in types: auth_type = 'login' elif 'plain' in types: auth_type = 'plain' if auth_type is not None: smtp.send('AUTH %s' % auth_type.upper()) user = input('Your username:\n') passw = getpass.getpass('Your password:\n') user = b64encode_str(user) passw = b64encode_str(passw) smtp.send(user, log_=False) smtp.send(passw, log_=False) to_send_parts.append(('MAIL FROM: <%s>' % args.fromemail, True)) to_send_parts.append(('RCPT TO: <%s>' % args.toemail, True)) if 'pipelining' in smtp.extensions: smtp.send_pipelining(to_send_parts)