我正在使用Symfony 4编写一个小型网站。 有一个简单的联系表单应该发送电子邮件,似乎很容易,直到我意识到我无法配置它^^
我从这里开始关注Symfony doc说明: [https://symfony.com/doc/current/email.html][1]
主要是让Swift邮件依赖的意思:
composer require mailer
我的控制器看起来像这样:
/**
* @Route("/contact", name="contact_handling")
*/
public function contactHandler(Request $request, \Swift_Mailer $mailer)
{
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$message = (new \Swift_Message('Hello Email'))
->setFrom('send@example.com')
->setTo('myownemail@hotmail.fr')
->setBody("plopppp mail")
;
$mailer->send($message);
$contact = $form->getData();
return $this->render('home.html.twig', array(
'form' => $form->createView(),
));
}
return $this->render('home.html.twig', array(
'form' => $form->createView(),
));
无论回复中的下一步做什么(我也试图弄清楚如何避免页面重新加载,只返回“OK”或“不行”等答案,然后我的Javascript弹出一条消息)< / p>
对于开发环境(顺便提一下,我会遇到同样的问题), 我的.env conf文件有这个参数:
MAILER_URL=smtp://smtp-mail.outlook.com:587?encryption=tls&username=myownemail@hotmail.fr&password=mypwd
在尝试使用我自己的电子邮件帐户时,这可能是我的问题
Smtp地址,端口和加密是网站上的一些参数,如下所示: [https://www.lifewire.com/what-are-the-outlook-com-smtp-server-settings-1170671][1]
当然,我从来没有收到任何东西。 如果有人熟悉这可以帮助我,那将是非常好的:)
我正在使用Windows10 + PhpStorm + php7 +内置symfony服务器
谢谢!
编辑: 输出来自:php bin / console debug:config SwiftmailerBundle
swiftmailer:
default_mailer: default
mailers:
default:
url: '%env(MAILER_URL)%'
spool:
type: memory
path: 'C:\www\h4h\var\cache\dev/swiftmailer/spool'
id: null
transport: smtp
command: '/usr/sbin/sendmail -bs'
username: null
password: null
host: localhost
port: null
timeout: 30
source_ip: null
local_domain: null
encryption: null
auth_mode: null
delivery_addresses: { }
logging: true
delivery_whitelist: { }
编辑2: 我刚刚尝试将conf放在config / packages / swiftmailer.yaml中而没有更多的成功,但至少,php bin / console debug:config SwiftmailerBundle输出正确的信息:
swiftmailer:
transport: gmail
username: mylogin
password: mypwd
host: smtp.gmail.com
port: 587
encryption: ssl
auth_mode: login
spool:
type: file
path: '%kernel.cache_dir%/swiftmailer/spool'
sender_address: ~
antiflood:
threshold: 99
sleep: 0
delivery_addresses: []
disable_delivery: ~
logging: '%kernel.debug%'