Thread: Php email help

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    156
    Rep Power
    13

    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 Code:
    <?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();


    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    It would be helpful if you told us what the error was or what is not working. You are suppressing errors ('@') so that is not helping you either.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    156
    Rep Power
    13
    Originally Posted by gw1500se
    It would be helpful if you told us what the error was or what is not working. You are suppressing errors ('@') so that is not helping you either.
    Hi

    I am not getting any errors, it says the email has been sent but not receiving the carbon copy of the email

    so it could be this line causing the error

    @mail($send_email_to, $ccemail, $email_subject, $message,$headers);
    return true;
  6. #4
  7. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    remove all those @ signs (not the ones in the email of course)
    and place this above your code.
    i
    PHP Code:
    // only for development
    ini_set('display_errors',1); 
     
    error_reporting(E_ALL); 
    and give us the error

    P.s.
    this is wrong also (apart from the @ sign):
    PHP Code:
    @mail($send_email_to$ccemail$email_subject$message,$headers); 
    it should be
    PHP Code:
    mail($send_email_to$email_subject$message,$headers); 
    see the manual
    the CC is part of the headers

    Also instead of these @ signs you might want to use [PHPNET="if"]if[/PHPNET] clauses like

    PHP Code:
    if(mail($send_email_to$email_subject$message,$headers)){//if true
       
    echo 'mail succesfully sent';
    }else{
      
    //failure

    Last edited by aeternus; February 18th, 2013 at 01:29 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    156
    Rep Power
    13
    So I put the error coding in you gave above the first <?php, is that right or after the first <?php
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by ianhaney
    So I put the error coding in you gave above the first <?php, is that right or after the first <?php
    the error reporting piece is php so between the tags

    PHP Code:
    <?php

    //error reporting on
    ini_set('display_errors',1); 
     
    error_reporting(E_ALL);

    // your script hereunder


    ?>
    Only use that for development!

    P.s. notice I edited my previous post, read it again solution in there probably
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    156
    Rep Power
    13
    Ok cool

    I have managed to get it working a slightly different way by the following

    $send_email_to = "email@email.co.uk,anotheremail@email.co.uk";
  14. #8
  15. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Originally Posted by ianhaney
    Ok cool

    I have managed to get it working a slightly different way by the following

    $send_email_to = "email@email.co.uk,anotheremail@email.co.uk";
    Please read my first post!!

IMN logo majestic logo threadwatch logo seochat tools logo