电子邮件验证似乎不再发送

发布于 2025-02-01 09:43:16 字数 2285 浏览 5 评论 0原文

当用户签署新的电子邮件地址时,我想发送电子邮件验证。因此,在寄存器控制器上我添加了此信息:

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

因此,如果电子邮件有效,它将调用助手类newRegisterMemberWithemail,它是这样的:

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

因此,它曾经工作正常并正确地发送电子邮件以进行验证,但是我不知道为什么现在不发送电子邮件。

实际上,我已经与不同的邮件服务提供商一起对此进行了测试,并且均为 yahoo & gmail 电子邮件没有以某种方式收到!

但是对于本地邮件服务提供商,我的国家发送了正确的发送电子邮件!

我不知道这里发生了什么,因为逻辑似乎很好...

所以,如果您知道,请让我知道...我真的很感谢你们的任何想法或建议。

另外,如果您想查看:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看海 2025-02-08 09:43:16

当我被使用gmail作为smtp时,我面临同样的问题。

原因:

当我们直接在SMTP设置中使用Gmail密码时,由于某些Gmail 策略将在某个时间(几个月)后阻止并停止发送电子邮件。

解决方案:

我们需要从我们的Gmail安全性创建App-Password,并在smtp设置中使用该密码。在Google文章下方将指导:

如何在gmail上创建app-password

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

我希望对您有所帮助。

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.

若无相欠,怎会相见 2025-02-08 09:43:16

如果您使用Google邮件发送电子邮件,那么我们遇到了同样的问题。

2022年5月30日,Google停止支持不太安全的应用程序或第三方应用程序。

我认为这是您发送邮件不起作用的原因(如果您使用Google邮件作为邮件发件人,请考虑此答案)

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)

辞取 2025-02-08 09:43:16

我在发送电子邮件时遇到问题,尤其是向Gmail帐户发送。因此,我改变了方法并克服了这个问题。

请检查下面的答案

< 72151267#72151267“> laravel电子邮件

示例邮件类

<?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('[email protected]', '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;
    }
}

用法

$email = '[email protected]'; // 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 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('[email protected]', '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 = '[email protected]'; // 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));
薄凉少年不暖心 2025-02-08 09:43:16

我认为问题不是您的代码。我认为这与您发送实践有关。一种解决方案是使用旨在发送诸如SparkPost的电子邮件(我为SparkPost工作的完整披露)的服务。还有很多。这些服务可以帮助您确保您遵循电子邮件最佳实践。

您可以在没有电子邮件服务的情况下完成此工作,但至少您应该验证您正在遵循Maawg提出的最佳实践: https://www.m3aawg.org/published-documents

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文