简体   繁体   中英

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

I'm new to this site so i'll try and explain myself as clearly as possible!

I'm currently in the process of creating a web form which when submitted sends an email to an account I have made on my server.

All is working fine but was wondering if there was a way i could add multiple accounts for the submitted form to send too.

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.
}

The code here is working correctly when sending an email to rtozer@tandtcivils.co.uk but i want to be able to add; dtozer@tandtcivils.co.uk to recieve a copy of the form submission also.

If you need any further information from me please leave a comment!

Thanks in advance, Sam

You can have multiple TO addresses. For example:

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

Or use CC or BCC headers:

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

Or:

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

There are a number of ways to organize your email recipients, depending on whether you want them to be direct recipients, carbon copy recipients, or blind carbon copy recipients. Lots of examples are available in the PHP documentation .

The key benefit here, as opposed to what you attempted in your code, is that you need only send the email once as opposed to sending the same email multiple times. Just arrange multiple recipients on that one email.

Thanks to Reza Mamun try this:

//......
//...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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM