简体   繁体   English

尝试使用SwiftMailer发送带有Symfony 2的电子邮件:无法与主机smtp.gmail.com [#0]建立连接

[英]Trying to send emails with Symfony 2 using SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]

Here's a description of my environment first, since it's a bit particular : I use a Windows 8.1 PC combined with a virtual machine running with Vagrant . 这里首先描述我的环境,因为它有点特别:我使用Windows 8.1 PC和运行Vagrant的虚拟机。 It's basically a Debian 64bits machine. 它基本上是Debian 64bits机器。 This VM is in fact my server. 这个VM实际上是我的服务器。

Then, I use Symfony 2 for my project and I'm trying to use SwiftMailer with gmail (using my own gmail adress) in order to send emails thanks to a contact form (email address of the user, name of the user and content of the mail). 然后,我使用Symfony 2作为我的项目,我正在尝试使用SwiftMailer和gmail(使用我自己的gmail地址),以便通过联系表单(用户的电子邮件地址,用户名和内容)发送电子邮件。邮件)。

My action fills the email data with the ones given in the form and sends the mail. 我的操作使用表单中给出的内容填充电子邮件数据并发送邮件。 Btw, it checks if the data is valid by using the ->isValid() method. 顺便说一句,它使用 - > isValid()方法检查数据是否有效。

My problem comes after I submit the form, I receive an Exception from SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0] 我提交表单后出现问题,我从SwiftMailer收到异常:无法与主机smtp.gmail.com [#0]建立连接

Here's my config_dev.yml : 这是我的config_dev.yml:

swiftmailer:
    transport: gmail
    username:  "%gmail_user%"
    password:  "%gmail_password%"
    delivery_address: "%gmail_user%"
    encryption: ssl
    host: smtp.gmail.com
    port: 465
    auth_mode: login

The "%gmail_user%" and stuff is defined in a parameters.yml file. “%gmail_user%”和stuff在parameters.yml文件中定义。 Here's the content : 这是内容:

parameters:
mailer_transport: gmail
mailer_user: null
mailer_password: null
gmail_user: my_gmail_address@gmail.com
gmail_password: my_gmail_passwd

After a lot of search through the whole Internet, I've tried many things from here or from elsewhere, and nothing did work : Using tls, trying port 587, trying with the IP of the smtp.gmail.com server gave me the same error. 经过整个互联网的大量搜索后,我从这里或其他地方尝试了很多东西,没有任何工作:使用tls,尝试端口587,尝试使用smtp.gmail.com服务器的IP给了我同样的东西错误。

The SSL extension seems to be enabled too : SSL扩展似乎也启用了:

php --info | grep openssl
OpenSSL support => enabled

and some other stuff that tells the version of OpenSSL. 以及其他一些告诉OpenSSL版本的东西。

I checked my IMAP settings and the general setting that enables or disables applications to access to my gmail, it is enabled. 我检查了我的IMAP设置以及启用或禁用应用程序访问我的Gmail的常规设置,它已启用。

I also tried to telnet smtp.google.com 465 and it worked fine since I reached to connect. 我还试图telnet smtp.google.com 465 ,自从我联系到它以来它运行良好。

I just don't know what to do now. 我现在不知道该怎么做。 I hope some of you had the same issue and can help me. 我希望你们中的一些人有同样的问题,可以帮助我。

Have a nice day ! 祝你今天愉快 !

Edit : Problem solved and a new one appeared 编辑:问题解决了,出现了一个新问题

After further investigation, I randomly tried to change the config_dev.yml file that way : 经过进一步调查后,我随机尝试以这种方式更改config_dev.yml文件:

transport: mail

This solved my problem with the SwiftMailerException, though the mail doesn't come to the delivery_address email address even if I change it to another address I own (an hotmail one). 这解决了SwiftMailerException的问题,尽管邮件没有到达delivery_address电子邮件地址,即使我将其更改为我拥有的另一个地址(一个hotmail)。 I'll work on it, but I hope my solution can help. 我会继续努力,但我希望我的解决方案可以提供帮助。

Edit #2 : Tried to solve the sending problem but nothing worked. 编辑#2:尝试解决发送问题但没有任何效果。

This is going to be a book.. I tried to add a spool system by adding this in the config.yml : 这将是一本书..我试图通过在config.yml中添加这个来添加一个假脱机系统:

spool: {type: memory }

The mail seem to be sent since the profiler tells me he sent it. 邮件似乎是在发送者告诉我他发送邮件之后发送的。 I added this to my controller to send all the mails that could be spooled : 我将此添加到我的控制器以发送可能被假脱机的所有邮件:

$spool = $transport->getSpool();
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));

