【发布时间】: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/],但我从未收到邮件