1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to change from email address in php contact form

Discussion in 'PHP' started by Kaleem Ullah Hashmi, Aug 30, 2016.

  1. #1
    I have build a PHP contact form with validation which works fine .
    But receiving problem. when submit i receive email but it is showing wrong email address in from area.
    I want to show my actual address.
    Here is live link http://www.luxpress.co.uk/checking/contact_form.php
    when you submit enter your own email address you will receive confirmation email also.
    check what i receive in from area.
    from: 2.secureserver.net
    to:
    date: Wed, Aug 31, 2016 at 12:38 AM
    I want to show

    Here is my PHP validation form code

    <?php
    // Initialize variables to null.
    $name ="";        //Sender Name
    $email ="";     //Sender's email ID
    $purpose ="";    //Subject of mail
    $message ="";    //Sender's Message
    
    $nameError ="";         
    $emailError ="";
    $purposeError ="";
    $messageError ="";
    $successMessage ="";
    
    // Additional headers
    
    $headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
    
    //On submitting form below function will execute
    
    if(isset($_POST['submit']))
      {
      // checking null values in message
        if (empty($_POST["name"])){
            $nameError = "Name is required";
          }
       else {
           $name = test_input($_POST["name"]);
           // check name only contains letters and whitespace
           if (!preg_match("/^[a-zA-Z ]*$/",$name)){
                $nameError = "Only letters and white space allowed";
             }
         }
    // checking null values in message
       if (empty($_POST["email"])) {
           $emailError = "Email is required";
          }
       else {
          $email = test_input($_POST["email"]);
          }
    // checking null values in message  
       if (empty($_POST["purpose"])) {
          $purposeError = "Purpose is required";
         }
       else {
         $purpose = test_input($_POST["purpose"]);
        }
    // checking null values in message
       if (empty($_POST["message"])) {
          $messageError = "Message is required";
         }
       else {
         $message = test_input($_POST["message"]);
        }
      // checking null values in all fields
    if( !($name=='') && !($email=='') && !($purpose=='') &&!($message=='') )
    
      {// checking valid email
        if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
       
            $header= $name."<". $email .">";
                    $headers = "CONTACT FORM";
         /* Let's prepare the message for the e-mail */
            $msg = "Hello! $name
    
    Thank you...! For Contacting Us.
    
    Name: $name
    E-mail: $email
    Purpose: $purpose
    Message: $message
    This is a Contact Confirmation mail.
    We Will contact You as soon as possible.";
    
    $msg1 = " $name Contacted Us.
    
    Here are some information about $name.
    
    Name: $name
    E-mail: $email
    Purpose: $purpose
    Message: $message ";
    
    /* Send the message using mail() function */
      if(mail($email, $headers, $msg ) && mail("kuhashmi@gmail.com", $header, $msg1 ))
        {
        $successMessage = "Thank you and that Red Dragon Digital Services received their enquiry and someone will get back to them.";
        }
      }
    else { $emailError = "Invalid Email"; }
    
    }
    }
    // function for filtering input values
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
    
    ?>
    PHP:

     
    Kaleem Ullah Hashmi, Aug 30, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well... You're not at all using the From: header, so I'd suggest adding it, with the email you want to send from. Also, you're using quite a bit of regex to get heck email - php has gotten a FILTER function which can do this for you.
     
    PoPSiCLe, Aug 30, 2016 IP
  3. Kaleem Ullah Hashmi

    Kaleem Ullah Hashmi Active Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Thanks for your reply. Can you plz guide me or help to write header code. Because i have very little idea about header.
    I appreciate if you paste the code here for header so i can use it properly.
     
    Kaleem Ullah Hashmi, Aug 30, 2016 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    sarahk, Aug 30, 2016 IP
  5. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #5
    You'll be better off using PHPMailer.
     
    Blizzardofozz, Sep 5, 2016 IP
    ThePHPMaster likes this.