Skip to content

Commit 5f30836

Browse files
authored
Merge pull request #95 from utopia-php/add-default-recipient
feat: added bcc only emails to smtp
2 parents d880b6b + 3694fcf commit 5f30836

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/Utopia/Messaging/Adapter/Email/SMTP.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
private string $password = '',
2828
private string $smtpSecure = '',
2929
private bool $smtpAutoTLS = false,
30-
private string $xMailer = ''
30+
private string $xMailer = '',
3131
) {
3232
if (!\in_array($this->smtpSecure, ['', 'ssl', 'tls'])) {
3333
throw new \InvalidArgumentException('Invalid SMTP secure prefix. Must be "", "ssl" or "tls"');
@@ -72,6 +72,14 @@ protected function process(EmailMessage $message): array
7272
$mail->AltBody = \strip_tags($mail->AltBody);
7373
$mail->AltBody = \trim($mail->AltBody);
7474

75+
if (empty($message->getTo())) {
76+
if (empty($message->getBCC()) && empty($message->getDefaultRecipient())) {
77+
throw new \Exception('Email requires either "to" recipients or both BCC and a default recipient configurations');
78+
}
79+
80+
$mail->addAddress($message->getDefaultRecipient());
81+
}
82+
7583
foreach ($message->getTo() as $to) {
7684
$mail->addAddress($to);
7785
}
@@ -111,7 +119,8 @@ protected function process(EmailMessage $message): array
111119
$sent = $mail->send();
112120

113121
if ($sent) {
114-
$response->setDeliveredTo(\count($message->getTo()));
122+
$totalDelivered = \count($message->getTo()) + \count($message->getCC() ?: []) + \count($message->getBCC() ?: []);
123+
$response->setDeliveredTo($totalDelivered);
115124
}
116125

117126
foreach ($message->getTo() as $to) {
@@ -122,6 +131,22 @@ protected function process(EmailMessage $message): array
122131
$response->addResult($to, $sent ? '' : $error);
123132
}
124133

134+
foreach ($message->getCC() as $cc) {
135+
$error = empty($mail->ErrorInfo)
136+
? 'Unknown error'
137+
: $mail->ErrorInfo;
138+
139+
$response->addResult($cc['email'], $sent ? '' : $error);
140+
}
141+
142+
foreach ($message->getBCC() as $bcc) {
143+
$error = empty($mail->ErrorInfo)
144+
? 'Unknown error'
145+
: $mail->ErrorInfo;
146+
147+
$response->addResult($bcc['email'], $sent ? '' : $error);
148+
}
149+
125150
return $response->toArray();
126151
}
127152
}

src/Utopia/Messaging/Messages/Email.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Email implements Message
1919
* @param string|null $replyToEmail The email address of the reply to.
2020
* @param array<Attachment>|null $attachments The attachments of the email.
2121
* @param bool $html Whether the message is HTML or not.
22+
* @param string|null $defaultRecipient The default recipient of the email.
2223
*
2324
* @throws \InvalidArgumentException
2425
*/
@@ -33,7 +34,8 @@ public function __construct(
3334
private ?array $cc = null,
3435
private ?array $bcc = null,
3536
private ?array $attachments = null,
36-
private bool $html = false
37+
private bool $html = false,
38+
private ?string $defaultRecipient = null
3739
) {
3840
if (\is_null($this->replyToName)) {
3941
$this->replyToName = $this->fromName;
@@ -126,4 +128,9 @@ public function isHtml(): bool
126128
{
127129
return $this->html;
128130
}
131+
132+
public function getDefaultRecipient(): ?string
133+
{
134+
return $this->defaultRecipient;
135+
}
129136
}

tests/Messaging/Adapter/Email/SMTPTest.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function testSendEmailWithAttachment(): void
5454
$fromName = 'Test Sender';
5555
$fromEmail = '[email protected]';
5656

57-
5857
$message = new Email(
5958
to: [$to],
6059
subject: $subject,
@@ -78,4 +77,43 @@ public function testSendEmailWithAttachment(): void
7877
$this->assertEquals($subject, $lastEmail['subject']);
7978
$this->assertEquals($content, \trim($lastEmail['text']));
8079
}
80+
81+
public function testSendEmailOnlyBCC(): void
82+
{
83+
$sender = new SMTP(
84+
host: 'maildev',
85+
port: 1025,
86+
);
87+
88+
$defaultRecipient = '[email protected]';
89+
$subject = 'Test Subject';
90+
$content = 'Test Content';
91+
$fromName = 'Test Sender';
92+
$fromEmail = '[email protected]';
93+
$bcc = [
94+
[
95+
'email' => '[email protected]',
96+
'name' => 'Test Recipient 2',
97+
],
98+
];
99+
100+
$message = new Email(
101+
to: [],
102+
subject: $subject,
103+
content: $content,
104+
fromName: $fromName,
105+
fromEmail: $fromEmail,
106+
bcc: $bcc,
107+
defaultRecipient: $defaultRecipient,
108+
);
109+
110+
$response = $sender->send($message);
111+
112+
$lastEmail = $this->getLastEmail();
113+
114+
$this->assertResponse($response);
115+
$this->assertEquals($fromEmail, $lastEmail['from'][0]['address']);
116+
$this->assertEquals($subject, $lastEmail['subject']);
117+
$this->assertEquals($content, \trim($lastEmail['text']));
118+
}
81119
}

0 commit comments

Comments
 (0)