从symfony发送PHP邮件
我想问一下如何从Symfony框架组件Symfony mailer发送PHP mail()从symfony发送PHP邮件,php,symfony,mailer,Php,Symfony,Mailer,我想问一下如何从Symfony框架组件Symfony mailer发送PHP mail() "symfony/mailer": "5.0.*", DSN: 控制器方法: public function test(): Response { $transport = new EsmtpTransport('localhost'); $mailer = new Mailer($transport); $username = $this->getUser()->
"symfony/mailer": "5.0.*",
DSN:
控制器方法:
public function test(): Response
{
$transport = new EsmtpTransport('localhost');
$mailer = new Mailer($transport);
$username = $this->getUser()->getUsername();
/** @var Users $user */
$user = $this->getDoctrine()->getRepository(Users::class)->findOneBy(['username' => $username]);
if (!$user)
return new Response("User $username not found! Email not tested.");
$to = $user->getEmail();
if ($to) {
$email = new Email();
$email->from('test@mydomain.com');
$email->to($to);
$email->subject('Test mail');
$email->text('This is test mail from ... for user ' . $to);
$mailer->send($email);
return new Response('Mail send!');
}
return new Response('Mail not sent - user email information missing!');
}
如果我正确理解您的问题,您希望使用新的symfony mailer组件发送电子邮件 不久前,我使用mailer组件编写了一个邮件服务,也许你能从中获得一些灵感
namespace App\Service;
use App\Utils\Utils;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class MailService
{
private $mailer;
/**
* MailService constructor.
*
* @param $mailer
*/
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
/**
* @param string $renderedView
* @param string $adresse
* @param string $subject
*
* @throws TransportExceptionInterface
* here $renderedview is a a twig template i used to generate my email html
*/
public function sendMail(string $renderedView, string $adresse, string $subject, string $env)
{
if ('dev' !== $env) {
$email = (new Email())
->from(your@email.com)
->to($adresse)
->subject($subject)
->html($renderedView);
$this->mailer->send($email);
}
}
}
您必须根据MAILER参数配置MAILER\u DSN
()
在文档中,您将发现如何处理一些常见的邮件或自己进行配置
祝您好运,体验愉快:)如果我正确理解了您的问题,您希望使用新的symfony mailer组件发送电子邮件 不久前,我使用mailer组件编写了一个邮件服务,也许你能从中获得一些灵感
namespace App\Service;
use App\Utils\Utils;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class MailService
{
private $mailer;
/**
* MailService constructor.
*
* @param $mailer
*/
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
/**
* @param string $renderedView
* @param string $adresse
* @param string $subject
*
* @throws TransportExceptionInterface
* here $renderedview is a a twig template i used to generate my email html
*/
public function sendMail(string $renderedView, string $adresse, string $subject, string $env)
{
if ('dev' !== $env) {
$email = (new Email())
->from(your@email.com)
->to($adresse)
->subject($subject)
->html($renderedView);
$this->mailer->send($email);
}
}
}
您必须根据MAILER参数配置MAILER\u DSN
()
在文档中,您将发现如何处理一些常见的邮件或自己进行配置
祝你好运,体验一下吧:)使用php邮件函数并不是最聪明的事情,因为它会阻塞,而且有时会非常慢。如果您真的想使用mailer组件,您可能可以为它编写自己的传输,但是如果您有合适的用例,为什么要使用php的
mail()
?对于本地测试,有fakesmtp甚至空邮件程序,您可以使用探查器对其进行调试。@Jakumi您好,非常感谢您提供更深入的解释。根据您的建议,使用smtp更好吗?如果我想使用mail(),是否在Symfony mailer组件中存在此传输?Thx。最好使用smtp。在默认的symfony mailer组件中,php的邮件没有传输
,但是linuxsendmail有传输
(查看了/usr/bin/sendmail
。据我所知,php的mail
-使用了一些操作系统命令,必须对其进行配置,否则您的邮件也无法到达目的地。我收集了您对此的最新信息,因此我强烈建议您使用smtp。它是标准化的,非常容易设置,并且没有任何其他功能。)mail/Sendmail的缺点使用php mail函数并不是最明智的做法,因为它会阻塞邮件,而且有时会非常慢。如果您真的想使用它,您可能可以为mailer组件编写自己的传输,但可能您有一个合适的用例,为什么要使用php的mail()
?对于本地测试,有fakesmtp甚至空邮件程序,您可以使用探查器对其进行调试。@Jakumi Hi,非常感谢您的深入解释。根据您的建议,最好使用smtp?如果我想使用mail(),在Symfony-mailer组件?Thx中存在此传输。最好使用smtp。在默认的Symfony-mailer组件中,php的mail
没有传输,但是linux的sendmail
(查看了/usr/bin/sendmail
。据我所知,php的mail
-使用了一些操作系统命令,必须对其进行配置,否则您的邮件也无法到达目的地。我收集了您对此的最新信息,因此我强烈建议您使用smtp。它是标准化的,非常容易设置,并且没有任何其他功能。)mail/sendmail的缺点