Skip to content

Commit

Permalink
Enable more expressive emails with send_email_with_attachment
Browse files Browse the repository at this point in the history
This commit adds the ability to write more expressive e-mails
with the `send_email_with_attachment` function by enabling
direct `MIMEText` to be passed as the `body` of the e-mail to be
sent.

This allows for `MIMEText` with subtype `html` (or other subtypes)
which enables feature like messages with hyperlinks, etc.

Previously, the function only allowed passing `str` as the body
text which disable use of hyperlinks, etc.

See the following for examples of fancy e-mail features:
https://realpython.com/python-send-email/#sending-fancy-emails
  • Loading branch information
njmei committed Sep 5, 2024
1 parent 813926d commit cd52373
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/aibs_informatics_aws_utils/ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ def send_email_with_attachment(
source: Union[str, EmailAddress],
to_addresses: Sequence[Union[str, EmailAddress]],
subject: str,
body: str = "",
body: Union[str, MIMEText] = "",
attachments_paths: Optional[List[Path]] = None,
) -> SendRawEmailResponseTypeDef:
"""
Args:
source: Source email address
to_addresses: List of recipient email addresses
subject: Email subject
body: Email body
body: Email body which can be either basic str or MIMEText (which can allow html with hyperlinks)
attachments_paths: List of optional paths to read contents from and attach to the email
Returns: `SendEmailResponseTypeDef`
"""
Expand All @@ -139,7 +139,10 @@ def send_email_with_attachment(
msg["To"] = ", ".join(to_addresses)

msg_body = MIMEMultipart("alternative")
msg_body.attach(MIMEText(body))
if isinstance(body, str):
msg_body.attach(MIMEText(body))
else:
msg_body.attach(body)
msg.attach(msg_body)

if attachments_paths is not None:
Expand Down

0 comments on commit cd52373

Please sign in to comment.