skip to Main Content

I am creating a website using WordPress on WAMP server.

I use BlankSlate theme and used Elementor plugin to design.

I completed all the pages, and now the Contact Us form has to be inserted.

The plugin forms did not work for me while sending emails, and my client did not like the layout. So I copied HTML code from a another website and did the CSS part without a plugin.

Now that the designing part is over, in order to send emails, I am trying to implement the PHP.

Elementor did not accept any form PHP. So I decided to code this page completely. I created a child theme and started editing Page.php, renaming it to Page-contact-us.php.

The page design is perfect, but sending emails is neither working through PHP nor through the plugin.

Apart from the code and design, am I missing any mail setup or configuration?

Here’s the code:

    <?php 
    $name=$_POST['name'];`enter code here`
    $email=$_POST['email']; 
    $phone=$_POST['phone']; 
    $company=$_POST['company'];
    $msg=$_POST['msg'];
    $to='[email protected]';
    $subject='Contact form';
    $message= "Name:" 
   .$name."n"."Phone:".$phone."n"."From:".$company."n"."Wrote the 
    following:"."nn".$msg;
    $headers ="MIME-Version:1.0"."rn";
    $headers .= "Content-type:text/html;charset=UTF-8"."rn";
    $headers="From: ".$email;
    ?>

    <form id="contact" action="" method="post">
    <h3>CONTACT US</h3>
    <h4>And we will get back to you. </h4>
    <fieldset>
    <input name="name" placeholder="Your name" type="text" tabindex="1" 
    required autofocus>
    </fieldset>
    <fieldset>
    <input name = "email" name placeholder="Your Email Address" type="email" 
    tabindex="2" required>
    </fieldset>
    <fieldset>
    <input name="phone" placeholder="Your Phone Number " type="tel" 
    tabindex="3"  required>
    </fieldset>
    <fieldset>
    <input placeholder="Your current company" type="text" tabindex="4" 
     required>
    </fieldset>
    <fieldset>
    <textarea placeholder="Type your message here...." tabindex="5" 
    required></textarea>
    </fieldset>
    <fieldset>
    <button name="submit" type="submit" id="contact-submit" data- 
    submit="...Sending">Submit</button>
    </fieldset>
    </form>
     <?php
    if (isset($_POST['submit']))
     { 
     if(mail($to,$subject,$message,$headers))
     {
         echo "Thanks for the email; We will get back to you shortly!";
     }
        else { echo "Failed to send; Please try again";}
     }
     ?>         

3

Answers


  1. Chosen as BEST ANSWER

    The solution for my problem is that the elements that the passed through the form to the php like name, email, phone are the generic terms which wordpress will not allow or it can be said those elements are already used in-built in wordpress. So I changed the elements name to contact_name, contact_email, contact_phone and it works perfectly.


  2. There are good resources available on how to code contact forms. There are a number of things you could do to improve what you have so far. 1) Use wordpress wp_mail to send the mail

    $success = wp_mail( string|array $to, string $subject, string $message, string|array $headers = '', string|array $attachments = array() )
    

    2) As Mohit suggested, you should also have some if then else code. Something like:

    if (form_submitted_securely_and_from_this_site) then {
    wp_mail,
    display success or failure message
    }
    else show contact form

    3) For “form_submitted_securely_and_from_this_site” – I’d suggest adding some code to be sure that any post submissions actually come from your website, from that page. There are a number of pages on the web about how to code secure forms.

    4) Also I suggest having sanitisation and validation on the fields submitted. WordPress has functions to help with that. See https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data.

    Login or Signup to reply.
  3. Your textarea element ( which has placeholder="Type your message here....") and the input element before it (having placeholder="Your current company") don’t have name attributes. Judging from the code at the top of the page these names should be “company” and “msg”. Without those name attributes your form won’t work, since their values won’t be transmitted so that $company=$_POST['company']; and $msg=$_POST['msg']; won’t be defined.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search