Laravel 7 配置郵件服務 Mailgun

Laravel 7 配置郵件服務 Mailgun

2016年的時候,Mailgun 免費註冊帳號還能添加自定義域名(當時Y Cheung 曾寫過一篇blog:Centos7 安裝 Postfix 並配置 mailgun 服務),想不到現在新註冊帳號已經是需要先綁定銀行卡才能使用自定義域名,而註冊分配給你的測試域名Sandbox subdomain壓根就不能用,你會遇到類似以下的報錯信息:

{   "message": "Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authoriz (truncated...)  

Y Cheung後悔當年沒多綁定幾個域名啊哎~綁銀行卡什麼的好麻煩啊,而且很危險⋯⋯去年Y Cheung 就遇到過booking.com綁定的銀行卡被盜刷的問題,還好後來追回錢來了。

那總之,現在在Laravel 7 上配置 Mailgun 郵件服務的流程是:

  • 註冊Mailgun帳號
  • 在Mailgun添加銀行卡信息
  • 在Mailgun添加新自定義域名
  • 在Nameserver上配置域名相關DNS信息(Mailgun上有顯示具體內容)
  • 在Mailgun上驗證域名DNS配置
  • 在Laravel 環境配置文件.env中添加MAILGUN_DOMAINMAILGUN_SECRET變量,domain就是你剛才在mailgun添加的自定義域名,secret就是這個自定義域名的 API key。
  • 確認Laravel的config/services.php文件中有以下代碼段
    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
    ],
  • 在Laravel 的config/mail.php文件中的 Mailer Configurations添加以mailgun配置:
    'mailers' => [
...
        'mailgun' => [
            'transport' => 'mailgun',
        ]

That's all.

Y Cheung

Y Cheung

Blogger, Programer & Traveler.
Shanghai