Changeset - 9a0c41175e66
[Not reviewed]
default
6 2 0
Mads Kiilerich - 5 years ago 2020-10-12 21:05:32
mads@kiilerich.com
Grafted from: ddd4226ea976
mail: use plain standard library for sending mails instead of rcmail

Avoid a lot of custom complexity that adds no value.

A few trivial lines from rcmail are inlined.
8 files changed with 38 insertions and 754 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -26,8 +26,12 @@ Original author and date, and relevant c
 
:license: GPLv3, see LICENSE.md for more details.
 
"""
 

	
 
import email.mime.multipart
 
import email.mime.text
 
import email.utils
 
import os
 
import smtplib
 
import time
 
import traceback
 
from collections import OrderedDict
 
from operator import itemgetter
 
@@ -41,7 +45,6 @@ from kallithea.lib import celerylib, con
 
from kallithea.lib.helpers import person
 
from kallithea.lib.hooks import log_create_repository
 
from kallithea.lib.indexers.daemon import WhooshIndexingDaemon
 
from kallithea.lib.rcmail.smtp_mailer import SmtpMailer
 
from kallithea.lib.utils import action_logger
 
from kallithea.lib.utils2 import asbool, ascii_bytes
 
from kallithea.lib.vcs.utils import author_email
 
@@ -309,10 +312,38 @@ def send_email(recipients, subject, body
 
        log.warning(logmsg)
 
        return False
 

	
 
    msg = email.mime.multipart.MIMEMultipart('alternative')
 
    msg['Subject'] = subject
 
    msg['From'] = app_email_from  # fallback - might be overridden by a header
 
    msg['To'] = ', '.join(recipients)
 
    msg['Date'] = email.utils.formatdate(time.time())
 

	
 
    for key, value in headers.items():
 
        msg[key] = value
 

	
 
    msg.attach(email.mime.text.MIMEText(body, 'plain'))
 
    msg.attach(email.mime.text.MIMEText(html_body, 'html'))
 

	
 
    try:
 
        m = SmtpMailer(app_email_from, smtp_username, smtp_password, smtp_server, smtp_auth,
 
                       smtp_port, smtp_use_ssl, smtp_use_tls)
 
        m.send(recipients, subject, body, html_body, headers=headers)
 
        if smtp_use_ssl:
 
            smtp_serv = smtplib.SMTP_SSL(smtp_server, smtp_port)
 
        else:
 
            smtp_serv = smtplib.SMTP(smtp_server, smtp_port)
 

	
 
        if smtp_use_tls:
 
            smtp_serv.starttls()
 

	
 
        if smtp_auth:
 
            smtp_serv.ehlo()  # populate esmtp_features
 
            smtp_serv.esmtp_features["auth"] = smtp_auth
 

	
 
        if smtp_username and smtp_password:
 
            smtp_serv.login(smtp_username, smtp_password)
 

	
 
        smtp_serv.sendmail(app_email_from, recipients, msg.as_string())
 
        smtp_serv.quit()
 

	
 
        log.info('Mail was sent to: %s' % recipients)
 
    except:
 
        log.error('Mail sending failed')
 
        log.error(traceback.format_exc())
kallithea/lib/rcmail/__init__.py
Show inline comments
 
deleted file
kallithea/lib/rcmail/exceptions.py
Show inline comments
 
deleted file
kallithea/lib/rcmail/message.py
Show inline comments
 
deleted file
kallithea/lib/rcmail/response.py
Show inline comments
 
deleted file
kallithea/lib/rcmail/smtp_mailer.py
Show inline comments
 
deleted file
kallithea/lib/rcmail/utils.py
Show inline comments
 
deleted file
kallithea/tests/other/test_mail.py
Show inline comments
 
@@ -10,7 +10,7 @@ from kallithea.tests import base
 
class smtplib_mock(object):
 

	
 
    @classmethod
 
    def SMTP(cls, server, port, local_hostname):
 
    def SMTP(cls, server, port):
 
        return smtplib_mock()
 

	
 
    def ehlo(self):
 
@@ -25,7 +25,7 @@ class smtplib_mock(object):
 
        smtplib_mock.lastmsg = msg
 

	
 

	
 
@mock.patch('kallithea.lib.rcmail.smtp_mailer.smtplib', smtplib_mock)
 
@mock.patch('kallithea.lib.celerylib.tasks.smtplib', smtplib_mock)
 
class TestMail(base.TestController):
 

	
 
    def test_send_mail_trivial(self):
 
@@ -191,6 +191,6 @@ class TestMail(base.TestController):
 
        assert 'Subject: %s' % subject in smtplib_mock.lastmsg
 
        assert body in smtplib_mock.lastmsg
 
        assert html_body in smtplib_mock.lastmsg
 
        assert 'Extra: yes' in smtplib_mock.lastmsg
 
        assert 'extra: yes' in smtplib_mock.lastmsg
 
        # verify that headers dict hasn't mutated by send_email
 
        assert headers == {'extra': 'yes'}
0 comments (0 inline, 0 general)