05/08/2015 - SYMFONY
Example below shows us how to send emails with Swiftmailer. We're going to use Mailtrap which is a free and great dummy email testing tool.
If you don't use Mailtrap settings below, emails won't be triggered but you can still see the email in symfony profiler. If you use Mailtrap settings like we do, you'll see the emails in Mailtrap inbox.
parameters:
# mailer_transport: smtp
# mailer_host: 127.0.0.1
# mailer_user: null
# mailer_password: null
# mailer_noreply:
# - noreply@mydomain.com
mailer_transport: smtp
mailer_host: mailtrap.io
mailer_user: 301048d74474739u1
mailer_password: ed9a12310a297t
mailer_auth_mode: cram-md5
mailer_port: 2525
mailer_noreply:
- noreply@mydomain.com
This would send the email in normal environment.
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
auth_mode: "%mailer_auth_mode%"
port: "%mailer_port%"
spool: { type: memory }
This would create football/app/cache/test/email/spool/default/HolnCItlkD.message
file containing the details of email in test
environment.
swiftmailer:
disable_delivery: true
spool:
type: file
path: %kernel.cache_dir%/email/spool
services:
application_frontend.util.mailer:
class: Application\FrontendBundle\Util\Mailer
arguments:
- @mailer
- @templating
- @logger
- %mailer_noreply%
services:
application_frontend.service.default:
class: Application\FrontendBundle\Service\AccountService
arguments:
- @application_frontend.util.mailer
namespace Application\FrontendBundle\Util;
use Psr\Log\LoggerInterface;
use Swift_Mailer;
use Swift_Message;
use Swift_RfcComplianceException;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class Mailer implements MailerInterface
{
private $mailer;
private $templating;
private $logger;
private $noreply;
public function __construct(
Swift_Mailer $mailer,
EngineInterface $templating,
LoggerInterface $logger,
array $noreply
) {
$this->mailer = $mailer;
$this->templating = $templating;
$this->logger = $logger;
$this->noreply = $noreply;
}
public function sendRegistration(array $to)
{
$body = $this->templating->render(
'ApplicationFrontendBundle:Mailer:registration.txt.twig', ['signature' => 'inanzzz']
);
$this->send($to, 'Registration confirmation!', $body, 'registration');
}
private function send($to, $subject, $body, $messageId)
{
$failedRecipients = [];
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($this->noreply)
->setTo($to)
->setBody($body, 'text/html');
$message->getHeaders()->addTextHeader('X-Message-ID', $messageId);
try {
$this->mailer->send($message, $failedRecipients);
} catch (Swift_RfcComplianceException $e) {
$this->logger->critical(
sprintf(
'Failed to send email to recipients [%s] with message: %s',
implode(', ', $failedRecipients),
$e->getMessage()
)
);
}
}
}
namespace Application\FrontendBundle\Service\AccountService;
use Application\FrontendBundle\Util\MailerInterface;
class AccountService implements AccountServiceInterface
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function sendEmail()
{
$this->mailer->sendRegistration(['customer@customer.com']);
}
}
<h3>Registration</h3>
<br />
<p>Hello,</p>
<br />
<p>Thank you for registering with us.</p>
<br />
<p>{{ signature }}</p>