[英]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:因此,如果电子邮件有效,它将调用一个帮助类
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);
}
}
}
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!事实上,我已经用不同的邮件服务提供商对此进行了测试,对于Yahoo和Gmail ,电子邮件都没有以某种方式收到!
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:如果您想看一下,这也是我的
AuthMail
类:
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
.当我将
Gmail
用作smtp
时,我曾经遇到过同样的问题。
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.当我们直接在 smtp 设置中使用我们的
Gmail
密码时,由于某些 Gmail policies
,它会在某个时间(几个月)后被阻止并停止发送电子邮件。
we need to create an app-password
from our Gmail security and use that password in smtp
settings.我们需要从我们的 Gmail 安全性中创建一个
app-password
,并在smtp
设置中使用该密码。 below google article will guide:下面的谷歌文章将指导:
How to create app-password on gmail如何在gmail上创建应用程序密码
laravel
: laravel
smtp 设置: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. 2022 年 5 月 30 日,Google 将停止支持安全性较低的应用程序或第三方应用程序。
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.我在发送电子邮件时遇到问题,尤其是发送到 gmail 帐户。 So I have changed my approach and overcome that issue.
所以我改变了我的方法并克服了这个问题。
Please check my answer below请在下面查看我的答案
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).
一种解决方案是使用旨在发送电子邮件的服务,例如 SparkPost(完全披露我为 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您可以在没有电子邮件服务的情况下完成这项工作,但至少您应该验证您是否遵循 MAAWG 提出的最佳实践: https ://www.m3aawg.org/published-documents
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.