Laravel 5.5不发送电子邮件

时间:2018-02-19 16:27:59

标签: php laravel email

您好我尝试从我的注册脚本向用户(link)发送电子邮件。我有配置文件:

config/mail.php

return [

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

    'host' => env('MAIL_HOST', 's44.linuxpl.com'),

    'port' => env('MAIL_PORT', 587),

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'verify@coins.webmg.pl'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    'username' => env('verify@coins.webmg.pl'),

    'password' => env('MySecretPassword'),

    'sendmail' => '/usr/sbin/sendmail -bs',

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

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
    'stream' => [
        'tls' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true,
        ],
    ]
];

并提交.env

MAIL_DRIVER=smtp
MAIL_HOST=s44.linuxpl.com
MAIL_PORT=587
MAIL_USERNAME=verify@coins.webmg.pl
MAIL_PASSWORD=MySecretPassword
MAIL_ENCRYPTION=tls

RegisterController.php

发送邮件的脚本
protected function create(array $data)
{

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    $verifyUser = VerifyUser::create([
        'user_id' => $user->id,
        'token' => str_random(40)
    ]);

    $sendMail = Mail::to($user->email)->send(new VerifyMail($user));

    return $user;
}  

班级VerifyMail

class VerifyMail extends Mailable
{
    use Queueable, SerializesModels;
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('emails.verifyUser');
    }
}

要发送verifyUser.blade.php

的文件
<h2>Welcome to the site {{$user['name']}}</h2>

我不知道此配置有什么问题,因为我可以正确登录到邮箱,​​因此电子邮件地址和密码是正确的。此外,laravel不会返回任何错误,脚本本身也会正确执行。

3 个答案:

答案 0 :(得分:1)

将您的mail.php文件恢复为默认值替换:

'username' => env('verify@coins.webmg.pl'),
'password' => env('MySecretPassword'),

'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

您误解了env($key, $default = null)方法的工作原理。它所采用的第一个参数是环境变量的(例如'MAIL_USERNAME'),第二个参数是默认值(可以是可选的)。 / p>

将您的邮件凭据存储在 .env仅限文件中,绝不会在配置文件中env()来电。

Laravel Docs: Configuration

答案 1 :(得分:0)

我找到了解决方案。问题出在文件.env上。该文件已加载以前的设置。这足以清理缓存文件。

php artisan config:cache
php artisan config:clear
php artisan cache:clear

这里有很好的描述Trying to get Laravel 5 email to work

答案 2 :(得分:0)

将config / mail.php恢复为默认值,然后尝试直接在laravel .env文件中设置以下值

please try like this

function deleteItem(index){
            var getLSCityName = localStorage.getItem('savedLocations');
            getLSCityName.splice(index,1); // delete item at index
        }

祝愿这对您有所帮助