Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1956.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure @email-send adds message intro ONLY for non-multipart messages. Fixes #1956.
7 changes: 6 additions & 1 deletion src/plone/restapi/services/email_send/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ def reply(self):
)
)

message = f"{message_intro} \n {message}"
if not isinstance(message, str):
message = str(message)

message = message_from_string(message)
if not message.is_multipart():
payload = message.get_payload()
message.set_payload(f"{message_intro}\n\n{payload}")

message["Reply-To"] = sender_from_address
try:
host.send(
Expand Down
159 changes: 159 additions & 0 deletions src/plone/restapi/tests/test_services_email_send.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from email import message_from_string
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from plone.app.testing import setRoles
from plone.app.testing import SITE_OWNER_NAME
from plone.app.testing import SITE_OWNER_PASSWORD
Expand Down Expand Up @@ -98,3 +101,159 @@ def test_email_send_anonymous(self):
)

self.assertEqual(response.status_code, 401)

def test_email_send_plain_text_has_intro(self):
response = self.api_session.post(
"/@email-send",
json={
"to": "jane@doe.com",
"from": "john@doe.com",
"message": "This is a plain text message.",
"name": "John Doe",
"subject": "Test Subject",
},
)
transaction.commit()

self.assertEqual(response.status_code, 204)
msg = self.mailhost.messages[0]
if isinstance(msg, bytes) and bytes is not str:
msg = msg.decode("utf-8")

self.assertTrue("You are receiving this mail because" in msg)
self.assertTrue("John Doe" in msg)
self.assertTrue("This is a plain text message." in msg)
intro_pos = msg.find("You are receiving this mail because")
message_pos = msg.find("This is a plain text message.")
self.assertGreater(
message_pos, intro_pos, "Intro text should appear before message"
)

def test_email_send_multipart_no_intro(self):
multipart_msg = MIMEMultipart("alternative")
multipart_msg["Subject"] = "Test Multipart"
multipart_msg["From"] = "sender@example.com"
multipart_msg["To"] = "recipient@example.com"

text_part = MIMEText("This is the plain text version.", "plain")
html_part = MIMEText(
"<html><body>This is the <b>HTML</b> version.</body></html>", "html"
)

multipart_msg.attach(text_part)
multipart_msg.attach(html_part)

message_str = multipart_msg.as_string()

response = self.api_session.post(
"/@email-send",
json={
"to": "jane@doe.com",
"from": "john@doe.com",
"message": message_str,
"name": "John Doe",
"subject": "Test Multipart Subject",
},
)
transaction.commit()

self.assertEqual(response.status_code, 204)
msg = self.mailhost.messages[0]
if isinstance(msg, bytes) and bytes is not str:
msg = msg.decode("utf-8")

self.assertFalse("You are receiving this mail because" in msg)
self.assertTrue("multipart/alternative" in msg)
self.assertTrue("This is the plain text version." in msg)
self.assertTrue("This is the <b>HTML</b> version." in msg)

def test_email_send_multipart_structure_preserved(self):
multipart_msg = MIMEMultipart("alternative")
text_part = MIMEText("Plain text content here.", "plain")
html_part = MIMEText(
"<html><body><p>HTML content here.</p></body></html>", "html"
)

multipart_msg.attach(text_part)
multipart_msg.attach(html_part)

message_str = multipart_msg.as_string()

response = self.api_session.post(
"/@email-send",
json={
"to": "jane@doe.com",
"from": "john@doe.com",
"message": message_str,
"subject": "Structure Test",
},
)
transaction.commit()

self.assertEqual(response.status_code, 204)
msg = self.mailhost.messages[0]
if isinstance(msg, bytes) and bytes is not str:
msg = msg.decode("utf-8")

parsed_msg = message_from_string(msg)

self.assertTrue(parsed_msg.is_multipart())
self.assertTrue("multipart/alternative" in parsed_msg.get_content_type())
parts = list(parsed_msg.walk())
self.assertEqual(len(parts), 3)
text_found = False
html_found = False
for part in parts:
if part.get_content_type() == "text/plain":
self.assertTrue("Plain text content here." in part.get_payload())
text_found = True
elif part.get_content_type() == "text/html":
self.assertTrue("HTML content here." in part.get_payload())
html_found = True
self.assertTrue(text_found, "Text part not found")
self.assertTrue(html_found, "HTML part not found")

def test_email_send_multipart_with_attachment(self):
"""Test that multipart messages with attachments are handled correctly."""
from email import encoders
from email.mime.base import MIMEBase

multipart_msg = MIMEMultipart()
multipart_msg["Subject"] = "Test with Attachment"
multipart_msg["From"] = "sender@example.com"
multipart_msg["To"] = "recipient@example.com"

text_part = MIMEText("This message has an attachment.", "plain")
multipart_msg.attach(text_part)

attachment = MIMEBase("application", "octet-stream")
attachment.set_payload(b"Test attachment content")
encoders.encode_base64(attachment)
attachment.add_header(
"Content-Disposition",
'attachment; filename="test.txt"',
)
multipart_msg.attach(attachment)

message_str = multipart_msg.as_string()

response = self.api_session.post(
"/@email-send",
json={
"to": "jane@doe.com",
"from": "john@doe.com",
"message": message_str,
"subject": "Attachment Test",
},
)
transaction.commit()

self.assertEqual(response.status_code, 204)
msg = self.mailhost.messages[0]
if isinstance(msg, bytes) and bytes is not str:
msg = msg.decode("utf-8")

self.assertFalse("You are receiving this mail because" in msg)
self.assertTrue("multipart" in msg.lower())
self.assertTrue("Content-Disposition" in msg)
self.assertTrue("attachment" in msg.lower())