Skip to content

Commit c871524

Browse files
committed
feat(sweego): Add attachments support for Sweego Mailer Bridge
1 parent c65ec65 commit c871524

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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)