Php email help

Hi

I have a html contact form in my website and it’s action is using a php file and I am trying to get the contact form to send a email to two different email addresses but can’t get it working correctly

The php file coding the contact form is using is below


<?php


// EDIT THE 2 LINES BELOW AS REQUIRED


$send_email_to = "sales@bhwebsites.co.uk";
$ccemail = "sales@irhwebsites.co.uk";
$email_subject = "Enquiry from the website";


function send_email($name,$email,$email_message)
{
  global $send_email_to;
global $ccemail;
  global $email_subject;


  $headers = "MIME-Version: 1.0" . "\\r\
";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\\r\
";
  $headers .= "From: ".$email. "\\r\
";
$headers .= "Cc: ".$ccemail. "\\r\
";


  $message = "<strong>Email = </strong>".$email."<br>";
  $message .= "<strong>Name = </strong>".$name."<br>";
  $message .= "<strong>Message = </strong>".$email_message."<br>";
  @mail($send_email_to, $ccemail, $email_subject, $message,$headers);
  return true;
}


function validate($name,$email,$message)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';
  $return_array['email_msg'] = '';
  $return_array['message_msg'] = '';


 if($email == '')
  {
        $return_array['success'] = '0';
        $return_array['email_msg'] = 'email is required';
  }
  else
  {
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$/';
        if(!preg_match($email_exp,$email)) {
          $return_array['success'] = '0';
          $return_array['email_msg'] = 'enter valid email.';
        }
  }


  if($name == '')
  {
        $return_array['success'] = '0';
        $return_array['name_msg'] = 'name is required';
  }
  else
  {
         $string_exp = "/^[A-Za-z .'-]+$/";
        if (!preg_match($string_exp, $name)) {
          $return_array['success'] = '0';
         $return_array['name_msg'] = 'enter valid name.';
        }
  }



  if($message == '')
  {
        $return_array['success'] = '0';
        $return_array['message_msg'] = 'message is required';
  }
  else
  {
        if (strlen($message) < 2) {
          $return_array['success'] = '0';
          $return_array['message_msg'] = 'enter valid message.';
        }
  }
  return $return_array;
}


$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];



$return_array = validate($name,$email,$message);
if($return_array['success'] == '1')
{
  send_email($name,$email,$message);
}


header('Content-type: text/json');
echo json_encode($return_array);
die();


?>

Hi ianhaney, Welcome to the forums. :slight_smile:

You can’t add extra bits into the mail() function. See if the answer at the end of this thread from a few days ago helps at all:

three things,


global $send_email_to;  
global $ccemail;  
global $email_subject;  

change to 
global $send_email_to, $ccemail, $email_subject; 

Remove the @ before mail, that suppresses error messages that you may want for debugging.

call the function directly


$name = 'me';
$email = 'me@me.com';
$email_message = 'Hello world';
send_email($name,$email,$email_message); 

That should get you going in the right direction.