Php Email Help

Discussion in 'PHP' started by ianhaney, Feb 18, 2013.

  1. #1
    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\n"; 
      $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
      $headers .= "From: ".$email. "\r\n"; 
    $headers .= "Cc: ".$ccemail. "\r\n"; 
     
     
      $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(); 
     
     
    ?>
    Code (markup):
     
    ianhaney, Feb 18, 2013 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    Just seperate the multiple email addresses with a comma
    $send_email_to = "sales@bhwebsites.co.uk,anotheremail@here.co.uk"; 
    Code (markup):
     
    malky66, Feb 18, 2013 IP
    ryan_uk likes this.
  3. ianhaney

    ianhaney Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #3
    Hi

    Thank you for your reply

    I just tried what you said and still not receiving the carbon copy of the email
     
    ianhaney, Feb 18, 2013 IP
  4. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #4
    Are you sure you got the email addresses correct, it's always worked for me that way
     
    malky66, Feb 18, 2013 IP
  5. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #5
    What is in the actual mail headers of the email you receive?
     
    ryan_uk, Feb 18, 2013 IP
  6. ianhaney

    ianhaney Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #6
    Yeah def got the email addresses correct, ahh it is working now think there is just a delay in the emails getting to me, I think my host company are having webmail issues

    Thank you for the reply

    appreciate it
     
    ianhaney, Feb 18, 2013 IP
    malky66 likes this.
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    Side note,
    @mail($send_email_to, $ccemail, $email_subject, $message,$headers); 
    Code (markup):
    $ccemail should not be in there as its already part of your headers or here
    global $ccemail;
    Code (markup):
    so you can remove those bits.
     
    MyVodaFone, Feb 18, 2013 IP