我有一个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"); } ?>I've got a php web form that I'm trying to use on a contact us page. Everything is working as intended except for the ability to send the successful submissions to multiple email addresses. I've tried the following:
$targetEmail = 'email@gmail.com', 'email2@gmail.com';as well as putting the $targetEmail in an array:
$targetEmail = array( 'Email1' => 'email@gmail.com', 'Email1' => 'email2@gmail.com', );but it never submits to both addresses. Any suggestions would be greatly appreciated.
The full code can be found below
<?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"); } ?>最满意答案
您需要使用逗号分隔字符串中的电子邮件地址:
$targetEmail = 'email@gmail.com,email2@gmail.com';对于数组,您尝试使用相同的键创建两个条目,因此它会覆盖它。 相反,使用不同的键:
$targetEmail = array( 'Email1' => 'email@gmail.com', 'Email2' => 'email2@gmail.com', );You need to separate out the email addresses WITHIN the string with the comma:
$targetEmail = 'email@gmail.com,email2@gmail.com';With the array, you're attempting to create two entries with the same key, so it overwrites it. Instead, use different keys:
$targetEmail = array( 'Email1' => 'email@gmail.com', 'Email2' => 'email2@gmail.com', );
更多推荐
$targetEmail,com',php,电脑培训,计算机培训,IT培训"/> <meta name="de
发布评论