phplaravelemaillaravel-5.8laravel-mail

Email verification does not seem to be sending anymore


I want to send email verification when a user signs up with a new Email Address. So at the Register Controller I added this:

public function register(Request $request)
{   
    if(Session::has('email')){
        return Redirect::back()->withErrors(['msg' => 'Email was already sent to you, please check the spam folder too.']);
    }else{
        $validatedEmail = $request->validate([
            'user_input' => 'required|unique:users,usr_email|regex:/(.+)@(.+)\.(.+)/i|max:125|min:3',
        ],[
            'user_input.required' => 'You must enter this field',
            'user_input.unique' => 'This email is already registered',
            'user_input.regex' => 'This email is not correct',
            'user_input.max' => 'Maximum length must be 125 characters',
            'user_input.min' => 'Minimum length must be 3 characters',
        ]);
        $register = new NewRegisterMemberWithEmail();
        return $register->register();
    }
}

So if the email was valid, it will call a helper class NewRegisterMemberWithEmail which goes like this:

class NewRegisterMemberWithEmail
{
    public function register()
    {
        try{
            $details = [
                'title' => 'Verify email'
            ];
            Mail::to(request()->all()['user_input'])->send(new AuthMail($details));
            Session::put('email',request()->all()['user_input']);
            return redirect()->route('login.form');
        }catch(\PDOException $e){
            dd($e);
        }
    }
}

So it used to work fine and correctly sends the email for verification, but I don't know why it does not send email nowadays.

In fact I have tested this with different mail service providers and for both Yahoo & Gmail the email did not received somehow!

But for local mail service provider based in my country the email was sent properly!

I don't know really what's going on here because the logic seems to be fine...

So if you know, please let me know... I would really really appreciate any idea or suggestion from you guys.

Also here is my AuthMail Class if you want to take a look at:

class AuthMail extends Mailable
{
    use Queueable, SerializesModels;
    
    public $details;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

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

Solution

  • Once I was faced same problem when I was used Gmail as smtp.

    Reason:

    when we used our Gmail password directly in smtp settings then due to some Gmail policies it'll be blocked after sometime (months) and stopped email sending.

    Solution:

    we need to create an app-password from our Gmail security and use that password in smtp settings. below google article will guide:

    How to create app-password on gmail

    .env smtp setting for laravel:
    MAIL_MAILER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=<your-email>
    MAIL_PASSWORD=<app-password>
    MAIL_ENCRYPTION=tls
    

    I hope that'll help you.