Skip to content

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

Open
@kaya-zekioglu

Description

@kaya-zekioglu

Issue Summary

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

For some context:

  • I have code which sends composes and sends emails using python's email package.
  • I wish to use sendgrid to handle sending the emails as my SMTP proxy service in lieu of AWS SES or some other similar service.
  • AWS SES offers a send_raw_email method which accepts the result of invoking email.message.EmailMessage.as_bytes() and as such is compatible with my existing code which composes these emails.
  • I looked at the sendgrid library and this from_EmailMessage classmethod appears to provide a similar bridge from python's native email to provider specific API parameter.
  • However as documented in this issue it appears that this classmethod is not functional in the latest version of your library.
    • As a side note: now that this error lead me to look at the implementation of this classmethod it does appear to be too simplistic to account for the complexity of a generic email object. In particular this line would likely raise error if the content were not representable as a string (i.e. the Multipart Content-Type.)

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'] = '[email protected]'
msg['To'] = '[email protected]'

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:

  • sendgrid-python version: 6.6.0
  • python version: 3.7.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions