php 向两个地址发送电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8939463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:49:13  来源:igfitidea点击:

Send email to two addresses?

phpemail

提问by Rob

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";} ?>

回答by J0HN

mailaccepts comma-separated list of recipeinets, as stated in Manual. So just set $toto something like

mail接受以逗号分隔的配方列表,如手册中所述。所以只需设置$to为类似

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

Refer to RFC2822for more details on valid email address specifications.

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

回答by Sudhir Bastakoti

You could also do:

你也可以这样做:


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

Hope it helps

希望能帮助到你

回答by Treffynnon

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:

逗号分隔 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.

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

Using BCC:

使用 BCC:

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

使用BCC:withmail()需要传入 headers 参数。不推荐这样做 - 见下文。

Suggestions

建议

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

我不建议mail()直接使用该功能。使用SwiftMailerPHPMailer,因为它们提供了更大的灵活性、安全性和更好的编程 API。

回答by Emanuel Landeholm

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

PHP 邮件需要额外的标题。使用密件抄送:[email protected]

回答by KH42

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.

为了将电子邮件分别发送给多个收件人,并将他们的电子邮件放在 TO: 字段(而不是 BCC 字段)中,您必须编写一个循环。

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