Skip to content

Commit 728d371

Browse files
committed
feature #59612 [Mailer] Add attachments support for Sweego Mailer Bridge (welcoMattic)
This PR was merged into the 7.3 branch. Discussion ---------- [Mailer] Add attachments support for Sweego Mailer Bridge | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes<!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT Sweego has added support for attachments on their API. This PR include it in the Bridge. I tested it in a real application with .txt, .pdf and .png files. Mails are delivered with attachments. As no other Bridge has tests for attachments AFAIK, I didn't write them. It could be part of a dedicated PR. Commits ------- b5b76fc feat(sweego): Add attachments support for Sweego Mailer Bridge
2 parents b7d8f16 + b5b76fc commit 728d371

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/Symfony/Component/Mailer/Bridge/Sweego/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add support for attachments
8+
49
7.2
510
---
611

src/Symfony/Component/Mailer/Bridge/Sweego/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ MAILER_DSN=sweego+api://API_KEY@default
2424
where:
2525
- `API_KEY` is your Sweego API Key
2626

27+
Features
28+
--------
29+
30+
### Attachments
31+
32+
The bridge supports both regular attachments and inline attachments (for embedding images in HTML emails):
33+
34+
```php
35+
use Symfony\Component\Mime\Email;
36+
37+
$email = new Email();
38+
$email
39+
->to('to@example.com')
40+
->from('from@example.com')
41+
->subject('Email with attachments')
42+
->text('Here is the text version')
43+
->html('<p>Here is the HTML content</p>')
44+
// Regular attachment
45+
->attach('Hello world!', 'test.txt', 'text/plain')
46+
// Inline attachment (embedded image)
47+
->embed(fopen('image.jpg', 'r'), 'image.jpg', 'image/jpeg')
48+
;
49+
```
50+
2751
Webhook
2852
-------
2953

src/Symfony/Component/Mailer/Bridge/Sweego/Transport/SweegoApiTransport.php

+28
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ private function getPayload(Email $email, Envelope $envelope): array
100100
$payload['message-html'] = $email->getHtmlBody();
101101
}
102102

103+
if ($email->getAttachments()) {
104+
$payload['attachments'] = $this->getAttachments($email);
105+
}
106+
103107
if ($payload['headers'] = $this->prepareHeaders($email->getHeaders())) {
104108
if (\count($payload['headers']) > 5) {
105109
throw new InvalidArgumentException('Sweego API supports up to 5 headers.');
@@ -111,6 +115,30 @@ private function getPayload(Email $email, Envelope $envelope): array
111115
return $payload;
112116
}
113117

118+
private function getAttachments(Email $email): array
119+
{
120+
$attachments = [];
121+
foreach ($email->getAttachments() as $attachment) {
122+
$headers = $attachment->getPreparedHeaders();
123+
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
124+
$disposition = $headers->getHeaderBody('Content-Disposition');
125+
126+
$att = [
127+
'content' => $attachment->bodyToString(),
128+
'filename' => $filename,
129+
'disposition' => $disposition,
130+
];
131+
132+
if ('inline' === $disposition) {
133+
$att['content_id'] = $attachment->hasContentId() ? $attachment->getContentId() : $filename;
134+
}
135+
136+
$attachments[] = $att;
137+
}
138+
139+
return $attachments;
140+
}
141+
114142
private function prepareHeaders(Headers $headers): array
115143
{
116144
$headersPrepared = [];

0 commit comments

Comments
 (0)