Aşağıdaki örnekte Swiftmailer ile nasıl email gönderildiğini göreceğiz. Ücretsiz ve harika bir email test aracı olan Mailtrap'ı testimizde kullanacağız.


Parametres.yml


Eğer Mailtrap ayarlarını kullanmazsanız, email bilgilerini symfony profiler içinde görebilirsiniz. Eğer Mailtrap ayarlarını kullanırsanız, emailin Mailtrap inboxu içinde olduğunu görebilirsiniz.


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

Config.yml ve Config_dev.yml


Aşağıdaki ayarlar normal ortamlarda emaili gönderir.


swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
auth_mode: "%mailer_auth_mode%"
port: "%mailer_port%"
spool: { type: memory }

Config_test.yml


Aşağıdaki ayarlar test ortamında football/app/cache/test/email/spool/default/HolnCItlkD.message dosyasını yaratır ve bu dosya, emailin bilgilerini barındırır.


swiftmailer:
disable_delivery: true
spool:
type: file
path: %kernel.cache_dir%/email/spool

Services.yml


services:
application_frontend.util.mailer:
class: Application\FrontendBundle\Util\Mailer
arguments:
- @mailer
- @templating
- @logger
- %mailer_noreply%

Services.yml


services:
application_frontend.service.default:
class: Application\FrontendBundle\Service\AccountService
arguments:
- @application_frontend.util.mailer

Mailer.php


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()
)
);
}
}
}

AccountService.php


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']);
}
}

Registration.txt.twig


<h3>Registration</h3>
<br />
<p>Hello,</p>
<br />
<p>Thank you for registering with us.</p>
<br />
<p>{{ signature }}</p>

Sonuç


Symfony profiler



Mailtrap inbox



Mailtrap ayarları