laravel 5.4:从localhost发送电子邮件不起作用

时间:2017-06-28 15:19:27

标签: php laravel email laravel-5 laravel-5.3

我想通过laravel 5.4从我的localhost发送电子邮件。它显示以下错误: screenshot of the error

这是我的.env文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tfl

这是我的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', 'mail'),

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

    /*
    |--------------------------------------------------------------------------
    | 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', 'myemail@gmail.com'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

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

];

这是我在App \ mail \ directory

中的邮件类
<?php

namespace App\Mail;

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

class CompanyVerificationEmails extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.comp_ver_emails');
    }
}

这是我的控制器

<?php

namespace App\Http\Controllers;

use App\General_model;

use App\Http\Requests;

use Session;

use Mail;
use App\Mail\CompanyVerificationEmails;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Validation\ValidatesRequests;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use Illuminate\Foundation\Auth\Access\AuthorizesResources;


class General_controller extends Controller
{

public function sentemail()
{
     $mail_to = 'receiver_email@gmail.com';

     Mail::to($mail_to)->send(new CompanyVerificationEmails);

     dd('Mail send successfully');
}

}

4 个答案:

答案 0 :(得分:2)

我也有这个问题,这就是我修复它的方法。虽然不建议更改供应商文件,但此修复工作正常,它使我能够从我的本地主机发送邮件。

  1. 在供应商文件夹中找到StreamBuffer。它应该在这个位置
  2.   

    C:\ XAMPP \ htdocs中\ mylaravel \厂商\ swiftmailer \ swiftmailer \ lib中\类\夫特\运输

    1. 打开文件StreamBuffer.php
    2. 找到此方法_establishSocketConnection
    3. 添加如下所示
    4.   

      $ options ['ssl'] ['verify_peer'] = FALSE;

           

      $ options ['ssl'] ['verify_peer_name'] = FALSE;

          private function _establishSocketConnection(){
             ....
      
             $options['ssl']['verify_peer'] = FALSE;
             $options['ssl']['verify_peer_name'] = FALSE;
      
             $streamContext = stream_context_create($options);
             $this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
      
          .... }
      

      就像ssl一直阻止邮件发送一样。我希望这有帮助

答案 1 :(得分:0)

我建议您使用log driver邮件或设置一个免费邮件帐户,但如果您真的想从本地开发计算机上测试一下,那么answer可能有所帮助:

答案 2 :(得分:0)

尝试发送邮件时,这是一个常见的Windows错误。

尝试在extension=php_openssl.dll

中取消注释php.ini

答案 3 :(得分:-1)

Route::get('sendemail', function () {

    $data = array(
        'name' => "Learning Laravel",
    );

    Mail::send('welcome', $data, function ($message) {

        $message->from('ashokchavda193@gmail.com', 'Learning Laravel');

        $message->to('ashokchavda193@gmail.com')->subject('Learning Laravel test email');

    });

    return "Your email has been sent successfully";

});