[英]Email verification does not seem to be sending anymore
當用戶使用新的電子郵件地址注冊時,我想發送電子郵件驗證。 所以在注冊控制器我添加了這個:
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 ,電子郵件都沒有以某種方式收到!
但是對於我所在國家/地區的本地郵件服務提供商,電子郵件已正確發送!
我真的不知道這里發生了什么,因為邏輯似乎很好......
所以如果你知道,請告訴我......我真的很感謝你們的任何想法或建議。
如果您想看一下,這也是我的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');
}
}
當我將Gmail
用作smtp
時,我曾經遇到過同樣的問題。
當我們直接在 smtp 設置中使用我們的Gmail
密碼時,由於某些 Gmail policies
,它會在某個時間(幾個月)后被阻止並停止發送電子郵件。
我們需要從我們的 Gmail 安全性中創建一個app-password
,並在smtp
設置中使用該密碼。 下面的谷歌文章將指導:
laravel
smtp 設置:MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<your-email>
MAIL_PASSWORD=<app-password>
MAIL_ENCRYPTION=tls
我希望這會對你有所幫助。
如果您使用谷歌郵件發送電子郵件,那么我們有同樣的問題。
2022 年 5 月 30 日,Google 將停止支持安全性較低的應用程序或第三方應用程序。
這就是我認為您的發送郵件不起作用的原因(如果您使用谷歌郵件作為郵件發件人,請考慮這個答案)
我在發送電子郵件時遇到問題,尤其是發送到 gmail 帳戶。 所以我改變了我的方法並克服了這個問題。
請在下面查看我的答案
示例郵件類
<?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;
}
}
用法
$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));
我認為問題不在於您的代碼。 我認為這與您發送的做法有關。 一種解決方案是使用旨在發送電子郵件的服務,例如 SparkPost(完全披露我為 SparkPost 工作)。 還有很多其他的。 這些服務可以幫助您確保遵循電子郵件最佳做法。
您可以在沒有電子郵件服務的情況下完成這項工作,但至少您應該驗證您是否遵循 MAAWG 提出的最佳實踐: https ://www.m3aawg.org/published-documents
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.