cPanel使用Laravel 5.4在生产中发送电子邮件

时间:2018-06-28 12:36:28

标签: php laravel email smtp cpanel

我在Godaddy虚拟主机上有一个Laravel项目

我想发送有关提交一些数据的电子邮件

因此,我使用“从我发送给我”的Gmail帐户在本地计算机上进行了测试,并且工作正常,但是当我移出相同的确切配置错误时,错误突然开始出现

我遇到了与某个套接字有关的错误,并且找到了一个答案。

我尝试使用cPanel中创建的电子邮件,但没有用。

我试图设置可以正常运行的Gmail帐户,也无法正常工作。

Gmail帐户一直给我这个错误

local.ERROR: Connection could not be established with host smtp.gmail.com [Connection refused #111] {"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.gmail.com [Connection refused #111] at /home/cr4jtnq8a04f/public_html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)
[stacktrace]

当我更改为cpanel电子邮件设置时,我没有发现应该发送的电子邮件没有错误。

这是我的cPanel电子邮件设置的.env设置

MAIL_DRIVER=smtp
MAIL_HOST=mail.glistars.com
MAIL_PORT=25
MAIL_USERNAME=info@glistars.com
MAIL_PASSWORD=MyPassword
MAIL_ENCRYPTION=tls

这是我的config / 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', 'smtp'),

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

    /*
    |--------------------------------------------------------------------------
    | 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', 'info@glistars.com'),
        'name' => env('MAIL_FROM_NAME', 'Glitter Stars'),
    ],

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

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

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

    'stream' => [
    'ssl' => [
        'allow_self_signed' => true,
        'verify_peer' => false,
        'verify_peer_name' => false,
        ],
    ],

];

这是我的控制器功能

public function send() {

      $data = [
        'name' => request('name'),
        'phone' => request('phone'),
        'message' => request('message'),
        'lang' => request('lang'),
      ];

      Mail::to('abdul.elah.js@gmail.com')->send(new Enquiry($data));

    return response()->json([ 'message' => 'Email Sent Successfully' ]);

}

我收到回信,但电子邮件未发送。

1 个答案:

答案 0 :(得分:1)

这可能与在端口25上使用TLS发送有关。端口25通常是未加密的SMTP端口。从理论上讲,您可以使用具有任何加密功能的任何端口,但是Google可能不喜欢它。您是否曾对非Google收件人尝试过相同的配置?

无论如何,请尝试在端口587上发送并在465上失败(使用SSL而不是TLS)。

如果这不起作用,请尝试通过将邮件发送给没有任何安全性(仅用于测试)的收件人并使用独立的SMTP库来简化问题。