Make 'To' field only show one recipient
This is the PHP code I am using upon clicking 'Submit' button at the form.
Currently, when user inputs the email addresses, the message will be sent to those people. Upon checking my email when I was testing, I noticed that at the 'To:' field, it shows all the recipients.
I would like to hide the rest of开发者_StackOverflow中文版 the recipients' emails. How do I do this?
<?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>
You need to add an additional header for BCC, Blind Carbon Copy
$headers .= 'Bcc: ' . $_REQUEST['email2'] . "\r\n";
If you don't want to use a bcc
then you need to use a foreach
or similar.
Collect an array of email addresses using
<input type="text" name="address[]" />
then loop through them:
foreach($_POST['address'] as $email) :
$to = null;
$to = $email;
// validate the email
// set up the headers
mail($to, $subject, $message, $headers);
endforeach;
might be worth looking at a proper mail library for anything more advanced.
And be careful as this kind of form can be a goldmine to spammers unless you control the input properly by limiting the amount of emails, proper sanitization etc.
精彩评论