基本上,在我的家庭Ubuntu机器上,我希望能够从本地主机在symfony应用程序中发送电子邮件。我已经安装了sendmail及其基本配置。我可以使用命令行:sendmail -v命令发送电子邮件。收到电子邮件。我甚至可以使用PHP函数发送电子邮件:
$to = "receiver@mail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: sender@mail.com";
mail($to,$subject,$txt,$headers收到电子邮件。
然而,使用symfony邮件功能,我没有收到电子邮件,我也没有收到任何错误。
这是我在php.ini中的sendmail配置:
[mail function]
; For Win32 only.
; https://php.net/smtp
SMTP = localhost
; https://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; https://php.net/sendmail-from
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; https://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i这是我的symfony php代码:
public function resetPassword(User $user, ResetPasswordToken $resetToken): void
{
$to = "receiver@mail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: sender@mail.com";
mail($to,$subject,$txt,$headers); // <----- THIS IS WORKING!!!
$subject = 'My subject';
$email = (new TemplatedEmail());
$email->from(new Address('sender@mail.com'));
$email->to('receiver@mail.com');
$email->subject($subject);
$email->htmlTemplate('shop/email/reset_password.html.twig');
$email->context([
'resetToken' => $resetToken
]);
try {
$this->mailer->send($email); // <----- THIS IS NOT WORKING!!!
} catch (TransportExceptionInterface $e) {
dd($e->getCode());
}
}在我的.env文件中,我尝试了多个不同的配置,包括:
###> symfony/mailer ###
MAILER_DSN=smtp://localhost
###< symfony/mailer ###
###> symfony/mailer ###
MAILER_DSN=sendmail://default
###< symfony/mailer ###
###> symfony/mailer ###
MAILER_DSN=native://default
###< symfony/mailer ###他们都不起作用。任何想法都是值得赞赏的!
发布于 2022-08-19 12:25:43
令人震惊的是一切都很好。问题是,如果您使用--webapp选项与它们的安装程序一起安装symfony,就会安装队列信使:https://symfony.com/doc/current/messenger.html和所有在队列中的电子邮件。除掉信使解决了我的问题。谢谢!
发布于 2022-08-19 09:26:12
调试$this->mailer,例如,先用dd()调试,如果没有帮助.进行真正正确的调试。
同时,请记住,有时清除缓存可能会有所帮助。
php bin/console cache:clear
https://stackoverflow.com/questions/73413298
复制相似问题