2014-09-24 61 views
1

花了兩天的時間試圖讓它工作,我終於決定在這裏問。我試圖在用戶提交聯繫表單後生成一封電子郵件,在閱讀Symfony website上的文檔以及無數其他關於stackoverflow和google的文章後,我只是無法使其工作。看看這個指令,它看起來應該是非常直接的,但沒有一個解決方案正在工作。我試圖通過Gmail,並通過我們公司SMTP發送郵件,但都沒有奏效SwiftMailer不發送Symfony 2.5中的電子郵件

這是我目前parameters.yml

swiftmailer: 
    transport:   smtp 
    username:    username (confirmed that i am entering it right) 
    password:    password (confirmed that i am entering it right) 
    host:     mail.domain.com 
    port:     26 
    auth_mode:   login 

以上設置不給任何錯誤,它只是提交表單,並顯示成功消息但沒有郵件收到

我甚至嘗試了其他幾種使用Gmail的組合,但這會返回用戶身份驗證錯誤,我一遍又一遍地確保我輸入正確的登錄詳細信息並100%確定它們是正確,因爲通過瀏覽器我可以登錄。

config.yml:

swiftmailer: 
    transport: %mailer_transport% 
    encryption: %mailer_encryption% 
    auth_mode: %mailer_auth_mode% 
    host:  %mailer_host% 
    username: %mailer_username% 
    password: %mailer_password% 
    spool:  { type: memory } 

parameters.yml:

mailer_transport: smtp 
mailer_encryption: ssl 
mailer_auth_mode: login 
mailer_host:  smtp.gmail.com 
mailer_username: [email protected] 
mailer_password: mypassword 

這是我的控制器看起來像它生成電子郵件

public function indexAction(Request $request) 
    { 
     $enquiry = new Enquiry(); 
     $form = $this->createForm(new EnquiryType(), $enquiry); 

     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $message = \Swift_Message::newInstance() 
       ->setSubject('Contact enquiry') 
       ->setFrom('[email protected]') 
       ->setTo('[email protected]') 
       ->setBody(
        $this->renderView('ContactBundle:Default:contactEmail.txt.twig', array('enquiry' => $enquiry)) 
       ); 
      //print_r($message); 
      $this->get('mailer')->send($message); 
      $this->get('session')->getFlashBag()->add(
       'notice', 
       'Your contact enquiry was successfully sent. Thank you!' 
      ); 

      return $this->redirect($this->generateUrl('contact_form')); 
     } 

     return $this->render(
      'ContactBundle:Default:contact.html.twig', 
      array(
       'form' => $form->createView() 
      ) 
     ); 

    } 

我會真的很感激如果我現在可以得到一些幫助,因爲我現在完全無能爲力。

+0

首先,使用symfony控制檯檢查您的配置。其次,如果您使用gmail.com發送郵件,請更改傳輸 – jamek 2014-09-24 13:58:11

+0

我可以在profiler中看到郵件,我甚至可以在config.yml中添加'delivery_address',這是爲了測試電子郵件,但它不工作時,我刪除它 – Baig 2014-09-24 14:03:16

+0

當試圖通過Gmail發送郵件但我沒有幫助 – Baig 2014-09-24 14:04:45

回答

7

我得到了它的兩個公司基於SMTP和Gmail

工作,爲公司SMTP

config.yml:

swiftmailer: 
    transport: %mailer_transport% 
    encryption: %mailer_encryption% 
    auth_mode: %mailer_auth_mode% 
    host:  %mailer_host% 
    username: %mailer_username% 
    password: %mailer_password% 
    spool:  { type: memory } 

parameters.yml:

mailer_transport: smtp 
    mailer_host: mail.domain.com 
    mailer_user: [email protected] 
    mailer_password: password 

對於Gmail config.yml設置保持不變,但在parameters.yml做如下改變

mailer_transport: gmail 
    mailer_host: smtp.gmail.com 
    mailer_user: [email protected] 
    mailer_password: password 

然後訪問該link谷歌的允許應用程序登錄到您的帳戶發送郵件。

在你的控制器,你是在告訴函數來發送電子郵件->setFrom('[email protected]')應匹配的電子郵件ID被生成的電子郵件,否則你: 您也可以使用此link

注意查看登錄的任何suspecious活動將不會收到一封電子郵件,我看過這個並浪費了2天時間。

$message = \Swift_Message::newInstance() 
       ->setSubject('Contact enquiry') 
       ->setFrom('[email protected]') \\match this to mailer_user in parameters.yml 
       ->setTo('[email protected]') 
       ->setBody(
        $this->renderView('ContactBundle:Default:contactEmail.txt.twig', array('enquiry' => $enquiry)) 
       ); 

這就是我如何得到它的工作:)最後,希望這會幫助那裏的人卡住。我可以確認此解決方案適用於我在開發和生產服務器上。

+1

*「應該與生成電子郵件的電子郵件ID匹配」*這是您的SMTP服務器的限制,而不是SwiftMailer。如果您的SMTP服務器允許欺騙(例如Mandrill,SendGrid等),那麼'username'和'from地址'是絕對正確的。 Swiftmailer的調試標誌在這裏非常有用,您的日誌也可以。 – 2014-09-24 16:13:40

+1

隨着您的回答,最能幫助我的是您所做的評論:「將此匹配到parameters.yml中的mailer_user」。這是不工作的真正原因。 – 2017-02-03 06:11:22

相關問題