Skip to content
Open
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
41 changes: 41 additions & 0 deletions bundle/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,47 @@ public function sendMail(array|string $receivers, string $subject, string $templ
$this->mailer->send($email);
}

/**
* Sends a group mail to users in bcc.
*
* Sender and recipient are set to sender
*
* Sender can be:
* a string: [email protected]
* an array: array( '[email protected]' => 'Netgen Site' )
*
* Bcc can be:
* a string: [email protected]
* or:
* array( '[email protected]' => 'Netgen Site' ) or
* array( '[email protected]', '[email protected]' ) or
* array( '[email protected]' => 'Netgen Site', '[email protected]' => 'Example' )
*
* @param string|string[] $bcc
* @param array<string, mixed> $parameters
* @param string|string[]|null $sender
*/
public function sendGroupMail(array|string $bcc, string $subject, string $template, array $parameters = [], array|string|null $sender = null): void
Copy link
Member

@emodric emodric Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I would do here is not add a sendGroupMail that only sends to bcc, but add two new parameters at the end of sendMail method ccReceivers and bccReceivers which can then be used in conjuction with regular receivers.

It's a pretty common usecase to have a regular receiver and then a list of ccs and bccs.

This way, we would not have to hardcode that the sender is also a receiver, which is fine in itself, but not always.

{
try {
$senderAddress = $this->createSenderAddress($sender);
} catch (InvalidArgumentException $e) {
$this->logger->error($e->getMessage());

return;
}

$email = (new Email())
->from($senderAddress)
->sender($senderAddress)
->to($senderAddress)
->bcc(...$this->createReceiverAddresses($bcc))
->subject($this->translator->trans($subject, [], 'ngsite_mail'))
->html($this->twig->render($template, $parameters));

$this->mailer->send($email);
}

/**
* Creates a sender address from provided value.
* If sender is not provided (if it is null), it attempts to get the sender from the parameters:
Expand Down