Laravel not sending email or not received mail

Viewed 995

I am trying to send a mail from my domain email (cPanel), but I didn't receive any mail in my Gmail Account. Also, Laravel didn't display any errors.

.env

MAIL_MAILER=smtp
MAIL_HOST=mail.domain.com
MAIL_PORT=465
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=support@domain.com
MAIL_FROM_NAME="${APP_NAME}"

Also, I add implements MustVerifyEmail to the User model class.

web.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendUserEmailVerification;

Route::get('/send-email', function () {
    Mail::to("myaccount@gmail.com")->send(new SendUserEmailVerification());
    return response("success");
});

SendUserEmailVerification.php

<?php

namespace App\Mail;

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

class SendUserEmailVerification 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->markdown('emails.user.email_verfy');
    }
}

email_verfy.blade.php

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

When I use mailtrap.io, it shows mail in the inbox.

Also, it's working on using PHPMailer as https://github.com/PHPMailer/PHPMailer but it seems a little bit hard to manage email verification on Laravel.

Why I didn't get the mail that Laravel send?

Does anybody know something?

Please guide me.

1 Answers

.ENV

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-gmail-username
MAIL_PASSWORD=your-application-specific-password
MAIL_ENCRYPTION=tls

Send it with the Mail::raw() function:

\Mail::raw('Hi, welcome user!', function ($message) {
  $message->to(..)
    ->subject(..)
    ->setBody('<h1>Hi, welcome user!</h1>', 'text/html');
});

Other option is using the Mail::send() function:

\Mail::send([], [], function ($message) {
  $message->to('my@email.com')
    ->subject('my subject')
    ->setBody('Hi, welcome user!'); 
});

Hope this works for you, please let me know!

Previous answer before edit after Tomerikoo's comment:

What exactly is going wrong? Do you not want to receive your mail on Mailtrap but Gmail instead?

Related