Skip to content
Closed
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
1 change: 1 addition & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SMTP_DEFAULT_RECIPIENT=
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
SENDGRID_API_KEY=
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
- run: git checkout HEAD^2
- name: Run Tests
env:
SMTP_DEFAULT_RECIPIENT: ${{ secrets.SMTP_DEFAULT_RECIPIENT }}
MAILGUN_API_KEY: ${{ secrets.MAILGUN_API_KEY }}
MAILGUN_DOMAIN: ${{ secrets.MAILGUN_DOMAIN }}
SENDGRID_API_KEY: ${{ secrets.SENDGRID_API_KEY }}
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
- ./tests:/usr/local/src/tests
- ./phpunit.xml:/usr/local/src/phpunit.xml
environment:
- SMTP_DEFAULT_RECIPIENT
- MAILGUN_API_KEY
- MAILGUN_DOMAIN
- SENDGRID_API_KEY
Expand Down
12 changes: 11 additions & 1 deletion src/Utopia/Messaging/Adapter/Email/SMTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SMTP extends EmailAdapter
* @param string $smtpSecure SMTP Secure prefix. Can be '', 'ssl' or 'tls'
* @param bool $smtpAutoTLS Enable/disable SMTP AutoTLS feature. Defaults to false.
* @param string $xMailer The value to use for the X-Mailer header.
* @param string $defaultRecipient The default recipient to use for the email.
*/
public function __construct(
private string $host,
Expand All @@ -27,7 +28,8 @@ public function __construct(
private string $password = '',
private string $smtpSecure = '',
private bool $smtpAutoTLS = false,
private string $xMailer = ''
private string $xMailer = '',
private string $defaultRecipient = ''
) {
if (!\in_array($this->smtpSecure, ['', 'ssl', 'tls'])) {
throw new \InvalidArgumentException('Invalid SMTP secure prefix. Must be "", "ssl" or "tls"');
Expand Down Expand Up @@ -72,6 +74,14 @@ protected function process(EmailMessage $message): array
$mail->AltBody = \strip_tags($mail->AltBody);
$mail->AltBody = \trim($mail->AltBody);

if (empty($message->getTo())) {
if (empty($message->getBCC()) || empty($this->defaultRecipient)) {
throw new \Exception('Email requires either "to" recipients or both BCC and a default recipient configurations');
}

$mail->addAddress($this->defaultRecipient);
}

foreach ($message->getTo() as $to) {
$mail->addAddress($to);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Messaging/Adapter/Email/SMTPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,44 @@ public function testSendEmailWithAttachment(): void
$this->assertEquals($subject, $lastEmail['subject']);
$this->assertEquals($content, \trim($lastEmail['text']));
}

public function testSendEmailOnlyBCC(): void
{
$defaultRecipient = \getenv('SMTP_DEFAULT_RECIPIENT') ?? 'placeholder@localhost.test';
$sender = new SMTP(
host: 'maildev',
port: 1025,
defaultRecipient: $defaultRecipient,
);

$subject = 'Test Subject';
$content = 'Test Content';
$fromName = 'Test Sender';
$fromEmail = 'sender@localhost.test';
$bcc = [
[
'email' => 'tester@localhost.test',
'name' => 'Test Recipient',
],
];

$message = new Email(
to: [],
subject: $subject,
content: $content,
fromName: $fromName,
fromEmail: $fromEmail,
bcc: $bcc,
);

$response = $sender->send($message);

$lastEmail = $this->getLastEmail();

$this->assertResponse($response);
$this->assertEquals($defaultRecipient, $lastEmail['to'][0]['address']);
$this->assertEquals($fromEmail, $lastEmail['from'][0]['address']);
$this->assertEquals($subject, $lastEmail['subject']);
$this->assertEquals($content, \trim($lastEmail['text']));
}
}
Loading