Skip to content

[Mailer] Fix attachment content encoding for SendgridTransport #32645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Jul 21, 2019

Q A
Branch? 4.3
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

Using the SendgridTransport (HTTP), emails cannot be sent as soon as they have attachments.
This is due to Sendgrid not supporting line breaks in base64 encoded content (encoding is considered invalid, which results in a 400 response from the Sendgrid API).

PHP Fatal error: Uncaught Symfony\Component\Mailer\Exception\TransportException: Unable to send an email: The attachment content must be base64 encoded. (code 400). in ./vendor/symfony/sendgrid-mailer/Http/Api/SendgridTransport.php:51 Stack trace: #0 ./vendor/symfony/mailer/Transport/Http/Api/AbstractApiTransport.php(59): Symfony\Component\Mailer\Bridge\Sendgrid\Http\Api\SendgridTransport->doSendEmail(Object(Symfony\Component\Mime\Email), Object(Symfony\Component\Mailer\DelayedSmtpEnvelope))

It has been reported multiple times in sendgrid official integrations, and the answer was always "please open a PR to document this in our troubleshooting section, strict base64 is required".
See limitation documented here https://github.com/sendgrid/sendgrid-ruby/blob/master/USE_CASES.md#adding-attachments and a bug report sendgrid/sendgrid-php#272.

@@ -146,6 +146,14 @@ public function getPreparedHeaders(): Headers
return $headers;
}

/**
* @internal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't make sense to me to have an internal method for cross component uses
if we think this is a needed API, we should commit to it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against making this public, that leaks internal information I don't want to expose.

@@ -113,9 +113,11 @@ private function getAttachments(Email $email): array
$headers = $attachment->getPreparedHeaders();
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
$disposition = $headers->getHeaderBody('Content-Disposition');
// Sendgrid does not accept line breaks for base64 encoded attachments
$content = 'base64' === $attachment->getEncoding() ? base64_encode($attachment->getBody()) : $attachment->bodyToString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like bypassing the encoding abstraction
can't we instead decide for the encoding earlier? (a line-break-less variant of base64)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have a specific base64 encoder, but it can’t be done earlier since the email is not aware of the transport. The transport would have to set the encoder on each attachment (DataPart instance) at send time

nicolas-grekas added a commit that referenced this pull request Jan 4, 2020
…tuart Fyfe)

This PR was squashed before being merged into the 4.3 branch.

Discussion
----------

[Mailer] Remove line breaks in email attachment content

Line breaks are not allowed in attachment content when sending over the
API.

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #33671, Closes #32645
| License       | MIT
| Doc PR        |

This is a fix for #33671. Send grid's API throws a 400 error when sending email attachments with default base64 encoding.
Removing the line breaks resolves this issue.

Commits
-------

a28a7f9 [Mailer] Remove line breaks in email attachment content
@chalasr chalasr deleted the sendgrid-api-attach branch January 4, 2020 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants