我能够发送单一的电子邮件,但当涉及到多封电子邮件,作业类无法发送给多个用户。
下面的代码运行良好,并向一个用户发送电子邮件。
$email = new Report($this->user);
Mail::to($this->user->email)->queue($email); 即使是硬编码的电子邮件也是有效的。
$email = new Report($this->user);
Mail::to("example@hello.com")->queue($email); 但当我传递多封或多封电子邮件时,“职务”会失败:
$email = new Report($this->user);
$all_admin = User::select('email')->where('role',2)->get()->pluck('email')->toArray();
$all_admins = json_encode($all_admin, true);
Mail::to($all_admins )->queue($email); 此代码是在handle函数中的App\Jobs\ReportAdmin文件中编写的。
我以前发过很多电子邮件而不使用工作。
类似于:
Mail::send('emails.report', ['firstname'=>$firstname,'lastname'=>$lastname], function ($message)
{
$message->from('hello@example.com', 'auto-reply email');
$message->to($all_admins);
$message->subject('subject');
});发布于 2017-05-21 08:04:12
从医生那里
若要发送消息,请在邮件外观上使用To方法。to方法接受电子邮件地址、用户实例或用户集合。
那就这么做吧。
$email = new Report($this->user);
$admins = User::select('email')->where('role', 2)->get();
Mail::to($admins)->queue($email);这就是引擎盖下面发生的事情。如果您想使用不同的方式加载电子邮件列表。
public function to($address, $name = null)
{
return $this->setAddress($address, $name, 'to');
}
protected function setAddress($address, $name = null, $property = 'to')
{
foreach ($this->addressesToArray($address, $name) as $recipient) {
$recipient = $this->normalizeRecipient($recipient);
$this->{$property}[] = [
'name' => isset($recipient->name) ? $recipient->name : null,
'address' => $recipient->email,
];
}
return $this;
}
protected function addressesToArray($address, $name)
{
if (! is_array($address) && ! $address instanceof Collection) {
$address = is_string($name) ? [['name' => $name, 'email' => $address]] : [$address];
}
return $address;
}
protected function normalizeRecipient($recipient)
{
if (is_array($recipient)) {
return (object) $recipient;
} elseif (is_string($recipient)) {
return (object) ['email' => $recipient];
}
return $recipient;
}发布于 2017-05-20 19:32:32
试着改变这一点:
$all_admins = json_encode($all_admin, true);为此:
$all_admins = implode(';', $all_admin);这将为您提供有效的字符串格式。
编辑
您也可以尝试使用itteration:
$email = new Report($this->user);
$all_admin = User::select('email')->where('role',2)->get()
->pluck('email')->toArray();
$all_admins = json_encode($all_admin, true);
foreach ($all_admin as $admin) {
Mail::to($admin)->queue($email);
}这是一个更好的解决方案,因为每封电子邮件都是由一个任务发送的。
发布于 2017-05-20 19:41:02
我用了一个for循环,它起作用了。
不知道这是否是解决这个问题的最好方法。
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new Report($this->user);
$all_admin = User::select('email')->where('role',2)->get()->pluck('email')->toArray();
$count = count($all_admin);
for($i = 0; $i<$count; $i++)
{
Mail::to($all_admin[$i])->queue($email);
}
}https://stackoverflow.com/questions/44089814
复制相似问题