PHP表单提交到多个电子邮件

时间:2016-01-04 16:01:39

标签: php forms

我有一个php网页表单,我正在尝试在联系我们页面上使用。除了能够将成功提交的内容发送到多个电子邮件地址之外,一切都按预期工作。我尝试了以下内容:

$targetEmail = 'email@gmail.com', 'email2@gmail.com'; 

以及将$ targetEmail放入数组中:

$targetEmail = array(
  'Email1' => 'email@gmail.com',
  'Email1' => 'email2@gmail.com',
);

但它从不提交到这两个地址。任何建议都将不胜感激。

完整代码可以在下面找到

<?php
	//****************************************
	//edit here
	$senderName = 'Contact Form';
	$senderEmail = 'admin@gmail.com';
	$targetEmail = 'email1@gmail.com';
	$messageSubject = 'Contact Us Form';
	$redirectToReferer = false;
	$redirectURL = 'http://www.website.com/thankyou.html';
	//****************************************

	// mail content
	$name = $_POST['name'];
	$email = $_POST['email'];

	// prepare message text
	$messageText =	'Name: '.$name."\n".
					'Email: '.$email."\n";

	// send email
	$senderName = "=?UTF-8?B?" . base64_encode($senderName) . "?=";
	$messageSubject = "=?UTF-8?B?" . base64_encode($messageSubject) . "?=";
	$messageHeaders = "From: " . $senderName . " <" . $senderEmail . ">\r\n"
				. "MIME-Version: 1.0" . "\r\n"
				. "Content-type: text/plain; charset=UTF-8" . "\r\n";

	if (preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$targetEmail,$matches))
	mail($targetEmail, $messageSubject, $messageText, $messageHeaders);

	// redirect
	if($redirectToReferer) {
		header("Location: ".@$_SERVER['HTTP_REFERER'].'#sent');
	} else {
		// header("Location: ".$redirectURL);
		header("Location: http://www.website.com/thankyou.html");
	}
?>

1 个答案:

答案 0 :(得分:1)

您需要使用逗号分隔字符串中的电子邮件地址:

 $targetEmail = 'email@gmail.com,email2@gmail.com'; 

使用数组,您尝试使用相同的键创建两个条目,因此它会覆盖它。相反,使用不同的键:

$targetEmail = array(
  'Email1' => 'email@gmail.com',
  'Email2' => 'email2@gmail.com',
);