邮件错误:SMTP错误:无法连接到SMTP主机

时间:2019-10-01 18:55:52

标签: laravel phpmailer

如何解决此错误?我尽一切可能解决此问题,但无法解决。这是我的.env文件代码

MAIL_DRIVER=smtp
MAIL_HOST=fluorine.cloudhosting.co.uk
MAIL_PORT=465
MAIL_USERNAME=contacts@cleansafeltd.com
MAIL_PASSWORD=Hggjgjgghv123
MAIL_ENCRYPTION=ssl

这是mail.php的代码。

<?php

return [
    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'fluorine.cloudhosting.co.uk'),
    'port' => env('MAIL_PORT', 465),
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'contacts@cleansafeltd.com'),
        'name' => env('MAIL_FROM_NAME', 'Contact'),
    ],
    'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'markdown' => [
        'theme' => 'default',
        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
];

这是mailController

public function sendMail($to_email, $template_id, $users_insertId = 0) {
    set_time_limit(0);
    $mail = new PHPMailer(true);
    $mail->IsSendmail();
    $mail->IsSMTP(true);
    $mail->Host = "fluorine.cloudhosting.co.uk";
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Username = "website@cleansafeltd.com";
    $mail->Password = "Star9000!";
    $mail->Port = "465";
    $mail->IsHTML(true);
}

我也使用tls或端口587进行了所有精简,但未解决。

2 个答案:

答案 0 :(得分:0)

使用您的凭据(顺便说一句,您不应在公共场所发布密码,最好更改密码),我能够使用以下代码向自己发送邮件

Mail::to('mymail@domain')->send(new \App\Mail\TestMail);

\App\Mail\TestMail是一个Mailable类,定义如下

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    // Optional params are commented out. 
    // replyTo changes the address one gets when clicking the reply button
    public function build()
    {
        return $this->from(env('MAIL_USERNAME'), 'An alias name')
                    // ->to('mail@domain', 'alias name for recipient')
                    // ->cc('mail2@domain', 'alias name for cc')
                    // ->bcc('mail3@domain', 'alias name for bcc')
                    // ->replyTo('mail4@domain', 'alias for replyTo')
                    ->subject('This is a test')
                    ->view('mails.sample_mail'); // This is a laravel view.
    }
}

答案 1 :(得分:-2)

尝试一下:

  1. 设置您的Gmail帐户

    • 转到设置->转发和POP / IMAP

          - Check -> POP download: Enable POP for all mail (even mail that's already been downloaded)
      
        - Check -> IMAP access: Enable IMAP
      
        - Save
      
      • 转到Google帐户->安全->安全访问权限较少

        • 设置为打开,而不是关闭
  2. 将此设置为您的.env文件

    MAIL_DRIVER = smtp

    MAIL_HOST = smtp.googlemail.com

    MAIL_PORT = 465

    MAIL_USERNAME =您的电子邮件用户名

    MAIL_PASSWORD =您的电子邮件密码

    MAIL_ENCRYPTION = ssl

    MAIL_FROM_ADDRESS =您的电子邮件地址

    MAIL_FROM_NAME =您的电子邮件名

  3. 现在您可以使用您的smtp gmail帐户发送电子邮件