【问题标题】:Expected response code "250/251/252" but got code "530", with message "530 SMTP authentication is required."预期响应代码“250/251/252”,但得到代码“530”,消息“需要 530 SMTP 身份验证”。
【发布时间】:2022-01-10 11:56:29
【问题描述】:
###> symfony/mailer ###
MAILER_DSN=smtp://localhost
###< symfony/mailer ###

这是我的 .env 的一部分
我正在尝试在用户注册后发送电子邮件
但我不知道在 MAILER DSN 中输入什么,我收到此错误
THE ERROR 最后但并非最不重要的是这是我的邮件服务

<?php

namespace App\Service;

use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;

class Mailer{

    /**
     * @var MailerInterface
     */
    private $mailer;

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

    public function sendMail($email, $token){
        $email = (new TemplatedEmail())
            ->from('Lost-found@foundonly.com')
            ->to(new Address($email))
            ->subject('Thanks for signing up! Just one more thing to do')

            // path of the Twig template to render
            ->htmlTemplate('emails/signup.html.twig')

            // pass variables (name => value) to the template
            ->context([
                'token' => $token,
            ])
        ;

        $this->mailer->send($email);
    }
}

最后是寄存器控制器

<?php
namespace App\Controller;

use App\Entity\User;
use App\Form\RegisterType;
use App\Service\Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;


class RegisterController extends AbstractController
{
    /**
     * @var UserPasswordEncoderInterface
     */
    private $passwordEncoder;


    /**
     * @var Mailer
     */
    private $mailer;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder, Mailer $mailer)
    {
        $this->passwordEncoder = $passwordEncoder;
        $this->mailer = $mailer;
    }

    /**
     * @Route("/signup", name="signup")
     * @throws \Exception
     */
    public function register(Request $request): Response
    {
        $user = new User();
        $form = $this->createForm(RegisterType::class,$user);
        $form->handleRequest($request);

        if($form->isSubmitted()&&$form->isValid()){
            $user->setPassword(
                $this->passwordEncoder->encodePassword($user,$form->get("password")->getData())
            );
            $user->setToken($this->generateToken());
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            $this->mailer->sendMail($user->getEmail(),$user->getToken());
            $this->addFlash("success", "you are more than welcome into our community, just one more step | Check your mail please");
        }//37.12

        return $this->render('register/register.html.twig',[
            'form' => $form->createView()
        ]);
    }

    /**
     * @throws \Exception
     */
    private function generateToken(): string
    {
        return rtrim(strtr(base64_encode(random_bytes(32)),'+/','-_'),'=');
    }
}

?>

所以请任何人都可以在这里帮助我吗?我真的不知道在 mailer_dsn 中放什么

【问题讨论】:

  • 现在我收到了这个新错误:无法与主机“localhost:25”建立连接:stream_socket_client(): Unable to connect to localhost:25 (Aucune connexion n'a pu être établie car l'ordinateur cible l'a expressément refusée)
  • 如果您没有 SMTP 服务器并且想在本地环境中进行一些测试,您应该将 dot env 文件中的 MAILER_DSN=smtp://localhost 替换为 MAILER_DSN=null://localhost
  • 我将邮件发送到为该站点生成的临时电子邮件,她的链接是 [temp-mail.org/fr/],但我从未收到邮件

标签: symfony mailer


【解决方案1】:

您需要正确配置您的 Mailer 传输。它可以是 SMTP 服务器或本地 sendmail 二进制文件。

如果您不想打扰 sendmail 而更喜欢使用 SMTP 传输,最简单的解决方案是使用带有 symfony/google-mailer 组件的 gmail smtp 服务器

更多信息: Sending Emails with Mailer

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2017-07-23
    • 2020-08-20
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多