繁体   English   中英

SwiftMailer 不在 MessageHandler 中发送邮件

[英]SwiftMailer not sending mail in MessageHandler

我在我的 Symfony 5 项目中使用 SwiftMailer 来发送电子邮件。

我在 controller 中使用它来发送重置密码电子邮件,一切正常。 我现在正在尝试在 MessageHandler 中使用它,这是我现在使用的代码:

final class SendEmailMessageHandler implements MessageHandlerInterface
{
private $mailer;

public function __construct(\Swift_Mailer $mailer)
{
    $this->mailer = $mailer;
}

public function __invoke(SendEmailMessage $message)
{
    $mail = (new \Swift_Message())
        ->setFrom($message->getFrom())
        ->setTo($message->getTo())
        ->setBody($message->getBody(), $message->getContentType())
        ->setSubject($message->getSubject());
    $response = $this->mailer->send($mail);
}
}

回复没问题,但是邮件从来没有到达我的邮箱。

以下是我发送 SendEmailMessage 的方式:

class AskResetPassword extends AbstractController
{
use ResetPasswordControllerTrait;

private $resetPasswordHelper;
private $validator;
private $bus;

public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, ValidatorInterface $validator, MessageBusInterface $bus)
{
    $this->resetPasswordHelper = $resetPasswordHelper;
    $this->validator = $validator;
    $this->bus = $bus;
}

public function __invoke($data)
{
    $emailConstraints = new Assert\Email();

    $email = $data->getEmail();
    if ($email) {
        $errors = $this->validator->validate($email, $emailConstraints);
        if (count($errors) === 0) {
            return $this->processPasswordReset($email);
        } else {
            return new JsonResponse(['success' => false, 'error' => 'Invalid E-Mail format'], 404);
        }
    }
}

private function processPasswordReset($email)
{
    $user = $this->getDoctrine()->getRepository(User::class)->findOneBy([
        'email' => $email,
    ]);

    $this->setCanCheckEmailInSession();

    if (!$user) {
        // Do not reveal whether a user account was found or not.
        return new JsonResponse(['success' => true], 200);
    }

    try {
        $resetToken = $this->resetPasswordHelper->generateResetToken($user);
    } catch (ResetPasswordExceptionInterface $e) {
        return new JsonResponse(['success' => false, 'error' => 'There was a problem handling your password reset request - ' . $e->getReason()]);
    }
    $message = new SendEmailMessage($email);
    $message->setFrom('from.from@from.from');
    $message->setBody(
        $this->renderView('reset_password/email.html.twig', [
            'resetToken' => $resetToken,
            'tokenLifetime' => $this->resetPasswordHelper->getTokenLifetime()
        ])
    );
    $message->setSubject('Votre demande de changement de mot de passe');
    $this->bus->dispatch($message);

    return new JsonResponse(['success' => true], 200);
}
}

这是我的 swiftmailer.yaml:

swiftmailer:
    url: '%env(MAILER_URL)%'
    spool: { type: 'memory' }

你能帮助我吗?

我不记得确切原因以及在发布此内容时我正在努力寻找答案,但 swiftmailer 类型必须是文件而不是 memory。 这个GitHub 问题引用了这个。 您还可以在此处查看如何更改类型。

答案是“除非您想稍后处理它们,否则不要假脱机电子邮件”。 检查文档 假脱机电子邮件

假脱机程序是一种队列机制,它将逐个处理您的消息队列。 这是在 swift-mailer 被重写并添加回 symfony 时引入的。 结合提供抽象接口 MessageBusInterface 的 Messenger 组件,它将委托给正确的后端服务,该服务可以是 smtp 中继、推送通知或任何其他类型的 RPC,它们可能会触发对单独的 web 服务的操作。

由于 symfony 为消息总线添加了新功能,因此添加了此功能以将其用于消息队列和其他事务性电子邮件和通知单独处理的服务。

要处理您的线轴,只需运行:

APP_ENV=prod php bin/console swiftmailer:spool:send

在典型安装中禁用假脱机,当您在 memory 中启用假脱机时,它将等到请求完成并且 kernel 即将退出。 如果您使用其他任何东西来调试中途终止 kernel 或有其他组件和应用程序部分将 kernel 保留在 memory 中,则不会触发事件并发送邮件。

您可以在此处查看整个文档:发送电子邮件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM