我有一个表单,当我填写并提交时,它会向多个收件人发送电子邮件,当我这样做时,我会收到这个错误htmlspecialchar((需要参数1是字符串,给定数组(View:C:\wamp64\www\star\resources\views\email_invite_template.blade.php下面是我的代码:
表单
<input name="iname[]" class="form-control" type="text" placeholder="Name" >
<input name="iemail[]" class="form-control" type="text" placeholder="Email" >
<input type="submit" value="Send Invite" >
发送电子邮件
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
return $this->subject('Invitation to Mase')
->markdown('emails.email_invite_template')
->with('data', $this->data);
}
}
控制器
public function storeInvite(Request $request)
{
$eventid = $request->get('eventid');
$iname = $request->get('iname');
$iemail = $request->get('iemail');
$data = array(
'dname' => $request->iname,
'demail' => $request->iemail
);
foreach($data['demail'] as $user){
Mail::to($user)->send(new SendEmail($data));
}
return redirect()->back()->with('status','Invitation sent to users');
}
电子邮件模板(Email_invite_Template.php(
@component('mail::message')
<p>Hi, {{ $data['dname'] }}</p>
<p>You have just been invited to an event below is your invitation code:</p>
<p></b>{{ $data['demail'] }}</b></p>
Thanks,<br>
{{ config('app.name') }}
@endcomponent
感谢
不能在每次迭代中都向前传递$data
。您需要构建自己的数组,其中包含每个name
和email
对:
$data = [
'dname' => $request->iname,
'demail' => $request->iemail
];
// dname: `[0 => 'Mike', 1 => 'Bob', ...]
// demail: `[0 => 'mike@test.com', 1 => 'bob@test.com', ...]
foreach($data['demail'] as $index => $email){
Mail::to($email)->send(new SendEmail([
'dname' => $data['dname'][$index],
'demail' => $email
]));
}
现在,您发送一个包含2个元素的数组,dname
和demail
,每个元素代表一对数组元素:
// 1st Iteration: ['dname' => 'Mike', 'demail' => 'mike@test.com']
// 2nd Iteration: ['dname' => 'Bob', 'demail' => 'bob@test.com']
这100%取决于您在输入中输入相同数量的dname
和demail
值。如果你有3封电子邮件,但只有2个名字,这是行不通的。