Skip to content
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

add bcc to emails #2914

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function getConfigTreeBuilder()
->scalarNode('user_class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('firewall_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('model_manager_name')->defaultNull()->end()
->scalarNode('bcc_email')->defaultNull()->end()
->booleanNode('use_authentication_listener')->defaultTrue()->end()
->booleanNode('use_listener')->defaultTrue()->end()
->booleanNode('use_flash_notifications')->defaultTrue()->end()
Expand Down
11 changes: 7 additions & 4 deletions DependencyInjection/FOSUserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function load(array $configs, ContainerBuilder $container)
}

if (!empty($config['registration'])) {
$this->loadRegistration($config['registration'], $container, $loader, $config['from_email']);
$this->loadRegistration($config['registration'], $container, $loader, $config['from_email'], $config['bcc_email']);
}

if (!empty($config['change_password'])) {
$this->loadChangePassword($config['change_password'], $container, $loader);
}

if (!empty($config['resetting'])) {
$this->loadResetting($config['resetting'], $container, $loader, $config['from_email']);
$this->loadResetting($config['resetting'], $container, $loader, $config['from_email'], $config['bcc_email']);
}

if (!empty($config['group'])) {
Expand Down Expand Up @@ -206,8 +206,9 @@ private function loadProfile(array $config, ContainerBuilder $container, XmlFile
* @param ContainerBuilder $container
* @param XmlFileLoader $loader
* @param array $fromEmail
* @param string $bccEmail
*/
private function loadRegistration(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail)
private function loadRegistration(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail, $bccEmail)
{
$loader->load('registration.xml');
$this->sessionNeeded = true;
Expand All @@ -223,6 +224,7 @@ private function loadRegistration(array $config, ContainerBuilder $container, Xm
unset($config['confirmation']['from_email']);
}
$container->setParameter('fos_user.registration.confirmation.from_email', array($fromEmail['address'] => $fromEmail['sender_name']));
$container->setParameter('fos_user.bcc_email', $bccEmail);

$this->remapParametersNamespaces($config, $container, array(
'confirmation' => 'fos_user.registration.confirmation.%s',
Expand Down Expand Up @@ -250,7 +252,7 @@ private function loadChangePassword(array $config, ContainerBuilder $container,
* @param XmlFileLoader $loader
* @param array $fromEmail
*/
private function loadResetting(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail)
private function loadResetting(array $config, ContainerBuilder $container, XmlFileLoader $loader, array $fromEmail, $bccEmail)
{
$this->mailerNeeded = true;
$loader->load('resetting.xml');
Expand All @@ -261,6 +263,7 @@ private function loadResetting(array $config, ContainerBuilder $container, XmlFi
unset($config['email']['from_email']);
}
$container->setParameter('fos_user.resetting.email.from_email', array($fromEmail['address'] => $fromEmail['sender_name']));
$container->setParameter('fos_user.bcc_email', $bccEmail);

$this->remapParametersNamespaces($config, $container, array(
'' => array(
Expand Down
5 changes: 5 additions & 0 deletions Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,18 @@ protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail)
$renderedLines = explode("\n", trim($renderedTemplate));
$subject = array_shift($renderedLines);
$body = implode("\n", $renderedLines);
$bccEmail = $this->parameters['bcc_email'];

$message = (new \Swift_Message())
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($body);

if (!empty($bccEmail)) {
$message->setBcc($bccEmail);
}

$this->mailer->send($message);
}
}
6 changes: 6 additions & 0 deletions Mailer/TwigSwiftMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
$subject = $template->renderBlock('subject', $context);
$textBody = $template->renderBlock('body_text', $context);

$bccEmail = $this->parameters['bcc_email'];

$htmlBody = '';

if ($template->hasBlock('body_html', $context)) {
Expand All @@ -110,6 +112,10 @@ protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
->setFrom($fromEmail)
->setTo($toEmail);

if (!empty($bccEmail)) {
$message->setBcc($bccEmail);
}

if (!empty($htmlBody)) {
$message->setBody($htmlBody, 'text/html')
->addPart($textBody, 'text/plain');
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/mailer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
<argument key="resetting">%fos_user.resetting.email.from_email%</argument>
</argument>
<argument key="bcc_email">%fos_user.bcc_email%</argument>
</argument>
<tag name="fos_user.requires_swift" />
</service>
Expand All @@ -44,6 +45,7 @@
<argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
<argument key="resetting">%fos_user.resetting.email.from_email%</argument>
</argument>
<argument key="bcc_email">%fos_user.bcc_email%</argument>
</argument>
<tag name="fos_user.requires_swift" />
</service>
Expand Down
1 change: 1 addition & 0 deletions Resources/doc/configuration_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All available configuration options are listed below with their default values.
use_authentication_listener: true
use_username_form_type: true
model_manager_name: null # change it to the name of your entity/document manager if you don't want to use the default one.
bcc_email: null # change it to valid email to add bcc to all emails
from_email:
address: [email protected]
sender_name: webmaster
Expand Down
1 change: 1 addition & 0 deletions Tests/Mailer/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private function getMailer()
'confirmation' => '[email protected]',
'resetting' => '[email protected]',
),
'bcc_email' => null,
)
);
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Mailer/TwigSwiftMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private function getTwigSwiftMailer()
'confirmation' => '[email protected]',
'resetting' => '[email protected]',
),
'bcc_email' => null,
)
);
}
Expand Down