How Can I Send An Email To Multiple Email Addresses Through A Multiple Select Form?
-
14-07-2021 - |
Question
I am trying to send an email using the PHP mail
function to multiple email addresses selected through a multiple select form without doing a for each loop. I am currently saving the email addresses as an array, but the PHP mail function returns an error saying it only accepts one argument. Any ideas on how to make this work?
Solution
I am assuming your select looks something like this:
<select multiple="multiple" size="3" name="emails[]">
<option value="jane@domain.com">Jane</option>
<option value="jimmy@domain.com">Jimmy</option>
<option value="john@domain.com">John</option>
<option value="james@domain.com">James</option>
</select>
In that case, the list of e-mail address would be returned as an array to your PHP (note the empty square brackets in the name
attribute). To send e-mails to all the selected addresses without a loop, you can implode your array with a comma and feed it into the first argument of the PHP mail
function, like so:
<?php
$addresses=implode(", ", $_POST["emails"]);
mail($addresses, "Important message", "To whom it may concern, bla bla bla");
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow