2016-05-12 97 views
0

對不起,我是新手,我使用laravel 5.1項目,我想用電子郵件服務發送電子郵件。Laravel郵件服務

我用山魈之前,但它的付費郵件服務現在,隨着mailchimp連接,

,所以我尋找另一種解決方案是mailgun,當我創建mailgun帳戶,這是需要被激活,我不能發送電子郵件它。

我設置Gmail SMTP在我的本地項目,它的成功發送電子郵件,但是當我推設置到我的服務器時,它表明這個錯誤:

Connection could not be established with host smtp.gmail.com [ #0] 

我不知該電子郵件服務我必須使用容易設置和易於使用?

+0

請看'http:// www.mailgun.com /'。它的方式更容易使用 – Sid

+0

** sendgrid **郵件服務很簡單。我試過了。你可以參考我的回購:https://github.com/YasinPatel/send-email-in-laravel-using-sendgrid –

回答

0

爲了安全起見,文件mail.php具有env變量形式的鍵值,您必須在一個名爲.env的單獨文件中進行設置(我將在下面進一步介紹一個示例),當然,您也可以使用你有帳號的免費郵件提供商,如Gmail,Hotmail服務,Yandex的等

首先這是在配置的mail.php文件

'driver' => env('MAIL_DRIVER', $_ENV['MAIL_DRIVER']), 

/* 
|-------------------------------------------------------------------------- 
| SMTP Host Address 
|-------------------------------------------------------------------------- 
| 
| Here you may provide the host address of the SMTP server used by your 
| applications. A default option is provided that is compatible with 
| the Mailgun mail service which will provide reliable deliveries. 
| 
*/ 

'host' => env('MAIL_HOST', $_ENV['MAIL_HOST']), 

/* 
|-------------------------------------------------------------------------- 
| SMTP Host Port 
|-------------------------------------------------------------------------- 
| 
| This is the SMTP port used by your application to deliver e-mails to 
| users of the application. Like the host we have set this value to 
| stay compatible with the Mailgun e-mail application by default. 
| 
*/ 

'port' => env('MAIL_PORT',$_ENV['MAIL_PORT']), 

/* 
|-------------------------------------------------------------------------- 
| Global "From" Address 
|-------------------------------------------------------------------------- 
| 
| You may wish for all e-mails sent by your application to be sent from 
| the same address. Here, you may specify a name and address that is 
| used globally for all e-mails that are sent by your application. 
| 
*/ 

'from' => ['address' => '[email protected]', 'name' => 'My App'], 

/* 
|-------------------------------------------------------------------------- 
| E-Mail Encryption Protocol 
|-------------------------------------------------------------------------- 
| 
| Here you may specify the encryption protocol that should be used when 
| the application send e-mail messages. A sensible default using the 
| transport layer security protocol should provide great security. 
| 
*/ 

'encryption' => env('MAIL_ENCRYPTION', 'ssl'), 

/* 
|-------------------------------------------------------------------------- 
| SMTP Server Username 
|-------------------------------------------------------------------------- 
| 
| If your SMTP server requires a username for authentication, you should 
| set it here. This will get used to authenticate with your server on 
| connection. You may also set the "password" value below this one. 
| 
*/ 

'username' => env('MAIL_USERNAME', $_ENV['MAIL_USERNAME']), 

/* 
|-------------------------------------------------------------------------- 
| SMTP Server Password 
|-------------------------------------------------------------------------- 
| 
| Here you may set the password required by your SMTP server to send out 
| messages from your application. This will be given to the server on 
| connection so that the application will be able to send messages. 
| 
*/ 

'password' => env('MAIL_PASSWORD', $_ENV['MAIL_PASSWORD']), 

/* 
|-------------------------------------------------------------------------- 
| Sendmail System Path 
|-------------------------------------------------------------------------- 
| 
| When using the "sendmail" driver to send e-mails, we will need to know 
| the path to where Sendmail lives on this server. A default path has 
| been provided here, which will work well on most of your systems. 
| 
*/ 

'sendmail' => '/usr/sbin/sendmail -bs', 

]。

這裏是.ENV文件

MAIL_DRIVER=smtp 
MAIL_HOST='smtp.gmail.com' 
MAIL_PORT='465' 
MAIL_USERNAME='[email protected]' 
MAIL_PASSWORD='yourpassword' 
MAIL_ENCRYPTION=null 

然後在你的控制器,你需要寫郵件代碼,像這樣。正如你所看到的,我已經包含了一張圖片,其中的名字是從表單發送來的變量中獲取的,但是您可以通過硬編碼字符串替換它們以進行測試。

enter image description here

你需要的最後一件事是實際發送到收件人的電子郵件,其中,在這種情況下,我們呼籲,「郵寄」,可以是這樣的觀點:

enter image description here