简体   繁体   中英

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');
    }
}

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.

If you use google mail to send email then we have the same problem.

On May 30, 2022 Google stop supporting less secure applications or third party application.

This is I think the reason why your send mail does not work (consider this answer if you use google mail as mail sender)

I was having issues when sending email, especially to gmail accounts. So I have changed my approach and overcome that issue.

Please check my answer below

Laravel Email

Example Mail Class

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\Mime\Email;

class OrderInfoMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $data;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this
            ->subject('Order Confirmation')
            ->from('noreply@app.xxx.co.uk', 'XXX Portal')
            ->view('orders.templates.order-form')
            ->with([
                'name' => $this->data->name,
                'sales_representative_name' => $this->data->sales_representative_name,
                'sales_representative_phone' => $this->data->sales_representative_phone,
                "items" => $this->data->items,
                "address" => $this->data->address,
                "net" => $this->data->net,
                "payment" => $this->data->payment,
                "balance" => $this->data->balance,
            ]);

        $this->withSymfonyMessage(function (Email $message) {
            $message->getHeaders()->addTextHeader(
                'X-Mailer', 'PHP/' . phpversion()
            );
        });

        return $this;
    }
}

Usage

$email = 'a@b.com'; // pls change
$name = 'ab';// pls change

$data = new \stdClass();
$data->name = $name;
$data->sales_representative_name = \App\User::find(Auth::user()->id)->name;
$data->sales_representative_phone = \App\User::find(Auth::user()->id)->phones->first()->number;
$data->items = $order_items;
$data->address = $address;
$data->net = $net;
$data->payment = $payment;
$data->balance = $balance;

Mail::to($email)->send(new \App\Mail\OrderInfoMail($data));

I don't think the issue is your code. I think it is related to you sending practices. A solution is to use a service that is designed to send emails like SparkPost (full disclosure I work for SparkPost). There are many others. These services can help you make sure you are following email best practices.

You can make this work without an email service but at the very least you should verify you are following the best practices presented by MAAWG: https://www.m3aawg.org/published-documents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM