php - 如何向 Laravel 中的所有用户发送电子邮件?
问题描述
我想向我的应用程序中的所有用户发送电子邮件。我创建了一个单独的示例项目,其中唯一的功能是添加/创建新用户及其电子邮件和姓名。每当有新用户注册时,我想给我的每个现有用户发送电子邮件。就像“你好,我们有一个新成员!” 信息。
控制器
public function store()
{
$customer = Customer::create($this->validatedData());
foreach ($customer as $customers) {
Mail::to($customers->email)->send(new WelcomeMail());
}
return redirect('/customers');
}
解决方案
Here your code is correct but not completely
So I have modified it
Now you need to create one Job file using
php artisan make:job WelcomeMessage
and then run
php artisan migrate
to send the mail
use App\Job\WelcomeMessage;
public function store()
{
$customer = Customer::create($this->validatedData());
if ($customer) {
$allUser = Customer::where('id', '!=', $customer->id)->get();
$html = "Your html file with mail content";
$sub = "Welcome Mail";
foreach($allUser as $allUsers){
Mail::to($allUsers->email)->send(new WelcomeMessage($html,$sub));
}
}
return redirect('/customers');
}
If you run this command php artisan make:job WelcomeMessage
then it will create the page in the app\Job folder. Then paste the below code on that page.
<?php
namespace App\Emails;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class UserVerificationMail extends Mailable
{
use Queueable, SerializesModels;
public $subject;
public $file;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($file,$subject)
{
$this->subject = $subject;
$this->file = $file;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from(env('MAIL_FROM_ADDRESS'), env('APP_NAME'))
->subject($this->subject)
->markdown($this->file);
}
}
and then run php artisan queue:listen
This will work
Thank You
推荐阅读
- javascript - ReactJs useState .map() 不是函数
- java - 如何将这种递归解决方案转换为分而治之?
- python - 获取字典中的值的总和
- reactjs - 在 Firebase 中注册后将用户信息保存到数据库中
- while-loop - 如何从 ansible 读取文件并让它返回最后几行?
- requirements - 是否需要 OfficeOpenXML
- react-native-ios - 如何在反应本机ios webview中加载本地文件
- laravel - Laravel - 当我尝试重置密码时,我不断收到“此密码重置令牌无效”
- azure - 逻辑应用程序 Json 不是数组
- python - 从熊猫系列中提取 pos_tag_sents