PHP表单提交到多个电子邮件(PHP form submission to multiple emails)

编程入门 行业动态 更新时间:2024-10-27 05:27:26
PHP表单提交到多个电子邮件(PHP form submission to multiple emails)

我有一个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

本文发布于:2023-08-03 13:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   表单   电子邮件   PHP   emails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!