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
 
@@ -23,14 +23,18 @@ Original author and date, and relevant c
 
:created_on: Oct 6, 2010
 
:author: marcink
 
:copyright: (c) 2013 RhodeCode GmbH, and others.
 
: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
 
from time import mktime
 

	
 
import celery.utils.log
 
@@ -38,13 +42,12 @@ from tg import config
 

	
 
import kallithea
 
from kallithea.lib import celerylib, conf, ext_json
 
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
 
from kallithea.model import db
 
from kallithea.model.repo import RepoModel
 

	
 
@@ -306,16 +309,44 @@ def send_email(recipients, subject, body
 
        log.debug("Sending e-mail. " + logmsg)
 
    else:
 
        log.error("SMTP mail server not configured - cannot send e-mail.")
 
        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())
 
        return False
 
    return True
 

	
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
 
@@ -7,13 +7,13 @@ from kallithea.model import db
 
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):
 
        pass
 

	
 
    def quit(self):
 
@@ -22,13 +22,13 @@ class smtplib_mock(object):
 
    def sendmail(self, sender, dest, msg):
 
        smtplib_mock.lastsender = sender
 
        smtplib_mock.lastdest = set(dest)
 
        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):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = ['rcpt1', 'rcpt2']
 
        envelope_from = 'noreply@mailserver.org'
 
@@ -188,9 +188,9 @@ class TestMail(base.TestController):
 
        assert smtplib_mock.lastdest == set(recipients)
 
        assert smtplib_mock.lastsender == envelope_from
 
        assert r'From: "foo (fubar) \"baz\" (no-reply)" <%s>' % envelope_from in smtplib_mock.lastmsg
 
        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)