Only for Documentation:
# we need a message part to bundle the HTML and Plain Text
textmsgpart = MIMEMultipart("alternative")
# create plain text part of email
plaintextpart = MIMEText("Fill out your plain text", "plain", "utf-8")
# create html text of email
html = "<html><body><p>fill out your text in HTML</p></body></html>"
htmlpart = MIMEText(html, "html", "utf-8")
# bundle the parts
textmsgpart.attach(plaintextpart)
textmsgpart.attach(htmlpart)
# handle the file attachment
# attachment is an instance of NamedBlobFile with a PDF
filepart = MIMEBase("application", "pdf")
filepart.set_payload(attachment.data)
encode_base64(filepart)
filepart.add_header(
"Content-Disposition",
"attachment",
filename=(Header(attachment.filename, "utf-8").encode()),
)
# we need a mixed Multipart Message to bundle the text bundle and the attachment
msg = MIMEMultipart("mixed")
msg["From"] = "sender@somedomain"
msg["To"] = "recipient@some_other_domain"
msg["Subject"] = "The Mail Subject"
msg["Reply-To"] = "reply_to_other@xyz"
msg["Return-Path"] = "error@abc"
# add the text message bundle
msg.attach(textmsgpart)
# add the file attachment
msg.attach(textmsgpart)
# send with plone.api
api.portal.send_email(
sender=msg["From"],
recipient=msg["To"],
subject=msg["Subject"],
body=msg.as_string(),
immediate=True
)