Jump to content

.London Domains - £4
Photo

PHP mail form send multiple recipients


  • Please log in to reply
9 replies to this topic

#1 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 24 May 2018 - 21:57

Hi All,

 

I have a PHP mail form that sends an email to one email address. I want it to send to 2 email addresses but I cant seem to get it to work. 

 

Here is the form:

 

www.ealstothane.com/orderonline.htm

 

here is the code:

<?php

if(isset($_POST['email'])) {


    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "dan@elastothane.com";
    $email_subject = "order form";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }




    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['company_name']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }


     


    $name = $_POST['name']; // required
    $company_name = $_POST['company_name']; // required
    $comments = $_POST['comments']; // required


    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';


  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }


    $string_exp = "/^[A-Za-z .'-]+$/";


  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }


  if(!preg_match($string_exp,$company_name)) {
    $error_message .= 'The company name you entered does not appear to be valid.<br />';
  }


  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }


    $email_message = "Form details below.\n\n";


     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }


     


    $email_message .= "name: ".clean_string($name)."\n";
    $email_message .= "company Name: ".clean_string($company_name)."\n";
    $email_message .= "comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$order_from."\r\n".
'Reply-To: '.$order_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>


<!-- include your own success html here -->


'<script>window.location.href = "http://www.elastothane.com/done.htm";</script>';


<?php


}
?>
 
 
 
Many thanks!!!

Edited by Renaissance-Design, 28 May 2018 - 02:04.
Use the code formatting tools, please

  • 0

#2 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 25 May 2018 - 20:26

Please can someone help me?


  • 0

#3 Gibson

Gibson
  • Hefty Member
  • Flogging Gibbo
  • 1,809 posts

Posted 27 May 2018 - 19:51

http://php.net/manua...nction.mail.php

 

Example #5 - "Sending html email"

 

Look at how they add a CC recipient in by using the headers :

$headers[] = 'Cc: birthdayarchive@example.com';

  • 0

#4 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 30 May 2018 - 08:19

Thanks Gibson. Is anything required between the square brackets?
  • 0

#5 Gibson

Gibson
  • Hefty Member
  • Flogging Gibbo
  • 1,809 posts

Posted 30 May 2018 - 14:33

They're using an array for their headers. To keep it simple, in your example you could add 

 

<?php

if(isset($_POST['email'])) {


    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "dan@elastothane.com";
    $email_subject = "order form";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }




    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['company_name']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }


     


    $name = $_POST['name']; // required
    $company_name = $_POST['company_name']; // required
    $comments = $_POST['comments']; // required


    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';


  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }


    $string_exp = "/^[A-Za-z .'-]+$/";


  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The name you entered does not appear to be valid.<br />';
  }


  if(!preg_match($string_exp,$company_name)) {
    $error_message .= 'The company name you entered does not appear to be valid.<br />';
  }


  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }


  if(strlen($error_message) > 0) {
    died($error_message);
  }


    $email_message = "Form details below.\n\n";


     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }


     


    $email_message .= "name: ".clean_string($name)."\n";
    $email_message .= "company Name: ".clean_string($company_name)."\n";
    $email_message .= "comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$order_from."\r\n".
'Reply-To: '.$order_from."\r\n" .

'Cc: somebody@domain.com' . "\r\n" .

'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>


<!-- include your own success html here -->


'<script>window.location.href = "http://www.elastothane.com/done.htm";</script>';


<?php


}
?>
 
 
 
Many thanks!!!

 

 

 

I've added it in after the headers reply to line for you.


  • 0

#6 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 30 May 2018 - 16:11

Hi Gbson,

 

Thank you - it works! The Cc seems to be a bit slow coming through even though the email addresses both live on the same server. 

 

The results it posts in the email look as follows:

 

Order details 

name: Dave
company Name: The Test Company
email: Dave@dave.com
purchase order: ABC123
comments: Please send now!
Product1: Elastoline 6060
Kit1: 444/450KG drum set (A&B)
Colour1: Neutral
Quantity1: 16
Product2: --select--
Kit2: --select--
Colour2: --select--
Quantity2: 
Product3: --select--
Kit3: --select--
Colour3: --select--
Quantity3:

 

 

Is there any way that the email can separate the first 3 selections frome the next three and so on? i.e can I have a space between the selections like this:

 

Order details 

name: Dave
company Name: The Test Company
email: Dave@dave.com
purchase order: ABC123
comments: Please send now!

Product1: Elastoline 6060
Kit1: 444/450KG drum set (A&B)
Colour1: Neutral
Quantity1: 16
 

Product2: --select--
Kit2: --select--
Colour2: --select--
Quantity2: 
 

Product3: --select--
Kit3: --select--
Colour3: --select--
Quantity3:

 

Sorry I am really taking advantage now but you have been so helpful!

 

Thanks


Edited by juicylemons, 30 May 2018 - 17:07.

  • 0

#7 Gibson

Gibson
  • Hefty Member
  • Flogging Gibbo
  • 1,809 posts

Posted 31 May 2018 - 16:43

I may be missing something but I can't see any reference to the "Orders" generator code in your original code, and without that, it's impossible to figure out how to achieve that.


  • 0

#8 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 31 May 2018 - 17:12

