发送电子邮件到两个地址?

时间:2012-01-20 09:51:01

标签: php email

是否可以使用以下代码将查询电子邮件发送到两个地址?如果是这样,我该怎么做?

<?php if(isset($_GET['emailme']) && $_GET['emailme'] == 'true') { 
    // to and subject
    $to = "info@domain.com"; 
    $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";} ?>

5 个答案:

答案 0 :(得分:16)

mail接受以逗号分隔的食谱列表,如Manual中所述。所以只需将$to设置为

$to = "recipient1@domain.com,recipient2@domain.com";

有关有效电子邮件地址规范的详细信息,请参阅RFC2822

答案 1 :(得分:2)

最简单的修复

...
// 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";
...

以逗号分隔To:

虽然邮件允许您使用逗号分隔的收件人列表发送,但这不会保留其隐私。这就是为什么我使用了两次mail()来电,以便他们看不到其他电子邮件地址。

使用BCC:

BCC:mail()一起使用需要传入headers参数。不建议这样做 - 见下文。

建议

我不建议直接使用mail()功能。使用SwiftMailerPHPMailer可以提供更多灵活性,安全性和更好的编程API。

答案 2 :(得分:2)

你也可以这样做:


$to = "first@example.com, second@example.com";
mail($to, $subject, $body);

希望有所帮助

答案 3 :(得分:1)

PHP邮件需要额外的标头。使用密件抄送:other@email.address

答案 4 :(得分:0)

为了向多个收件人单独发送电子邮件,并将他们的电子邮件发送到TO:字段(而不是BCC字段),您必须编写一个循环。

$addresses = ['user1@domain.com','user2@domain.com','user3@domain.com'];
foreach($addresses as $address){
   mail($address, $subject, $message, $headers);
}