以下是我的環境描述,因爲它有點特別: 我使用Windows 8.1 PC結合使用Vagrant運行的虛擬機。它基本上是一臺Debian 64位機器。 這個虛擬機實際上是我的服務器。嘗試使用SwiftMailer發送帶有Symfony 2的電子郵件:無法與主機smtp.gmail.com建立連接[#0]
然後,我爲我的項目使用Symfony 2,我試圖使用SwiftMailer和gmail(使用我自己的gmail地址)來發送電子郵件,這要歸功於聯繫表單(用戶的電子郵件地址,用戶和郵件內容)。
我的行爲將填寫表單中給出的電子郵件數據併發送郵件。 順便說一下,它通過使用 - > isValid()方法來檢查數據是否有效。
我的問題來了,我提交表單後,我收到來自SwiftMailer一個例外:連接無法與主機smtp.gmail.com建立[#0]
這裏是我的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
「%gmail_user%」和stuff是在一個parameters.yml文件中定義的。這裏的內容:
parameters:
mailer_transport: gmail
mailer_user: null
mailer_password: null
gmail_user: [email protected]
gmail_password: my_gmail_passwd
很多在整個互聯網搜索之後,我嘗試了很多東西從這裏或從其他地方,並沒有什麼做的工作:使用TLS,試圖端口587,隨着IP嘗試smtp.gmail.com服務器給了我同樣的錯誤。
的SSL擴展似乎也啓用:
php --info | grep openssl
OpenSSL support => enabled
,並告訴OpenSSL版本一些其他的東西。
我檢查了我的IMAP設置以及啓用或禁用應用程序訪問我的Gmail的一般設置。
我也試過telnet smtp.google.com 465
,並且自從我達到連接後它工作正常。
我只是不知道該怎麼做。我希望你們中的一些人有同樣的問題,可以幫助我。
祝您有美好的一天!
編輯:問題解決,一個新出現
經過進一步調查,我隨機試圖改變config_dev.yml文件方式:
transport: mail
這解決了我的問題與SwiftMailerException ,儘管郵件沒有到達delivery_address電子郵件地址,即使我將其更改爲另一個我擁有的地址(一個hotmail郵件地址)。 我會努力工作,但我希望我的解決方案能夠提供幫助。
編輯#2:試圖解決發送問題,但沒有任何工作。
這將是一本書.. 我試圖在config.yml加入這個補充卷軸系統:
spool: {type: memory }
郵件似乎因爲探查告訴我,他把它交給發送。 我將此添加到我的控制器發送所有可能被纏繞的郵件:
$spool = $transport->getSpool();
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
我還添加了這個檢查,如果它真的發:
if ($this->mailer->send($message)) {
echo '[SWIFTMAILER] sent email to ' . $toEmail;
} else {
echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
}
我真的迷路了,不知道該怎麼做..
這裏是一個發送郵件在控制器代碼:
$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();
}
這總是告訴我'發送郵件',但我從來沒有收到它。
你是說用'運輸:mail'允許您發送電子郵件?這有點奇怪。你可以發佈創建和發送消息的控制器嗎? – George
使用'transport:mail'而不是'transport:gmail'解決了SwiftMailer異常。但是我仍然沒有收到郵件,但我認爲它來自線軸。 – AndoniLarz
你需要在你的配置'spool:{type:memory}'中手動添加spool。嘗試添加,看看它是否工作 – George