Skip to content

Commit

Permalink
Use SMTP compatible line separators in generated messages
Browse files Browse the repository at this point in the history
  • Loading branch information
d-maurer committed Feb 8, 2024
1 parent 083b967 commit 78abacf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changelog
5.2 (unreleased)
----------------

- Use SMTP compatible line separators in generated messages.

- Remove doc string from the MailHost ``sendTemplate`` method
to prevent publishing.



5.1 (2024-02-07)
----------------
Expand Down
8 changes: 4 additions & 4 deletions src/Products/MailHost/MailHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def sendTemplate(trueself,
immediate=False,
charset=None,
msg_type=None):
"""Render a mail template, then send it...
"""
# Render a mail template, then send it...
#
mtemplate = getattr(self, messageTemplate)
messageText = mtemplate(self, trueself.REQUEST)
trueself.send(messageText, mto=mto, mfrom=mfrom,
Expand Down Expand Up @@ -224,8 +224,8 @@ def send(self,

@security.protected(use_mailhost_services)
def simple_send(self, mto, mfrom, subject, body, immediate=False):
body = f'From: {mfrom}\nTo: {mto}\nSubject: {subject}\n\n{body}'
self._send(mfrom, mto, body, immediate)
msg = f'From: {mfrom}\nTo: {mto}\nSubject: {subject}\n\n{body}'
self.send(msg, immediate=immediate)

def _makeMailer(self):
""" Create a SMTPMailer """
Expand Down
33 changes: 21 additions & 12 deletions src/Products/MailHost/tests/testMailHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

import os.path
import re
import shutil
import tempfile
import unittest
Expand Down Expand Up @@ -208,11 +209,11 @@ def testSendWithMtoList(self):
self.assertEqual(mailhost.sent, outmsg)

def testSimpleSend(self):
outmsg = """\
From: [email protected]
To: "Name, Nick" <[email protected]>, "Foo Bar" <[email protected]>
Subject: This is the subject
outmsg = b"""\
From: [email protected]\r
To: "Name, Nick" <[email protected]>, "Foo Bar" <[email protected]>\r
Subject: This is the subject\r
\r
This is the message body."""

mailhost = self._makeOne('MailHost')
Expand All @@ -221,15 +222,15 @@ def testSimpleSend(self):
mfrom='[email protected]',
subject='This is the subject',
body='This is the message body.')
self.assertEqual(mailhost.sent, outmsg)
self.assertEqual(_rm_date(mailhost.sent), outmsg)
self.assertEqual(mailhost.immediate, False)

def testSendImmediate(self):
outmsg = """\
From: [email protected]
To: "Name, Nick" <[email protected]>, "Foo Bar" <[email protected]>
Subject: This is the subject
outmsg = b"""\
From: [email protected]\r
To: "Name, Nick" <[email protected]>, "Foo Bar" <[email protected]>\r
Subject: This is the subject\r
\r
This is the message body."""

mailhost = self._makeOne('MailHost')
Expand All @@ -239,7 +240,7 @@ def testSendImmediate(self):
subject='This is the subject',
body='This is the message body.',
immediate=True)
self.assertEqual(mailhost.sent, outmsg)
self.assertEqual(_rm_date(mailhost.sent), outmsg)
self.assertEqual(mailhost.immediate, True)

def testSendBodyWithUrl(self):
Expand Down Expand Up @@ -745,3 +746,11 @@ def testNotStartQueueProcessorThread(self):
self.assertFalse(mh.started_queue_processor_thread)
md = zope.sendmail.maildir.Maildir(self.smtp_queue_directory)
self.assertEqual(len(list(md)), 1)


_date_hdr_re = re.compile(b"^Date:.*\r\n", re.I | re.M)


def _rm_date(msg):
"""remove a ``Date`` header from *msg* (bytes)."""
return _date_hdr_re.sub(b"", msg)

0 comments on commit 78abacf

Please sign in to comment.