Not sure what you mean by orders generator code?
Have a look at the page www.elastothane.com/orderonline.htm and you will what is generating the order - html drop downs as well as free text boxes in the style of a contact form. Does that help?
  • 0

#9 Gibson

Gibson
  • Hefty Member
  • Flogging Gibbo
  • 1,809 posts

Posted 01 June 2018 - 16:53

What I meant was the php backend code that generates the orders section in the email from the dropdown values that are entered in the form. Do you have that?


  • 0

#10 juicylemons

juicylemons
  • Member
  • Newbie
  • 6 posts

Posted 07 June 2018 - 20:31

Hi,

 

There are only 2 bits of code - the PHP as seen above, and the html that creates the form which is as follows:

 

 
 
 
<form name="contactform" method="post" action="trial.php">



<table width="100px">
<tr>
 <td valign="top">
  <label for="name">Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="company_name">Company Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="company_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="telephone">Purchase Order*<br></label>
 </td>
 <td valign="top">
  <input  type="text" name="purchase_order" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Delivery Address / instructions*<br></label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="205" rows="6"></textarea>
 </td>
</tr>




<tr>
 <td valign="top">
<br><br>
<font face="verdana" size=1"" color="#393939">Please select the <b>1st</b> product you require:
<br>
<br>
<select name="product1" id="product_select">
<option>--select--</option>
<option>Elastoprime 3550 metal primer</option>
<option>Elastoprime 3558 concrete primer</option>
<option>Elastoprime 3528 concrete primer</option>
<option> Isoprime 3922 intercoat primer</option>
<option>-----------------------------------------</option>
<option>Elastoline 6050</option>
<option>Elastoline 6060</option>
<option>Elastoline 6070</option>
<option>Elastoline 7065 5 second gel</option>
<option>Elastoline 7065 10 second gel</option>
<option>Elastoline 7065 15 second gel</option>




</select>


</td>
</tr>


<tr>
<td colspan="2">


<br>
<font face="verdana" size=1"" color="#393939">Please select the set/kit size you require:
<br>
<br>
<select name="kit1" id="product_select">
<option>--select--</option>
<option>444/450KG drum set (A&B)</option>
<option>32KG pail set (A&B)</option>
<option>12.5KG (Elastoprime 3558)</option>
<option>21.41KG (Elastoprime 3528)</option>
<option>12.5 (Elastoprime 3550)</option>
<option>5KG (Isoprime 3922)</option>
<option>25KG (Isoprime 3922)</option>


</select>


</td>
</tr>


<tr>
<td colspan="2">


<br>
<font face="verdana" size=1"" color="#393939">Please select the colour you require:
<br>
<br>
<select name="colour1" id="product_select">
<option>--select--</option>
<option>Neutral</option>
<option>Black</option>
<option>Grey</option>
<option>-------------------------</option>
<option>No Colour - I'm a primer!</option>




</select>


</td>
</tr>




<tr>
<td valign="top">
<br><br>
<label for="quantity">Quantity Required</label>
</td>
</tr>
<tr>
<td valign="top">
<input type="text" name="quantity1" maxlength="30" size="30">
</td>
</tr>
<tr>
<td colspan="2">
<br><br>
<font face="verdana" size=1"" color="#393939">-------------------------------------------------------------------------------------------------
<br><br>


<tr>
<td colspan="2">
<font face="verdana" size=1"" color="#393939">Please select the <b>2nd</b> product you require (Don't forget your primer or top coat!):
</td></tr>


<tr>
<td>
<select name="product2"  id="product_select">
<option>--select--</option>
<option>Elastoline 3550 metal primer</option>
<option>Elastoline 3558 concrete primer</option>
<option>Elastoline MMP metal primer</option>
<option>Elastoline ISOprime GRP/intercoat primer</option>
<option>-----------------------------------------</option>
<option>Elastoline 1400 top coat</option>
<option>-----------------------------------------</option>
<option>Elastoline 2266</option>
<option>Elastoline 6050</option>
<option>Elastoline 6060</option>
<option>Elastoline 6070</option>
<option>Elastoline 7001FR 10 second gel</option>
<option>Elastoline 7001FR 45 second gel</option>
<option>Elastoline 7030</option>
<option>Elastoline 7065 5 second gel</option>
<option>Elastoline 7065 10 second gel</option>
<option>Elastoline 7065 15 second gel</option>
<option>Elastoline 7061FR 5 second gel</option>
<option>Elastoline 7061FR 10 second gel</option>
<option>Elastoline 7061FR 15 second gel</option>
<option>Elastoline 7420</option>
<option>Elastoline 7446FR</option>
<option>Elastoline 7850FR</option>
<option>Elastoline CG</option>
<option>Elastoline CG Edge (PU Table Edge)</option>


</select>


</td>




</tr>
<tr>




<td colspan="2">


<br>
<font face="verdana" size=1"" color="#393939">Please select the set/kit size you require:
<br>
<select name="kit2" id="product_select">
<option>--select--</option>
<option>444/450KG drum set (A&B)</option>
<option>32KG pail set (A&B)</option>
<option>1L 1400 Top Coat Kit</option>
<option>12L Primer Kit</option>




</select>
 
 
does this help?

Edited by Renaissance-Design, 11 June 2018 - 13:58.
Use the code formatting tools please.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users