46

What code I should do change in this PHP script to send one email to more than 20 email addresses?

<?php

$email_to = "youremailaddress@yourdomain.com"; // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

?>

Please give me an example. Thank you.

Samuel Liew
  • 72,637
  • 105
  • 156
  • 238
rakib
  • 463
  • 1
  • 4
  • 5
  • 31
    It's unfortunate that no one reads the manual anymore. – Alec Smart Dec 22 '10 at 04:45
  • 20
    Stackoverflow is the manual – Jeff Davis Mar 31 '17 at 01:26
  • 3
    Manuals and documentations are useful and relevant for in-depth research. But Googling and StackOverflowing is often faster if you need the answer to a specific but common use case; especially if it can be presented in the size of a tweet or single monitor height. I wish manuals would be so flexible as to provide both. – WoodrowShigeru Jan 17 '19 at 15:03
  • @AlecSmart: It's also unfortunate that so many manuals are so poorly written as to be nearly unusable. – Adrian Keister Nov 04 '20 at 18:43

14 Answers14

62

Fore readability sake in the code use an array and implode it to a comma separated string:-

$recipients = array(
  "youremailaddress@yourdomain.com",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
prodigitalson
  • 59,320
  • 9
  • 95
  • 112
  • 9
    Please never do it like that, if the recipients do not (or should not) know each other. This way every address is exposed to every recipient. – Paul Spiegel Oct 06 '17 at 21:43
42

Your

$email_to = "address@one.com, address@two.com, address@three.com"

Needs to be a comma delimited list of email adrresses.

mail($email_to, $email_subject, $thankyou);
Stefan H
  • 6,545
  • 4
  • 23
  • 34
29

Just separate them by comma, like $email_to = "youremailaddress@yourdomain.com, emailtwo@yourdomain.com, John Doe <emailthree@example.com>".

Phoenix
  • 4,386
  • 1
  • 19
  • 13
14

Following code will do the task....

<?php

$contacts = array(
"youremailaddress@yourdomain.com",
"youremailaddress@yourdomain.com",
//....as many email address as you need
);

foreach($contacts as $contact) {

$to      =  $contact;
$subject = 'the subject';
$message = 'hello';
mail($to, $subject, $message, $headers);

}

?>
Hardik
  • 17,950
  • 2
  • 40
  • 45
rajmohan
  • 1,598
  • 1
  • 13
  • 32
  • be aware that you have not include the var for the $headers and also if you put that in the loop it will several times in each mail. – Nrc Feb 03 '20 at 12:21
  • Unlike the previous responses that send one email to a group, this suggestion will send multiple emails, one to each recipient. Given the ambiguity of the original request, either response could be correct. Note that as @Paul Spiegel pointed out, it is bad form to send group emails in the To field. BCC is preferred. – DanimalReks Sep 22 '20 at 11:01
7

Something like this:

mail("john@doe.com , marry@mail.com , frank@domain.com", "Test e-mail", "Hi, this is a test message!");

http://myphpform.com/php-form-multiple-recipients.php

zsalzbank
  • 9,430
  • 1
  • 24
  • 39
5

It is very bad practice to send all email addresses to all recipients; you should use Bcc (blind carbon copies).

    $from = "myname@mymail.com";
    $recipList = "mailaddress1,mailaddress2,etc";
    $headers = "MIME-Version: 1.0\nContent-type: text/html; charset=utf-8\nFrom: {$from}\nBcc: {$recipList}\nDate: ".date(DATE_RFC2822);
    mail(null,$subject,$message,$headers); //send the eail
Roel B.
  • 59
  • 1
  • 1
  • 2
    Bcc does not really hide anything from anyone. All you need to do is look at the headers and you still know all the email addresses – RiggsFolly Aug 26 '20 at 16:27
5
    $recipients = "test1@test.com,test2@test.com,test3@test.com,test1@test.com";
    $email_array = explode(",",$recipients);
    foreach($email_array as $email)
    {
        echo $to      =  $email;
        $subject = 'the subject';
        $message = 'hello';
       $headers = 'From: webmaster@example.com' . "\r\n" .
       'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $message, $headers);

    }
Abhijit
  • 901
  • 1
  • 9
  • 21
2

The best way could be to save all the emails in a database.

You can try this code, assuming you have your email in a database

/*Your connection to your database comes here*/
$query="select email from yourtable";
$result =mysql_query($query);

/the above code depends on where you saved your email addresses, so make sure you replace it with your parameters/

Then you can make a comma separated string from the result,

while($row=$result->fetch_array()){
        if($rows=='')    //this prevents from inserting comma on before the first element
        $rows.=$row['email'];
        else
        $rows.=','.$row['email'];
    }

Now you can use

$to = explode(',',$rows); // to change to array

$string =implode(',',$cc); //to get back the string separated by comma

With above code you can send the email like this

mail($string, "Test", "Hi, Happy X-Mas and New Year");
tkt986
  • 1,081
  • 3
  • 17
  • 25
1

In mail function you can as many reciepient as you want in $emailto paramater seperated by comma.

XMen
  • 27,697
  • 41
  • 97
  • 149
1

Try this. It works for me.

$to = $email1 .','. $email2 .','. $email3;
JJJ
  • 32,246
  • 20
  • 88
  • 102
uriateho
  • 9
  • 1
  • 4
1

This worked for me,

$recipient_email = 'sales@abc.com,support@xyz.com';

$success = mail($recipient_email, $subject, $body, $headers);
treyBake
  • 6,277
  • 6
  • 25
  • 54
taha12us
  • 21
  • 1
1

You can just write multiple email address to whom you want to send and pass it as the first argument. Example:-

mail("FirstEmailId@ABC.com, SecondEmilId@XYZ.com","Subject","Message","From: EmailId@PQR.com");
Suraj Rao
  • 28,850
  • 10
  • 94
  • 99
0

I think the following code will works.

$tos = array('address1@yourdomain.com', 'address2@yourdomain.com');
foreach ($tos as $to){
    $ok = mail ($to, $subject, $body, $from);
}
if ($ok) {
    echo "Message Send";
} else { 
    echo "Error";
}
Ahmed Sagarwala
  • 400
  • 2
  • 13
prasanth
  • 17
  • 3
  • 2
    It's worth noting that this example sends the many copies of the same email... rather than the same email to many recipients. – counterbeing Dec 12 '13 at 21:30
  • 1
    In this way only the last mail is checked in the `if`. You should put the `if..else` blocks in the loop – Cliff Burton Apr 13 '17 at 11:55
-2

Programmatically sending an submitted form to multiple email address is a possible thing, however the best practice for this is by creating a mailing list. On the code the list address will be place and any change or update on email addresses to the recipients list can be done without changing in the code.

  • 1
    you are not answering the question. More giving life advice. But without knowing the specific use its hard to know what best practice is – Jon Feb 08 '22 at 15:09