Copy link to clipboard
Copied
I would like to know how do i set my PHP to send message from the website's contact page to more than one email. Please find my current PHP coding below;
<body>
<?php
$name=$_POST['name'];
$contact_number=$_POST['contact_number'];
$email=$_POST['email'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$town=$_POST['town'];
$county=$_POST['county'];
$postcode=$_POST['postcode'];
$propertyType=$_POST['propertyType'];
$propertyArea=$_POST['propertyArea'];
$rearExt= isset($_POST['rearExt'])?"Rear ground extension: Yes":"Rear ground extension: No";
$sideExt= isset($_POST['sideExt'])?"Side ground extension: Yes":"Side ground extension: No";
$twoStoreyRear= isset($_POST['twoStoreyRear'])?"Two-storey rear extension: Yes":"Two-storey rear extension: No";
$twoStoreySide= isset($_POST['twoStoreySide'])?"Two-storey side extension: Yes":"Two-storey side extension: No";
$loft= isset($_POST['loft'])?"Loft conversion: Yes":"Loft conversion: No";
$newBuild= isset($_POST['newBuild'])?"New Build: Yes":"New Build: No";
$flat= isset($_POST['flat'])?"Flat: Yes":"Flat: No";
$other= isset($_POST['other'])?"Other: Yes":"Other: No";
$message=$_POST['message'];
// you can specify which email you want your contact form to be emailed to here
$toemail = "example1@hotmail.com";
$subject = "From Website";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "From: $name <".$email.">\r\n";
$body = "Name: $name\r\n
Email: $email\r\n
Contact No: $contact_number\r\n
Address1: $address1\r\n
Address2: $address2\r\n
Town: $town\r\n
County: $county\r\n
Postcode: $postcode\r\n
Property Type: $propertyType\r\n
Property Area: $propertyArea\r\n
Rear ground extension: $rearExt\r\n
Side ground extension: $sideExt\r\n
Two-storey rear extension: $twoStoreyRear\r\n
Two-storey side extension: $twoStoreySide\r\n
Loft conversion: $loft\r\n
New build: $newBuild\r\n
Flat: $flat\r\n
Other: $other\r\n
Message:\r\n
$message";
if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
{
echo "That is not a valid email address. Please return to the"
." previous page and try again.";
exit;
}
mail($toemail, $subject, $body, $headers);
echo "Thanks for submitting your message";
?>
</body>
</html>
With mail function in php you can do a few different things. The easiest is to add the email to the variable declaration.
Change this:
$toemail = "example1@hotmail.com";
To this:
$toemail = "example1@hotmail.com, example2@hotmail.com";
You could also concatenate the value into the variable for headers to add bcc, cc, reply-to email headers, etc. Currently your code has one header. Well, there's three lines declaring value for the $headers variable, but only the last line is defined, overwritten actua
...Copy link to clipboard
Copied
Create an email alias or forward on your mail server. Add the list of email recipients to that alias. Use the alias in your php script. That way, when you modify your list of recipients, you can manage it in your email application, and not have to change any code.
Copy link to clipboard
Copied
Don't go with the email forward method described for numerous reasons:
forwarded email will be From the forwarded address which may not be the same as the actual From address. Forwarded address doesn't have control over what addresses are in BCC, CC reply-to fields, etc. Basically control over the email headers, which is what scripting allows you to do. It's very easy to create a script and choose what addresses are sent where without having to kludge through the setbacks of logging into mail host and/or mail application (if you're not using web based mail) and making changes there everytime you may want to change what addresses the emails are sent to. The argument of it being easier to change the forwarded addresses in your mail settings vs. changing the php script is invalid. Generally it's easier to change a script (if you know how to) than it would be to log into mail host and hurdle through their interface to change the forwarded addresses. For instance: if you only had a mobile device like an iPhone that didn't allow you to set forwarding then you'd have to login to your email provider and probably click at least 5 links before getting to the email forwarding page to make your changes.
Also the biggest reason of why you shouldn't do it is because then you'll never learn php as a result. That goes back to how generally it's easier to change a script (if you know how to). You see, if you don't practice the developments then you'll never learn. As a result you won't know how to make changes easier by using scripting, which is what I assume you want to do.
I mean... if you're going to use your email application as your main hub for anytime that you want to make a change to how the email is sent then why use scripting in the first place, right?
There's plenty of information on google if you search for the term php email multiple recipients. Do a search and see how many recommendations there are for creating a forward email alias. It's likely you won't find many that recommend that method.
Copy link to clipboard
Copied
Thank you for respond and advice. I would like to learn the PHP coding to allow multiple email but as i am new to this its bit difficult for me. I am learning web design in my spare time. Would you be able to help me with the coding for this?
Much apperciat all your help
Copy link to clipboard
Copied
With mail function in php you can do a few different things. The easiest is to add the email to the variable declaration.
Change this:
$toemail = "example1@hotmail.com";
To this:
$toemail = "example1@hotmail.com, example2@hotmail.com";
You could also concatenate the value into the variable for headers to add bcc, cc, reply-to email headers, etc. Currently your code has one header. Well, there's three lines declaring value for the $headers variable, but only the last line is defined, overwritten actually. This is because the lines after the first header variable aren't concatenated. Take a look at your code below:
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "From: $name <".$email.">\r\n";
It should have a . for the lines of same variable after first definition to concatenate the $headers variable value like below. Look up php concatenate on google for more info.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $name <".$email.">\r\n";
The \r\n at the end of the code represents a line break in the header. You can concatenate, or add to the header variable's value, by adding another line like so. Note the . in the next line, which declares a concatenation of the variable's value. A BCC field with two additional addresses has been added to the email header. You could add another line for cc or any other email header you like. That should get you going.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $name <".$email.">\r\n";
$headers .= "BCC: example2@hotmail.com, example3@hotmail.com";
Copy link to clipboard
Copied
Thank you so much for the coding. It work. I had to remove the 'dot' after the $headers beacuse when i recieved the message the all text were all in one line. Now the message show in paragraphs making it easier to read.
Only problem i experience was that, the email name appear as APACHE in my Inbox but this is not a major problem. I tested the contact page few times and i did not recieve all the message on to my second email address.
Once again thank you so much for all your help.