使用PHP邮件头发送电子邮件到多个地址


Use PHP mail header to send email to more than one address

我是这个网站的新手,所以我会尽可能清楚地解释我自己!

我目前正在创建一个web表单的过程中,该表单在提交时发送电子邮件到我在服务器上制作的帐户。

一切都很好,但我想知道是否有一种方法,我可以添加多个帐户提交的表格发送太。

My HTML:

<table style="width:100%">
            <form action="form_complete.php" method="post"> 
                    <tr>
                        <th>
                            <input type="text" required placeholder="Name" name="name" />
                        </th>
                        <th>
                            <input type="text" required placeholder="Email" name="email" />
                        </th>
                    </tr>
                    <tr>
                        <th>
                            <input type="text" placeholder="Contact Number" name="mobile" />
                        </th>
                        <th>
                            <input type="text" required placeholder="Subject" name="subject" />
                        </th>
                    </tr>
                    <tr> 
                        <td colspan="2"><textarea id="ta" name="message" required placeholder="Message"></textarea> 
                    </tr> 
                </table> 
                <input id="submit" type="submit" name="submit" value="submit" /> 

            </form>

form_complete.php

if(isset($_POST['submit'])){
$to = "rtozer@tandtcivils.co.uk"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$mobile = $_POST['mobile']; 
$subject = $_POST['subject'];
$subject2 = "Copy of your form submission";
$message = "Name: " .$name . "'n'n mobile number: " . $mobile . ".'n'n Message:" . "'n'n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "'n'n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header ('Location: http://www.tandtcivils.co.uk/form_complete.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
}

这里的代码是正确的工作时,发送电子邮件到rtozer@tandtcivils.co.uk,但我希望能够添加;dtozer@tandtcivils.co.uk也接收表单提交的副本。

如果你需要我的任何进一步的信息,请留下评论!

提前感谢,山姆

可以有多个TO地址。例如:

$to = "rtozer@tandtcivils.co.uk," . $_POST['email'];

或使用CCBCC标头:

$headers = "From:" . $from;
$headers .= "CC:" . $_POST['email'];

或:

$headers .= "CC:rtozer@tandtcivils.co.uk," . $_POST['email'];

有许多方法来组织您的电子邮件收件人,这取决于您是否希望他们是直接收件人,抄送收件人,或盲目抄送收件人。PHP文档中有很多例子。

这里的主要好处是,与您在代码中尝试的相反,您只需要发送一次电子邮件,而不是多次发送相同的电子邮件。

感谢Reza Mamun试试这个:

//......
//...Other setting goes here....
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
// More headers
$headers .= 'From: My Name <myemail@example.com>'. "'r'n";
//Multiple CC can be added, if we need (comma separated);
$headers .= 'Cc: myboss1@example.com, myboss2@example.com' . "'r'n";
//Multiple BCC, same as CC above;
$headers .= 'Bcc: myboss3@example.com, myboss4@example.com' . "'r'n";
mail($to, $subject, $message,  $headers);
相关文章: