Rob
Rob

Reputation: 6370

Send email to two addresses?

Is it possible to send the enquiry email to two addresses with the code below? If so how would I do it?

<?php if(isset($_GET['emailme']) && $_GET['emailme'] == 'true') { 
    // to and subject
    $to = "[email protected]"; 
    $subject = "Product enquiry"; 

    // get these from query string
    $name_field = $_GET['name'];
    $hospital_field = $_GET['hospital']; 
    $department_field = $_GET['department'];
    $email_field = $_GET['email']; 
    $tel_field = $_GET['tel']; 

    // get wishlist
    $query = "SELECT w.*, p.product_name, q.quantity_name, o.product_code, o.description 
                FROM wishlistbasket w, products p, product_quantities q, product_options o 
                WHERE sesid = '$sesid' AND w.pid = p.id AND w.qid = q.id AND w.oid = o.id ORDER BY w.pid, w.qid, w.oid";
    $res = mysql_query($query);
    $wish_list = '';
    if($res){
        while($row = mysql_fetch_assoc($res)) {
            if ($row['qty'] == 1) {
                $row['qty'] = "Quote";
            } else if ($row['qty'] == 2) {
                $row['qty'] = "Sample";
            } else if ($row['qty'] == 3) {
                $row['qty'] = "Quote and Sample";
            }
            $wish_list .= $row['product_code'] . ' - ' . $row['product_name'] . ', ' . $row['quantity_name'] . ', ' . $row['qty'] . '' . $row['product_options'] . "
            \n";
        }
    }

    // build mail body
    $body = "Hello,\n\n
You have an enquiry from the website, please see the details below:\n\n 
Name: $name_field\n
Hospital/institution: $hospital_field\n
Department: $department_field\n 
E-Mail: $email_field\n 
Tel: $tel_field\n 
Wishlist:\n $wish_list"; 
    mail($to, $subject, $body); 
    echo "Thanks";} ?>

Upvotes: 5

Views: 23042

Answers (5)

KH42
KH42

Reputation: 111

In order to send an email to multiple recipients, individually, with their email in the TO: field (not the BCC field), you will have to write a loop.

$addresses = ['[email protected]','[email protected]','[email protected]'];
foreach($addresses as $address){
   mail($address, $subject, $message, $headers);
}

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

The simplest fix

...
// build mail body
    $body = "Hello,\n\n
You have an enquiry from the website, please see the details below:\n\n 
Name: $name_field\n
Hospital/institution: $hospital_field\n
Department: $department_field\n 
E-Mail: $email_field\n 
Tel: $tel_field\n 
Wishlist:\n $wish_list"; 
    mail($to, $subject, $body);
    mail($to2, $subject, $body);
    echo "Thanks";
...

Comma separated To:

Whilst mail will allow you to send using a comma separated list of recipients this will not preserve their privacy. This is why I have used two calls to mail() so that they cannot see others email addresses.

Using BCC:

Using a BCC: with mail() requires passing in the headers parameter. This is not recommended - see below.

Suggestions

I would not recommend using the mail() function directly. Use SwiftMailer or PHPMailer as they provide more flexibility, safety and a better programming API.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

You could also do:


$to = "[email protected], [email protected]";
mail($to, $subject, $body);

Hope it helps

Upvotes: 2

J0HN
J0HN

Reputation: 26921

mail accepts comma-separated list of recipeinets, as stated in Manual. So just set $to to something like

$to = "[email protected],[email protected]";

Refer to RFC2822 for more details on valid email address specifications.

Upvotes: 21

Emanuel Landeholm
Emanuel Landeholm

Reputation: 1408

PHP mail takes additional headers. Use Bcc: [email protected]

Upvotes: 1

Related Questions