Email

Laravel 5.5 使用 Sendmail 驅動程序 CentOS 發送郵件 - (傳出)連接超時

  • August 6, 2019

我正在嘗試使用 sendmail 驅動程序發送電子郵件,但它不起作用並且我沒有收到任何錯誤。

我的.env文件:

MAIL_DRIVER=sendmail
MAIL_HOST=
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

我的mail.php文件:

<?php

return [

   /*
   |--------------------------------------------------------------------------
   | Mail Driver
   |--------------------------------------------------------------------------
   |
   | Laravel supports both SMTP and PHP's "mail" function as drivers for the
   | sending of e-mail. You may specify which one you're using throughout
   | your application here. By default, Laravel is setup for SMTP mail.
   |
   | Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
   |            "sparkpost", "log", "array"
   |
   */

   'driver' => env('MAIL_DRIVER', 'sendmail'),

   /*
   |--------------------------------------------------------------------------
   | 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', ''),

   /*
   |--------------------------------------------------------------------------
   | 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', 587),

   /*
   |--------------------------------------------------------------------------
   | 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' => env('MAIL_FROM_ADDRESS', 'no-reply@inspvirtual.mx'),
       'name' => env('MAIL_FROM_NAME', 'INSP Virtual'),
   ],

   /*
   |--------------------------------------------------------------------------
   | 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', 'tls'),

   /*
   |--------------------------------------------------------------------------
   | 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', null),

   'password' => env('MAIL_PASSWORD', null),

   /*
   |--------------------------------------------------------------------------
   | 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',

   /*
   |--------------------------------------------------------------------------
   | Markdown Mail Settings
   |--------------------------------------------------------------------------
   |
   | If you are using Markdown based email rendering, you may configure your
   | theme and component paths here, allowing you to customize the design
   | of the emails. Or, you may simply stick with the Laravel defaults!
   |
   */

   'markdown' => [
       'theme' => 'default',

       'paths' => [
           resource_path('views/vendor/mail'),
       ],
   ],

];

由於我沒有錯誤,即使在 laravel.log 文件中我什至不知道從哪裡開始檢查,但我已經檢查了正確的 sendmail 路徑,而且它是正確的,我錯過了什麼?

我還讀到您必須更改 /etc/hosts 中的一些內容我對其進行了一些更改,添加了“inspvirtual.mx”,但我不太確定這是否正確:

127.0.0.1   inspvirtual.mx
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

在我嘗試發送電子郵件後,郵件日誌會列印出來:

May 26 14:12:35 inspvirtual sendmail[3130]: x4Q9BDjB010067: to=<aaron@juicyboom.com>, delay=09:01:22, xdelay=00:00:00, mailer=esmtp, pri=1020554, relay=mail.juicyboom.com., dsn=4.0.0, stat=Deferred: Connection timed out with mail.juicyboom.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q9BWIJ010111: to=<aaron@juicyboom.com>, delay=09:01:03, xdelay=00:00:00, mailer=esmtp, pri=1020554, relay=mail.juicyboom.com., dsn=4.0.0, stat=Deferred: Connection timed out with mail.juicyboom.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q9CdaV010235: to=<aaron@juicyboom.com>, delay=08:59:56, xdelay=00:00:00, mailer=esmtp, pri=1020554, relay=mail.juicyboom.com., dsn=4.0.0, stat=Deferred: Connection timed out with mail.juicyboom.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q9Nnob011458: to=<aaron@juicyboom.com>, delay=08:48:46, xdelay=00:00:00, mailer=esmtp, pri=1020554, relay=mail.juicyboom.com., dsn=4.0.0, stat=Deferred: Connection timed out with mail.juicyboom.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8pH0A007887: to=<aaron.070194@gmail.com>, delay=09:21:18, xdelay=00:00:00, mailer=esmtp, pri=1020560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8rkK3008160: to=<aaron.070194@gmail.com>, delay=09:18:49, xdelay=00:00:00, mailer=esmtp, pri=1020560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8TUXJ005341: to=<aaron.070194@gmail.com>, delay=09:43:05, xdelay=00:00:00, mailer=esmtp, pri=1110560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8Wnpn005708: to=<aaron.070194@gmail.com>, delay=09:39:46, xdelay=00:00:00, mailer=esmtp, pri=1110560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8bZaD006249: to=<aaron.070194@gmail.com>, delay=09:35:00, xdelay=00:00:00, mailer=esmtp, pri=1110560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.
May 26 14:12:35 inspvirtual sendmail[3130]: x4Q8fPQg006712: to=<aaron.070194@gmail.com>, delay=09:31:10, xdelay=00:00:00, mailer=esmtp, pri=1110560, relay=alt4.gmail-smtp-in.l.google.com., dsn=4.0.0, stat=Deferred: Connection timed out with alt4.gmail-smtp-in.l.google.com.

重要的是,我在此伺服器上手動配置了所有必要的東西才能正常工作,因為這是一個專用伺服器,例如 DNS。

我還更改了/etc/mail/sendmail.mc文件,在這一行:

LOCAL_DOMAIN(`inspvirtual.mx')dnl

我還執行hostname檢查主機名是否正確,並列印出正確的主機名:inspvirtual.mx

我在手動從 0 配置伺服器方面很新,所以我在這裡有點迷失……

正如MichaelHampton建議的那樣,

如果所有消息都排在隊列中,connection timed out

那麼您的傳出 SMTP 連接很可能被您的 ISP 或 Intranet 防火牆阻止(被防火牆屏蔽)

建議的驗證程序:

  1. 檢查是否收到 SMTP 問候消息

$$ Is it sendmail fault? $$

telnet mail.juicyboom.com 25

2.使用諸如tcptraceroute定位防火牆位置之類的工具

引用自:https://serverfault.com/questions/968900