Skip to content

[Mailer] Add attachments support for Sweego Mailer Bridge #59612

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

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Sweego/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add support for attachments

7.2
---

Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Mailer/Bridge/Sweego/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ MAILER_DSN=sweego+api://API_KEY@default
where:
- `API_KEY` is your Sweego API Key

Features
--------

### Attachments

The bridge supports both regular attachments and inline attachments (for embedding images in HTML emails):

```php
use Symfony\Component\Mime\Email;

$email = new Email();
$email
->to('to@example.com')
->from('from@example.com')
->subject('Email with attachments')
->text('Here is the text version')
->html('<p>Here is the HTML content</p>')
// Regular attachment
->attach('Hello world!', 'test.txt', 'text/plain')
// Inline attachment (embedded image)
->embed(fopen('image.jpg', 'r'), 'image.jpg', 'image/jpeg')
;
```

Webhook
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ private function getPayload(Email $email, Envelope $envelope): array
$payload['message-html'] = $email->getHtmlBody();
}

if ($email->getAttachments()) {
$payload['attachments'] = $this->getAttachments($email);
}

if ($payload['headers'] = $this->prepareHeaders($email->getHeaders())) {
if (\count($payload['headers']) > 5) {
throw new InvalidArgumentException('Sweego API supports up to 5 headers.');
Expand All @@ -111,6 +115,30 @@ private function getPayload(Email $email, Envelope $envelope): array
return $payload;
}

private function getAttachments(Email $email): array
{
$attachments = [];
foreach ($email->getAttachments() as $attachment) {
$headers = $attachment->getPreparedHeaders();
$filename = $headers->getHeaderParameter('Content-Disposition', 'filename');
$disposition = $headers->getHeaderBody('Content-Disposition');

$att = [
'content' => $attachment->bodyToString(),
'filename' => $filename,
'disposition' => $disposition,
];

if ('inline' === $disposition) {
$att['content_id'] = $attachment->hasContentId() ? $attachment->getContentId() : $filename;
}

$attachments[] = $att;
}

return $attachments;
}

private function prepareHeaders(Headers $headers): array
{
$headersPrepared = [];
Expand Down
Loading