[
https://issues.apache.org/jira/browse/AIRFLOW-2907?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Fokko Driesprong resolved AIRFLOW-2907.
---------------------------------------
Resolution: Fixed
> Sendgrid - Attachments - ERROR - Object of type 'bytes' is not JSON
> serializable
> --------------------------------------------------------------------------------
>
> Key: AIRFLOW-2907
> URL: https://issues.apache.org/jira/browse/AIRFLOW-2907
> Project: Apache Airflow
> Issue Type: Bug
> Components: contrib
> Reporter: Leo Gallucci
> Assignee: Leo Gallucci
> Priority: Minor
> Labels: sendgrid
> Fix For: 2.0.0
>
>
> Attempting to attach files via Sendgrid operator always gives:
> {code}
> Sendgrid - Attachments - ERROR - Object of type 'bytes' is not JSON
> serializable
> {code}
> h4. How to reproduce:
> By setting files=['/nfs/somefile_to_attach.ipynb'] while using the
> EmailOperator
> {code}
> export
> AIRFLOW__EMAIL__EMAIL_BACKEND="airflow.contrib.utils.sendgrid.send_email"
> {code}
> {code}
> from airflow.operators.email_operator import EmailOperator
> email_task = EmailOperator(
> task_id='email_task',
> to="[email protected]",
> subject='This has an attachment',
> html_content='this has an attachment',
> files=['/nfs/somefile_to_attach.ipynb'],
> dag=dag)
> {code}
> h4. Root cause
> Returned the encoded `bytes` type
> https://docs.python.org/3/library/base64.html#base64.b64encode
> However `attachment.content` expects a string.
> Problem is in this line:
> https://github.com/apache/incubator-airflow/blob/9d516c7134eb22a3c2fc63cf96626ef6e8b247f2/airflow/contrib/utils/sendgrid.py#L91
> {code}
> attachment.content = base64.b64encode(f.read())
> {code}
> h4. Solution via str()
> By googling the found solution was:
> https://stackoverflow.com/questions/47479205/e-mail-attachment-using-sendgird
> https://stackoverflow.com/questions/44933816/encode-csv-file-for-sendgrids-email-api/44934090
> {code}
> attachment.content = str(base64.b64encode(f.read()), 'utf-8')
> {code}
> I will send a PR
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)