Я использую этот сервис Mailer Class для отправки электронной почты (с учетной записью Google). Вы можете отредактировать следующий файл конфигурации, чтобы использовать собственную конфигурацию smtp
In config / services.yaml
parameters:
mailer:
smtp_host: 'smtp.gmail.com'
smtp_port: 587
smtp_cert: 'tls'
smtp_username: 'myaccount@gmail.com'
smtp_password: 'mypassword'
services
mybase.mailer.services:
class: App\Services\Mailer
arguments: ['%mailer%']
public: true
В src / Services / Mailer. php
<?php
/**
* Service that can be used to send email using basic swift mailer transport
*/
namespace App\Services;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class Mailer
{
/**
* @var array $emailConfig
*/
private $emailConfig;
/**
* Mailer constructor
*
* @param ParameterBagInterface $params
*/
public function __construct(ParameterBagInterface $params)
{
$this->emailConfig = $params->get('mailer');
}
public function sendMail(array $mailInfo, string $html, array $files = [])
{
$emailConfig = $this->emailConfig;
$smtpHost = $emailConfig['smtp_host'];
$smtpPort = $emailConfig['smtp_port'];
$smtpCert = $emailConfig['smtp_cert'];
$smtpUsername = $emailConfig['smtp_username'];
$smtpPassword = $emailConfig['smtp_password'];
$transport = (new \Swift_SmtpTransport($smtpHost, $smtpPort, $smtpCert))
->setUsername($smtpUsername)
->setPassword($smtpPassword)
;
$swiftMailer = new \Swift_Mailer($transport);
$message = (new \Swift_Message($mailInfo['title']))
->setFrom([$smtpUsername => $mailInfo['senderName']])
->setTo($mailInfo['sendTo'])
->setBody($html, 'text/html');
foreach ($files as $file) {
$message->attach(\Swift_Attachment::fromPath($file));
}
$swiftMailer->send($message);
}
}
Внутри другого сервиса
<?php
namespace App\Services;
use App\Services\Mailer;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyOtherService
{
/**
* @var \Mailer $mailer;
*/
private $mailer;
/**
* @var \Twig\Environment $templating;
*/
private $templating;
/**
* @var ContainerInterface $container;
*/
private $container;
public function __construct(Mailer $mailer, \Twig\Environment $templating, ContainerInterface $container)
{
$this->mailer = $mailer;
$this->templating = $templating;
$this->container = $container;
}
public function methodName()
{
// Logic ....
$params = [
"sendTo" => 'myclient@email.com',
"title" => "This is my awesome title",
"senderName"=> 'My company name',
];
$html = $this->templating->render("path/to-my-email/email-template.html.twig", [
'some_variable' => 'SOME VARIABLES',
]);
$this->mailer->sendMail($params, $html);
}
}
Или от контроллера
public function sendClientEmail(Request $request, Mailer $mailer): Response
{
// My code logics
$params = [
"sendTo" => 'myclient@email.com',
"title" => "This is my awesome title",
"senderName"=> 'My company name',
];
$html = $this->render("path/to-my-email/email-template.html.twig", [
'some_variable' => 'SOME VARIABLES',
])->getContent();
$mailer->sendMail($params, $html);
// Rest of my code logics
}