2017-09-25 95 views
0

發送電子郵件在Synfony 3在需要幫助學習如何使用與Swift_mailer圖書館Symfony框架在開發模式下發送電子郵件。在開發模式

這裏是我迄今爲止

在config_dev.yml文件

swiftmailer: 
transport: '%mailer_transport%' 
host: '%mailer_host%' 
username: '%mailer_user%' 
password: '%mailer_password%' 
spool: { type: memory } 

在config.yml文件

swiftmailer: 
transport: '%mailer_transport%' 
host: '%mailer_host%' 
username: '%mailer_user%' 
password: '%mailer_password%' 
spool: { type: memory } 

在我parameters.yml文件

.... 
mailer_transport: gmail 
mailer_host: 127.0.0.1 
mailer_user: 'my_gamil_here' 
mailer_password: 'my_password_here' 

然後在我的騙局troller我有以下

public function indexAction(\Swift_Mailer $mailer) 
{ 



    $message = (new \Swift_Message('Hello Email')) 
    ->setFrom('[email protected]') 
    ->setTo('[email protected]') 
    ->setBody(
     $this->renderView(
      // app/Resources/views/Emails/registration.html.twig 
      'Emails/registration.html.twig', 
      array('name' => 'James') 
     ), 
     'text/html' 
    ); 

    $mailer->send($message); 

    return new Response('Email sent'); 
} 

} 

有人能告訴我,我什麼都錯了。沒有什麼會被髮送到電子郵件帳戶。

回答

0
  1. 確保,即mailer_user設置只有你的Gmail用戶名(沒有主機部分)。比方說,如果我有電子郵件[email protected],我的用戶名是myemail

  2. 您在使用兩步驗證爲您的Gmail帳戶?如果是的話,還需要爲您的帳戶生成的應用程序的密碼,如docs規定:

如果您的Gmail帳戶使用兩步驟驗證,您必須generate an App password並把它作爲的價值mailer_password參數。你還必須確保你allow less secure apps to access your Gmail account

不久,你可以按照here,登錄,選擇應用程序(在這種情況下Mail),並要使用郵件用,按Generate的設備,你會看到模式與您的應用程序的密碼,與此類似snub udpz xcvt jrdp。複製此密碼,並將其用作您的Gmail密碼(不含空格)。

如果這沒有幫助,還檢查日誌,一般如果沒有與SMTP服務器,swiftmailer打印錯誤日誌的一個問題。

+0

這工作我的朋友。非常感謝你! – Kaley36

0

'mailer_transport' 是發送郵件的protocole。
嘗試使用'smtp'代替'gmail':)

1

也許是因爲您指定了郵件主機爲localhost。檢查這個symfony文檔http://symfony.com/doc/current/email/gmail.html。並注意這部分:

Gmail的交通很簡單,使用SMTP傳輸 並設置這些選項的快捷方式:

encryption ssl 

auth_mode login 

host smtp.gmail.com