I also added this to check if it's really sent : 我还添加了这个来检查它是否真的发送过:

if ($this->mailer->send($message)) {
        echo '[SWIFTMAILER] sent email to ' . $toEmail;
    } else {
        echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
    }    

I am really lost and don't know what to do.. 我真的迷路了,不知道该怎么办..

Here's the code that send the mail in the controller : 这是在控制器中发送邮件的代码:

$message = \Swift_Message::newInstance()
        ->setSubject("The subject from the form")
        ->setFrom("The address from the form")
        ->setTo("The address which comes from an entity")
        ->setBody("The body from the form made with a twig template");

        $mailer = $this->container->get('mailer');

        $mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
        $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));

        if ($mailer->send($message)) {
            echo 'sent email';
        } else {
            echo 'not sending email: ' . $mailLogger->dump();
        }

This always tells me 'sent email', but I never receive it. 这总是告诉我“发送电子邮件”,但我从未收到过。

Did you try to make a connection with cURL? 您是否尝试与cURL建立连接? I had something similar with elasticsearch (elastica), apparently it used a cURL HTTP proxy I didn't know about because it was configured in php.ini. 我有类似的弹性搜索(elastica),显然它使用了我不知道的cURL HTTP代理,因为它是在php.ini中配置的。

off-topic: You can also try if mailcatcher works for your needs. 偏离主题:您也可以尝试使用mailcatcher满足您的需求。

I had the same error ( Connection could not be established with host smtp.gmail.com [ #0] ) and the reason was in antivirus (Avast!). 我有同样的错误( Connection could not be established with host smtp.gmail.com [ #0] ),原因是防病毒(Avast!)。 When I disabled it error has gone. 当我禁用它时,错误已经消失。

I found the solution and never answered here but since this question has like 2000+ views I feel like it's important. 我找到了解决方案并且从未在这里回答,但由于这个问题有2000多个观点,我觉得这很重要。

So, everything was fine and the only thing I didn't know about was that during development (aka when in dev environment), swiftmailer seems to disable delivery, as told here : http://symfony.com/doc/current/email/dev_environment.html#disabling-sending 所以,一切都很好,我唯一不知道的是在开发期间(也就是在dev环境中),swiftmailer似乎禁用了交付,如下所述: http ://symfony.com/doc/current/email /dev_environment.html#disabling-sending

All I had to do was to set this in config_dev.yml and config_test.yml files : 我所要做的就是设置这config_dev.ymlconfig_test.yml文件:

swiftmailer:
    disable_delivery: false

Just be careful to which address your mails go. 小心你的邮件去哪个地址。 For example, on this project, it would have sent dummy mails to our clients which would have been quite problematic. 例如,在这个项目上,它会向我们的客户发送虚假邮件,这可能会有很大问题。

To prevent this, you can do what's told in the symfony documentation ( http://symfony.com/doc/2.8/email/dev_environment.html#sending-to-a-specified-address-es ) 为了防止这种情况,您可以执行symfony文档中的说明( http://symfony.com/doc/2.8/email/dev_environment.html#sending-to-a-specified-address-es

# app/config/config_dev.yml
swiftmailer:
    delivery_addresses: ['dev@example.com']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法与SwiftMailer中的主机smtp.gmail.com [#0]建立连接 - Connection could not be established with host smtp.gmail.com [#0] in SwiftMailer 无法使用主机smtp.gmail.com建立Symfony2 SwiftMailer连接 - Symfony2 SwiftMailer Connection could not be established with host smtp.gmail.com Swiftmailer:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110] - Swiftmailer: Connection could not be established with host smtp.gmail.com [Connection timed out #110] 无法与主机smtp.gmail.com [#0]建立连接 - Connection could not be established with host smtp.gmail.com [ #0] 无法与主机smtp.gmail.com建立连接 - Connection could not be established with host smtp.gmail.com Laravel 在Windows 7中无法使用主机smtp.gmail.com建立Laravel 5连接 - Laravel 5 Connection could not be established with host smtp.gmail.com in windows 7 无法与主机 smtp.gmail.com 建立连接 - Connection could not be established with host smtp.gmail.com 无法在 laravel 5.6 电子邮件发送中与主机 smtp.gmail.com [连接被拒绝 #111] 建立连接 - Connection could not be established with host smtp.gmail.com [Connection refused #111] in laravel 5.6 email send Connection could not be established with host smtp.gmail.com:stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 - Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 如何在实时服务器上的 Laravel 5.4 中发送电子邮件 Swift_TransportException 无法与主机 smtp.gmail.com 建立连接 - how to send email in laravel 5.4 on live server Swift_TransportException Connection could not be established with host smtp.gmail.com
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM