关于此,还有很多其他帖子。我已经完成了几个并尝试了数组并在$ to字段等中输入电子邮件,但是如果我指定一封电子邮件,我的表单只会发送。我究竟做错了什么?我也是一个PHP菜鸟。
<?php
// EVALUATION FORM
$your_email = 'email1@address.com, email2@address.com';
session_start();
$errors = '';
$name = '';
$visitor_email = '';
$phone = '';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
///------------Do Validations-------------
if (empty($name) || empty($visitor_email)) {
$errors .= "\n Form was not submitted. Please fill out all the fields. ";
}
if (IsInjected($visitor_email)) {
$errors .= "\n Bad email value!";
}
if (empty($_SESSION['6_letters_code']) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) {
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if (empty($errors)) {
//send the email
$to = $your_email;
$subject = "Free Injury Evaluation form submission from Northwest Injury Centers";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "$name submitted the Free Injury Evaluation form:\n\n" . "Name: $name\n" . "Email: $visitor_email \n" . "Phone: $phone \n" . "IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body, $headers);
header('Location: thankyou.php');
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array(
'(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject, $str)) {
return true;
} else {
return false;
}
}
?>
答案 0 :(得分:3)
您可以提供多个收件人,但是您只能提供一个发件人。由于您$your_email
和$to
都使用$from
,因此您尝试发送包含多个发件人的电子邮件。
答案 1 :(得分:0)
您可以将cc添加到邮件header
,
$headers .= 'Cc: someemail@domain.com' . "\r\n";
这会将副本与to
电子邮件地址一起发送到上述电子邮件。
答案 2 :(得分:0)
这将有效:
$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";
代码中的前提可读性使用数组并将其内嵌到以逗号分隔的字符串:
$recipients = array(
"youremailaddress@yourdomain.com",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page