使“收件人”字段仅显示一个收件人

时间:2011-09-28 08:04:53

标签: php html forms email

这是我在单击表单上的“提交”按钮时使用的PHP代码。

目前,当用户输入电子邮件地址时,该消息将发送给这些人。在我测试时检查我的电子邮件时,我注意到在“收件人:”字段中,它显示了所有收件人。

我想隐藏其他收件人的电子邮件。我该怎么做?

<?php   
    // request variables like beneath // important 
    // $name=$_REQUEST["name"];   
    // multiple recipients 
    $to = $_REQUEST['email1'] . ', '; // note the comma 
    $to .= $_REQUEST['email2'];   
    // subject 
    $subject = 'Movie World: Stay Tuned!';

    $message = '
        <html>
            <head>
                <title>Movie World</title>
            </head>
            <body>
                <p>Movie World you would not wanna miss!</p>
            </body>
        </html>
    ';

    // To send HTML mail, the Content-type header must be set 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers 
    $headers .= 'From: Movie World <movie@movieworld.com>'; 

    // Mail it
    mail($to, $subject, $message, $headers);
?>

<script type="text/javascript">
    alert("Success! Thank you for your enquiry.");
    <!--
        window.location = "form.html"
    //-->
</script>

2 个答案:

答案 0 :(得分:2)

您需要为BCC,Blind Carbon Copy

添加额外的标题

$headers .= 'Bcc: ' . $_REQUEST['email2'] . "\r\n";

答案 1 :(得分:0)

如果您不想使用bcc,则需要使用foreach或类似内容。

使用收集一组电子邮件地址 <input type="text" name="address[]" />

然后循环遍历它们:

foreach($_POST['address'] as $email) :
    $to = null;
    $to = $email;
    // validate the email
    // set up the headers
    mail($to, $subject, $message, $headers);
endforeach;

可能值得查看proper mail library以获得更高级的内容。

请注意,除非您通过限制电子邮件数量,正确清理等来正确控制输入,否则这种形式可能成为垃圾邮件发送者的金矿。