Symfony6如何正确配置sendmail从localhost发送电子邮件

3htmauhk  于 2023-10-24  发布在  其他
关注(0)|答案(3)|浏览(111)

基本上在我家的Ubuntu机器上,我希望能够在我的symfony应用程序中从localhost发送电子邮件。我已经安装了sendmail及其基本配置。我可以使用命令行发送电子邮件:sendmail -v命令。电子邮件被接收。我甚至可以使用PHP函数发送电子邮件:

$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]";

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 = [email protected]

; 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 = "[email protected]";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: [email protected]";

    mail($to,$subject,$txt,$headers); // <----- THIS IS WORKING!!!

    $subject = 'My subject';

    $email = (new TemplatedEmail());
    $email->from(new Address('[email protected]'));
    $email->to('[email protected]');
    $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 ###

他们都没有工作。任何想法是赞赏!

ijnw1ujt

ijnw1ujt1#

所以显然一切都很好.问题是,如果你安装symfony与他们的安装程序使用--webapp选项,队列信使安装:https://symfony.com/doc/current/messenger.html和我所有的电子邮件在队列中.删除信使修复了我的问题.谢谢!

oaxa6hgo

oaxa6hgo2#

调试$this->mailer,例如,首先使用dd(),如果它没有帮助......实际上进行适当的调试。
此外,请记住,有时清除缓存可以帮助
php bin/console cache:clear

9lowa7mx

9lowa7mx3#

我不得不更改我的.env.local const以使用其中一个docker容器:

  1. MAILER_DSN=smtp://mailhog:1025
  2. MAILER_DSN=smtp://mailer:1025
    其中mailhogmailer是docker-compose.yml中docker容器的名称:
mailer:
        image: schickling/mailcatcher
        container_name: app-mailer
        ports:
            - "1080:1080" # web
            - "1025:1025" # smtp

    mailhog:
        image: mailhog/mailhog:latest
        container_name: app-mailhog
        ports:
            - "8025:8025"

相关问题