邮件 symfony 4 不工作
mailer symfony 4 not working
我使用 symfony 4 启动了一个项目,但邮件程序不起作用,但它应该很容易。
在你问之前,如果我从我的代码中复制过去的登录名和密码,我就可以登录我的邮件帐户,我也尝试使用 netcourrier 邮件帐户,而且 2 方式身份验证未激活,我允许不太安全的应用程序访问邮件帐户。
这是我的会议:
在我的 .env:
MAILER_URL=gmail://*******@gmail.com:********@localhost
在我的控制器中:
public function contact( \Swift_Mailer $mailer){
$message = (new \Swift_Message('Hello Email'))
->setFrom('*****@gmail.com')
->setTo('*******@gmail.com')
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'email/registration.html.twig',
array('url' => $url)
),
'text/html'
);
$mailer->send($message);
return $this->render(
'email/index.html.twig');}
我这样做的错误是:
Connection could not be established with host smtp.gmail.com [ #0]
我认为这不是邮件程序的问题,而是 gmail 的问题。我复制了你的步骤试图通过gmail的smtp服务器连接,但得到了同样的错误。当在 .env 文件中使用不同的 MAILER_URL (不同的 smtp-server)时,一切正常。
问题是您与 google 的 SMTP 连接,这是正确的:
MAILER_URL=smtp://smtp.gmail.com:587?encryption=tls&username=userGmail&password=PassGmail
我在App/Services
中将其定义为服务,这是代码
<?php
namespace App\Services;
class Enviomail {
private $mailer;
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function sendEmail($to, $subject, $texto) {
$message = (new \Swift_Message($subject))
->setFrom('juanitourquiza@gmail.com')
->setTo($to)
->setBody(($texto),'text/html');
return $this->mailer->send($message);
}
}
为了使用它,我从控制器调用它
use App\Services\Enviomail;
........
public function mailsolucion(Request $request, Enviomail $enviomail) {
if ($request->isMethod('POST')) {
$nombre=$request->get("nombre");
$email=$request->get("email");
$numero=$request->get("numero");
$empresa=$request->get("empresa");
$solucion=$request->get("solucion");
if (($nombre=="")||($email=="")||($numero=="")||($empresa=="")){
$this->addFlash(
'alert alert-danger',
'Toda la información es obligatoria'
);
return $this->redirectToRoute('registro');
}
$emailreceptor="juanitourquiza@gmail.com";
$asunto="Soluciones gruporadical.com";
$texto=$this->renderView(
'emails/soluciones.html.twig',
array(
'nombre' => $nombre,
'email' => $email,
'numero' => $numero,
'empresa' => $empresa,
'solucion' => $solucion,
)
);
$enviomail->sendEmail($emailreceptor,$asunto, $texto);
$this->addFlash(
'alert alert-success',
'Pronto nos pondremos en contacto.'
);
return $this->redirectToRoute('registro');
}
return $this->render('AppBundle:App:contacto.html.twig');
}
在 Symfony 上完美运行 4.x
我使用 symfony 4 启动了一个项目,但邮件程序不起作用,但它应该很容易。 在你问之前,如果我从我的代码中复制过去的登录名和密码,我就可以登录我的邮件帐户,我也尝试使用 netcourrier 邮件帐户,而且 2 方式身份验证未激活,我允许不太安全的应用程序访问邮件帐户。 这是我的会议: 在我的 .env:
MAILER_URL=gmail://*******@gmail.com:********@localhost
在我的控制器中:
public function contact( \Swift_Mailer $mailer){
$message = (new \Swift_Message('Hello Email'))
->setFrom('*****@gmail.com')
->setTo('*******@gmail.com')
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'email/registration.html.twig',
array('url' => $url)
),
'text/html'
);
$mailer->send($message);
return $this->render(
'email/index.html.twig');}
我这样做的错误是:
Connection could not be established with host smtp.gmail.com [ #0]
我认为这不是邮件程序的问题,而是 gmail 的问题。我复制了你的步骤试图通过gmail的smtp服务器连接,但得到了同样的错误。当在 .env 文件中使用不同的 MAILER_URL (不同的 smtp-server)时,一切正常。
问题是您与 google 的 SMTP 连接,这是正确的:
MAILER_URL=smtp://smtp.gmail.com:587?encryption=tls&username=userGmail&password=PassGmail
我在App/Services
中将其定义为服务,这是代码
<?php
namespace App\Services;
class Enviomail {
private $mailer;
public function __construct(\Swift_Mailer $mailer)
{
$this->mailer = $mailer;
}
public function sendEmail($to, $subject, $texto) {
$message = (new \Swift_Message($subject))
->setFrom('juanitourquiza@gmail.com')
->setTo($to)
->setBody(($texto),'text/html');
return $this->mailer->send($message);
}
}
为了使用它,我从控制器调用它
use App\Services\Enviomail;
........
public function mailsolucion(Request $request, Enviomail $enviomail) {
if ($request->isMethod('POST')) {
$nombre=$request->get("nombre");
$email=$request->get("email");
$numero=$request->get("numero");
$empresa=$request->get("empresa");
$solucion=$request->get("solucion");
if (($nombre=="")||($email=="")||($numero=="")||($empresa=="")){
$this->addFlash(
'alert alert-danger',
'Toda la información es obligatoria'
);
return $this->redirectToRoute('registro');
}
$emailreceptor="juanitourquiza@gmail.com";
$asunto="Soluciones gruporadical.com";
$texto=$this->renderView(
'emails/soluciones.html.twig',
array(
'nombre' => $nombre,
'email' => $email,
'numero' => $numero,
'empresa' => $empresa,
'solucion' => $solucion,
)
);
$enviomail->sendEmail($emailreceptor,$asunto, $texto);
$this->addFlash(
'alert alert-success',
'Pronto nos pondremos en contacto.'
);
return $this->redirectToRoute('registro');
}
return $this->render('AppBundle:App:contacto.html.twig');
}
在 Symfony 上完美运行 4.x