PHP Send-Mail form to multiple email addresses
You implode an array of recipients:
$recipients = array('jack@gmail.com', 'jill@gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php
Receiver, or receivers of the mail.
The formatting of this string must comply with » RFC 2822. Some examples are:
user@example.com
user@example.com
,anotheruser@example.com
- User
<user@example.com>
- User
<user@example.com>
, Another User <anotheruser@example.com
>
PHP form send email to multiple recipients
This will work:
$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";
PHP mail: Multiple recipients?
while($row = mysql_fetch_array($result))
{
$addresses[] = $row['address'];
}
$to = implode(", ", $addresses);
As specified on the mail()
manual page, the "to" parameter of the function can take a comma-separated list of addresses.
Sending mail to multiple recipients
You can put multiple email addresses into the to
field by simply adding a comma between them inside the parameter string like this:
mail("email1@mailserver.com, email2@mailserver.com", // rest of your code
Edit: Per comments below.
you can hide the multiple email addresses by using the additional headers param in the mail()
function as per the docs on it:
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
This is the fourth param in the mail()
arguments passed:
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Send email to multiple recipients
Easiest way would be to build up an array of email addresses and then loop through them sending an individual mail to each one:
$recipients = array('youremail@example.com', 'repemail@example.com', 'bob@example.com');
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}
(Note: this is not a good way to send bulk email to 100s of people, but will be fine in your case with just a few addresses.)
If you were happy to have multiple emails in the To field, you just need to comma separate them:
$to = 'youremail@example.com, repemail@example.com, bobemail@example.com';
mail($to,$subject,$message,$headers);
Edit: You can build up your array of recipients in the while loop, so something like:
$recipients = array();
while($row = mysql_fetch_array($result)) {
$recipients[] = $row['RepEmail'];
echo '<br />';
echo "Rep's email Address: ";
echo '<font color="#FF7600">',$row['RepEmail'], '</font>';
echo '<br />';
echo "Rep's ID: ";
echo '<font color="#FF7600">',$row['RepId'], '</font>';
echo '<hr />';
}
then add your static email address to this array:
$recipients[] = 'youremail@example.com';
and then send them as you were before, but this time doing the actual mail part in a loop, for each recipient:
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "webmaster @ send this to me.com";
$headers = "From:" . $from;
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}
Sending emails to multiple addresses using config PHP file and PHP Mailer
Based on the examples from PHPMailer courtesy of the link shared by @Chris Haas. I made these changes:
<?php
$config = [
"host" => "xxx",
"username" => "xxx",
"password" => "xxx",
"secure" => "ssl", // ssl or tls
"port" => 465,
"sendTo" => "abc@email.com",
"sendTo2" => "efg@email.com",
"sendTo3" => "hik@email.com",
"sendToBCC" => "xyz@email.com",
"from" => "no-reply@email.com",
"fromName" => "Contact Form"
];
And in the sending file I did this:
//Recipients
$mail->setFrom($config['from'], $config['fromName']);
$mail->addAddress($config['sendTo']);
$mail->addAddress($config['sendTo2']);
$mail->addAddress($config['sendTo3']);
$mail->addCC($config['sendToCC']);
$mail->addBCC($config['sendToBCC']);
$mail->addAddress($_POST['email']);
That resolved the issue.
Related Topics
How to Type Hint More Than One Type
PHP Get Start and End Date of a Week by Weeknumber
Select Outgoing Ip for Curl Request
In PHP How to Clear a Wsdl Cache
Pdo MySQL: Insert Multiple Rows in One Query
How to Write Data into an Excel Using PHP
PHP Exec() Will Not Execute Shell Command When Executed via Browser
Use Strings to Access (Potentially Large) Multidimensional Arrays
What Does "Mass Assignment" Mean in Laravel
PHP Replacing Special Characters Like à->A, è->E
How to Use Regexiterator in PHP
PHP Function to Delete All Between Certain Character(S) in String