【问题标题】:cPanel send email in production using Laravel 5.4cPanel 使用 Laravel 5.4 在生产环境中发送电子邮件
【发布时间】:2018-12-07 13:12:30
【问题描述】:

我有一个关于 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' ]);

}

我收到了回复,但电子邮件没有发送。

【问题讨论】:

  • 在服务器上尝试简单的邮件功能,测试邮件是否真的在服务器上工作?

标签: php laravel email smtp cpanel


【解决方案1】:

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

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

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

【讨论】:

  • 你的意思是我应该将我的 .env MAIL_PORT 更改为 465 或 587 ?剩余值相同?
  • 使用端口 465 获得“超时”
  • 您的提供商可能不支持通过 TLS 发送,或者您需要安装证书。您是否尝试过使用其他邮件服务器?但请先尝试 587 端口。
  • 端口 587 没有显示错误,也没有发送电子邮件,通过邮件服务器你的意思是不同于 SMTP 吗?不,我没有。证书呢?
  • 你怎么知道它没有发送电子邮件?通过不同的电子邮件服务器,我的意思是不是mail.glistars.com。证书:我不记得它在 cPanel 中是如何工作的,但我想你应该去 SSL 证书部分并确保你为邮件服务安装了证书。
猜你喜欢
  • 2021-03-21
  • 1970-01-01
  • 2023-04-02
  • 2018-02-01
  • 2019-03-12
  • 2016-03-20
  • 1970-01-01
  • 2012-05-04
  • 2014-04-04
相关资源
最近更新 更多