使用PHP邮件标题将电子邮件发送到多个地址

时间:2015-10-04 13:59:29

标签: php

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

我目前正在创建一个Web表单,该表单在提交时会向我在服务器上创建的帐户发送一封电子邮件。

一切正常,但想知道是否有办法可以为提交的表单添加多个帐户以便发送。

我的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 也接收表格提交的副本。

如果您需要我的任何进一步信息,请发表评论!

提前致谢, 山姆

2 个答案:

答案 0 :(得分:1)

您可以拥有多个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 documentation中提供了大量示例。

与您在代码中尝试的内容相反,此处的主要优势在于,您只需要发送电子邮件一次,而不是多次发送相同的电子邮件。只需在一封电子邮件上安排多个收件人即可。

答案 1 :(得分:0)

感谢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);