PHP Send-Mail表单到多个电子邮件地址(PHP Send-Mail form to multiple email addresses)

其他开发 IT屋
百度翻译此文   有道翻译此文
问 题

I'm very new to PHP and am using a basic template 'send-mail' form on a contact page. It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?

<?php     

$mail_to = 'daniel30293@gmail.com'; // specify your email here


// Assigning data from the $_POST array to variables

$name = $_POST['sender_name'];

$mail_from = $_POST['sender_email'];

$phone = $_POST['sender_phone'];

$web = $_POST['sender_web'];

$company = $_POST['sender_company'];

$addy = $_POST['sender_addy'];

$message = $_POST['sender_message'];


// Construct email subject

$subject = 'Web Prayer Request from ' . $name;


// Construct email body

$body_message = 'From: ' . $name . "\r\n";

$body_message .= 'E-mail: ' . $mail_from . "\r\n";

$body_message .= 'Phone: ' . $phone . "\r\n";

$body_message .= 'Prayer Request: ' . $message;



// Construct email headers

$headers = 'From: ' . $name . "\r\n";

$headers .= 'Reply-To: ' . $mail_from . "\r\n";

$mail_sent = mail($mail_to, $subject, $body_message, $headers);


if ($mail_sent == true){ ?>

<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');

window.location = 'prayer-request.php';

</script>

<?php } else { ?>

<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin@bondofperfection.com');

window.location = 'prayer-request.php';
</script>

<?php

    }

?>

Your help is greatly appreciated.

解决方案

You implode an array of recipients:

$recipients = array('jack@gmail.com', 'jill@gmail.com');

mail(implode(',', $recipients), $submit, $message, $headers);

See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php

Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

  • user@example.com
  • user@example.com, anotheruser@example.com
  • User <user@example.com>
  • User <user@example.com>, Another User <anotheruser@example.com>

本文地址:IT屋 » PHP Send-Mail form to multiple email addresses

问 题

我对PHP很陌生,在联系页面上使用基本模板“发送邮件”表单。
当点击“提交”按钮时,我们已经要求将电子邮件发送到多个电子邮件地址。我搜索了&还没有完全找到我需要的东西。我需要在下面的表格中添加什么代码才能将其发送到多个电子邮件地址?

 <?php 

$ mail_to ='daniel30293@gmail.com'; //在这里指定您的电子邮件


//将数据从$ _POST数组分配给变量

$ name = $ _POST ['sender_name'];

$ mail_from = $ _POST ['sender_email'];

$ phone = $ _POST ['sender_phone'];

$ web = $ _POST ['sender_web'];

$ company = $ _POST ['sender_company'];

$ addy = $ _POST ['sender_addy'];

$ message = $ _POST ['sender_message'];


//构造电子邮件主题

$主题='Web祈祷请求'。 $名称;


//构造邮件正文

$ body_message ='发件人:'。 $名称。 “\r\\\
”;

$ body_message。='电子邮件:'。 $ mail_from。 “\r\\\
”;

$ body_message。='电话:'。 $手机。 “\r\\\
”;

$ body_message。='祈祷请求:'。 $消息;



//构造邮件头

$ headers ='From:'。 $名称。 “\r\\\
”;

$ headers。='回复:'。 $ mail_from。 “\r\\\
”;

$ mail_sent = mail($ mail_to,$ subject,$ body_message,$ headers);


if($ mail_sent == true){?>

< script language =“javascript”type =“text / javascript”>
alert('您的祈祷请求已提交 - 谢谢。');

window.location ='prayer-request.php';

< / script>

<?php} else {?>

< script language =“javascript”type =“text / javascript”>
alert('邮件未发送,请通知网站管理员admin@bondofperfection.com');

window.location ='prayer-request.php';
< / script>

<?php

}

?>


非常感谢您的帮助。

解决方案

您爆炸了一个收件人数组:

  $ recipients = array('jack @ gmail .com','jill@gmail.com'); 

mail(implode(',',$ recipients),$ submit,$ message,$ headers);


请参阅PHP:Mail函数参考 - http://php.net/manual/en/function.mail.php



收件人或邮件的收件人。

此字符串的格式必须符合»RFC 2822.一些示例如下:




  • user@example.com

  • 使用者@ example.com , anotheruser@example.com

  • 用户< user@example.com>

  • 用户< user@example.com> ,另一位用户< ; anotheruser@example.com >


本文地址:IT屋 » PHP Send-Mail表单到多个电子邮件地址

官方微信
扫一扫关注IT屋
微信公众号搜索 “ IT屋 ” ,选择关注
与百万开发者在一起