我正在尝试使用SwiftMailer和Symfony2发送电子邮件。
在我遇到Can't connect to smtp.gmail.com
问题之前,但知道,我没有任何错误,但消息仍未发送。
这是我的配置.yml:
swiftmailer:
transport: mail
encryption: ssl
host: smtp.gmail.com
port: 465
auth_mode: login
username: myUsername@gmail.com
password: myPassword
config_test.yml :
swiftmailer:
disable_delivery: false
要发送的控制器:
$message = 'Swift_Message::newInstance(null)
->setSubject('Test')
->setFrom('test@gmail.com')
->setTo('test@gmail.com')
->setBody('Test test test !!');
$this->get('mailer')->send($message);
我尝试了许多在互联网上找到的修复程序,但没有任何修复:/
我正在尝试使用Wamp本地发送它
编辑:
我已将传输设置为SMTP,现在,当我使用端口443时,我超时,如果我使用465,我只会再次获得"无法连接"。
编辑2:
我尝试使用"传输:gmail",但仍然"无法连接"消息这是我的配置:
transport: gmail
username: 'myEmail@gmail.com'
password: 'myPassword'
要使用 Gmail 发送邮件,您可以使用transport: gmail
如果您需要更多信息:http://symfony.com/doc/current/cookbook/email/gmail.html
如果它配置良好但不起作用,请检查您的安全环境(防火墙等)
在App/config/config.yml中写这个:
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
在 App/config/parameters.yml 中:
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: yourmail@gmail.com
mailer_password: your_mail_password
在控制器中:
public function sendMailAction() {
$Request= $this ->getRequest();
if($Request ->getMethod()== "POST"){
$name= $Request -> get("name");
$email = $Request -> get("email");
$sujet = $Request -> get("subject");
$message = $Request -> get("message");
$mailer = $this->container->get('mailer');
$transport = 'Swift_SmtpTransport::newInstance('smtp.gmail.com',465,'ssl')
->setUsername('******')
->setPassword('******');
$sms = 'Swift_Message::newInstance('Test')
->setSubject($sujet)
->setFrom('your_mail_here@gmail.com')
->setTo($email)
->setBody($message);
$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
$this->get('mailer')->send($sms);
}
return $this->render('SwiftMailSwiftMailBundle:Mail:contact.html.twig');
}
如果你愿意,请让我把完整的项目发给你。祝你好运