sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.53k stars 712 forks source link

sendgrid.helpers.mail.mail.Mail.from_EmailMessage raises an error from within the sendgrid library #980

Open kaya-zekioglu opened 3 years ago

kaya-zekioglu commented 3 years ago

Issue Summary

sendgrid.helpers.mail.mail.Mail.from_EmailMessage raises an error from within the sendgrid library.

For some context:

Steps to Reproduce

  1. Create an instance of a very simple python email.message.EmailMessage (https://docs.python.org/3/library/email.examples.html)
  2. Try to convert it to sendgrid.helpers.mail.mail.Mail using the from_EmailMessage classmethod

Code Snippet

from email.message import EmailMessage
msg = EmailMessage()
msg.set_content('CONTENT')
msg['Subject'] = 'SUBJECT'
msg['From'] = 'me@email.com'
msg['To'] = 'you@email.com'

from sendgrid import Mail
Mail.from_EmailMessage(msg)

Exception/Log

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.7/site-packages/sendgrid/helpers/mail/mail.py", line 1000, in from_EmailMessage
    to_emails=Email(message.get('To')),
  File "/opt/conda/lib/python3.7/site-packages/sendgrid/helpers/mail/mail.py", line 72, in __init__
    self.add_to(to_emails, global_substitutions, is_multiple)
  File "/opt/conda/lib/python3.7/site-packages/sendgrid/helpers/mail/mail.py", line 276, in add_to
    self._set_emails(to_email, global_substitutions, is_multiple, p)
  File "/opt/conda/lib/python3.7/site-packages/sendgrid/helpers/mail/mail.py", line 180, in _set_emails
    personalization.add_email(emails)
  File "/opt/conda/lib/python3.7/site-packages/sendgrid/helpers/mail/personalization.py", line 29, in add_email
    raise ValueError('Please use a To, Cc or Bcc object.')
ValueError: Please use a To, Cc or Bcc object.

Technical details:

shwetha-manvinkurke commented 3 years ago

@kaya-zekioglu This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

micahjsmith commented 2 months ago

same error. this method is totally broken

micahjsmith commented 2 months ago

here is an implementation that does work for simple cases (untested for complex cases)

import email.message
from sendgrid.helpers.mail import Content, From, Header, HtmlContent, Mail, To

# See https://www.twilio.com/docs/sendgrid/api-reference/mail-send/errors#headers-errors
SENDGRID_HEADER_RESERVED_KEYS = (
    "x-sg-id",
    "x-sg-eid",
    "received",
    "dkim-signature",
    "Content-Type",
    "Content-Transfer-Encoding",
    "To",
    "From",
    "Subject",
    "Reply-To",
    "CC",
    "BCC",
)

def email_message_to_sendgrid_mail(message: email.message.EmailMessage) -> Mail:
    mail = Mail(
        from_email=From(message.get("From")),
        to_emails=To(message.get("To")),
        subject=message.get("Subject"),
    )
    mail.add_content(Content(message.get_content_type(), message.get_content().strip()))
    for k, v in message.items():
        if all(k.lower() != rk.lower() for rk in SENDGRID_HEADER_RESERVED_KEYS):
            logger.debug(f"Adding header {k}: {v}")
            mail.add_header(Header(k, v))
    return mail

python 3.11 sendgrid 6.11