Open kaya-zekioglu opened 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.
same error. this method is totally broken
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
Issue Summary
sendgrid.helpers.mail.mail.Mail.from_EmailMessage
raises an error from within the sendgrid library.For some context:
email
package.send_raw_email
method which accepts the result of invokingemail.message.EmailMessage.as_bytes()
and as such is compatible with my existing code which composes these emails.from_EmailMessage
classmethod appears to provide a similar bridge from python's nativeemail
to provider specific API parameter.Steps to Reproduce
email.message.EmailMessage
(https://docs.python.org/3/library/email.examples.html)sendgrid.helpers.mail.mail.Mail
using thefrom_EmailMessage
classmethodCode Snippet
Exception/Log
Technical details: