如何发送邮件多个 Laravel
How to send mail multiple Laravel
我发送到 1 个帐户,但它在 1 个帐户中发送了 2 次我想发送多封邮件
Laravel
public function get_sendmail(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'subject' => 'min:2',
'message' => 'min:10']);
$recipients = [ ["email" => "$request->email"], ["email" => "$request->email"] ];
$data = array(
'subject' => $request->subject,
'bodyMessage' => $request->message
);
foreach($recipients as $recipient) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $recipient;
Mail::send('email.contact', $dataToPassToEmailView, function($message) use ($recipient, $data) {
$message->to($recipient['email']);
$message->subject($data['subject']);
});
}
return redirect()->back()->with('message', 'success!');
}
谢谢
您的 $recipients
保存了两次相同的地址,因为您将相同的 $request->email
添加到两个 recipients
电子邮件密钥。
$recipients = [
[
"email" => "$request->email"
],
[
"email" => "$request->email"
]
];
要解决此问题,您可以将 $request->email
作为数组发送。如果请求来自表单,请尝试这样的操作:
<form action="{{ route('your-sendmail-route') }} method="YOUR METHOD">
@csrf
<label>First email:</label>
<input name="email[]" type="email" class="email">
<label>Second email:</label>
<input name="email[]" type="email" class="email">
<label>Third email:</label>
<input name="email[]" type="email" class="email">
<button type="submit">Send mail</button>
</form>
当您提交此表单时,在您的控制器中您会收到一组电子邮件,然后可以遍历它:
foreach($request->email as $email) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $email;
Mail::send('email.contact', $dataToPassToEmailView, function($message) use
($recipient, $data) {
$message->to($email);
$message->subject($data['subject']);
});
}
我想应该可以解决你的问题。
我发送到 1 个帐户,但它在 1 个帐户中发送了 2 次我想发送多封邮件
Laravel
public function get_sendmail(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'subject' => 'min:2',
'message' => 'min:10']);
$recipients = [ ["email" => "$request->email"], ["email" => "$request->email"] ];
$data = array(
'subject' => $request->subject,
'bodyMessage' => $request->message
);
foreach($recipients as $recipient) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $recipient;
Mail::send('email.contact', $dataToPassToEmailView, function($message) use ($recipient, $data) {
$message->to($recipient['email']);
$message->subject($data['subject']);
});
}
return redirect()->back()->with('message', 'success!');
}
谢谢
您的 $recipients
保存了两次相同的地址,因为您将相同的 $request->email
添加到两个 recipients
电子邮件密钥。
$recipients = [
[
"email" => "$request->email"
],
[
"email" => "$request->email"
]
];
要解决此问题,您可以将 $request->email
作为数组发送。如果请求来自表单,请尝试这样的操作:
<form action="{{ route('your-sendmail-route') }} method="YOUR METHOD">
@csrf
<label>First email:</label>
<input name="email[]" type="email" class="email">
<label>Second email:</label>
<input name="email[]" type="email" class="email">
<label>Third email:</label>
<input name="email[]" type="email" class="email">
<button type="submit">Send mail</button>
</form>
当您提交此表单时,在您的控制器中您会收到一组电子邮件,然后可以遍历它:
foreach($request->email as $email) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $email;
Mail::send('email.contact', $dataToPassToEmailView, function($message) use
($recipient, $data) {
$message->to($email);
$message->subject($data['subject']);
});
}
我想应该可以解决你的问题。