def send_report(rcpt, jobid=None, **options): subject = 'Your Marteau report' root = options.get('report.root', DEFAULT_ROOT) sender = options.get('report.sender', DEFAULT_SENDER) smtp_host = options.get('smtp.host', DEFAULT_SMTP_HOST) smtp_port = int(options.get('smtp.port', DEFAULT_SMTP_PORT)) smtp_user = options.get('smtp.user') smtp_password = options.get('smtp.password') # preparing the message if jobid is None: url = root else: url = '%s/test/%s' % (root, jobid) body = TMPL % url msg = MIMEText(body.encode('utf-8'), 'plain', 'utf8') def _normalize_realname(field): address = AddressList(field).addresslist if len(address) == 1: realname, email = address[0] if realname != '': return '%s <%s>' % (str(Header(realname, 'utf-8')), str(email)) return field msg['From'] = _normalize_realname(sender) msg['To'] = _normalize_realname(rcpt) msg['Subject'] = Header(subject, 'utf-8') logger.debug('Connecting to %s:%d' % (smtp_host, smtp_port)) try: server = smtplib.SMTP(smtp_host, smtp_port, timeout=5) except (smtplib.SMTPConnectError, socket.error), e: return False, str(e)
return '%s <%s>' % (str(Header(realname, 'utf-8')), str(email)) return field msg['From'] = _normalize_realname(sender) msg['To'] = _normalize_realname(rcpt) msg['Subject'] = Header(subject, 'utf-8') logger.debug('Connecting to %s:%d' % (smtp_host, smtp_port)) try: server = smtplib.SMTP(smtp_host, smtp_port, timeout=5) except (smtplib.SMTPConnectError, socket.error), e: return False, str(e) # auth if smtp_user is not None and smtp_password is not None: logger.debug('Login with %r' % smtp_user) try: server.login(smtp_user, smtp_password) except (smtplib.SMTPHeloError, smtplib.SMTPAuthenticationError, smtplib.SMTPException), e: return False, str(e) # the actual sending logger.debug('Sending the mail') try: server.sendmail(sender, [rcpt], msg.as_string()) finally: server.quit() return True, None