Skip to content

Commit 947cf27

Browse files
bug #53653 [Mailer] [Scaleway] Fix attachment handling (madbob)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Mailer] [Scaleway] Fix attachment handling | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Here a pair of fixes for attachments' handling. 1) the attribute in the JSON payload is "attachments", not "attachment". Here you find the [reference documentation](https://www.scaleway.com/en/developers/api/transactional-email/#going-further) 2) as `bodyToString()` already returns a base64-encoded string, there is no need to re-encode it. I've replicated the behaviour [adopted by BrevoMailer](https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/Mailer/Bridge/Brevo/Transport/BrevoApiTransport.php#L129), that I also use with profit Commits ------- 812d7a9 [Mailer] [Scaleway] Fix attachment handling
2 parents 0294563 + 812d7a9 commit 947cf27

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/Symfony/Component/Mailer/Bridge/Scaleway/Tests/Transport/ScalewayApiTransportTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mailer\Exception\HttpTransportException;
1919
use Symfony\Component\Mime\Address;
2020
use Symfony\Component\Mime\Email;
21+
use Symfony\Component\Mime\Part\DataPart;
2122
use Symfony\Contracts\HttpClient\ResponseInterface;
2223

2324
class ScalewayApiTransportTest extends TestCase
@@ -64,6 +65,10 @@ public function testSend()
6465
$this->assertSame(['email' => 'saif.gmati@symfony.com', 'name' => 'Saif Eddin'], $body['to'][0]);
6566
$this->assertSame('Hello!', $body['subject']);
6667
$this->assertSame('Hello There!', $body['text']);
68+
$this->assertCount(1, $body['attachments']);
69+
$this->assertSame('attachment.txt', $body['attachments'][0]['name']);
70+
$this->assertSame('text/plain', $body['attachments'][0]['type']);
71+
$this->assertSame(base64_encode('some attachment'), $body['attachments'][0]['content']);
6772

6873
return new JsonMockResponse(['emails' => [['message_id' => 'foobar']]], [
6974
'http_code' => 200,
@@ -76,7 +81,8 @@ public function testSend()
7681
$mail->subject('Hello!')
7782
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
7883
->from(new Address('fabpot@symfony.com', 'Fabien'))
79-
->text('Hello There!');
84+
->text('Hello There!')
85+
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'));
8086

8187
$message = $transport->send($mail);
8288

src/Symfony/Component/Mailer/Bridge/Scaleway/Transport/ScalewayApiTransport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function getPayload(Email $email, Envelope $envelope): array
9999
$payload['html'] = $email->getHtmlBody();
100100
}
101101
if ($attachements = $this->prepareAttachments($email)) {
102-
$payload['attachment'] = $attachements;
102+
$payload['attachments'] = $attachements;
103103
}
104104

105105
return $payload;
@@ -115,7 +115,7 @@ private function prepareAttachments(Email $email): array
115115
$attachments[] = [
116116
'name' => $filename,
117117
'type' => $headers->get('Content-Type')->getBody(),
118-
'content' => base64_encode($attachment->bodyToString()),
118+
'content' => str_replace("\r\n", '', $attachment->bodyToString()),
119119
];
120120
}
121121

0 commit comments

Comments
 (0)