0

I have used WP-Mail-SMTP to setup the mailer on my WordPress installation. I have tested it by sending email to my private mail.

I am now trying to send the form data using wp_mail() function[WP].

Below is the code.

<?php
if(isset($_POST["button_pressed"])){
// Checking For Blank Fields..
if($_POST["dname"]==""){
echo "Fill All Fields..";
}
else {
$email=$_POST['email'];
$message = $_POST['dname'];
$headers = 'From:' . "\r\n"; // Sender's Email
$headers .= 'Cc:' . "\r\n"; // Carbon copy to Sender

// Message lines should not exceed 70 characters (PHP rule), so wrap it
// $message = wordwrap($message, 70);    

wp_mail("PRIVATE-EMAIL-ADDRESS", $subject, $message, $headers)

echo "Your mail has been sent successfully ! Thank you for your feedback";
}
?>

HTML form

<form>
<div class="row">
<div class="form-group">
<div class="col-sm-6"><input class="form-control" type="text" placeholder="ENTER DOG'S NAME" id="dname" /></div>
<div class="col-sm-6"><input class="form-control" type="text" placeholder="ENTER OWNER'S NAME" id="name" /></div>
</div>
</div>


<input type="submit" value="SUBMIT" />
<input type="hidden" name="button_pressed" value="1" />

</form>

I have tried many tutorials, but none helped. Your comments and suggestions will be very helpful to me. Thanks for your time.

5
  • Is the mailing in general working on your site (to test, use the login password reset maybe) ? If not then you should probably talk to your host about this..
    – Ismail
    Jun 30, 2016 at 19:06
  • I can mail from the Roundcube email client on my website cpanel. It works. Only the code above in one of my pages is not working.
    – Rahul Bali
    Jun 30, 2016 at 19:34
  • Just a little thing, > wp_mail("PRIVATE-EMAIL-ADDRESS" are you sending to the correct email address?
    – Ismail
    Jun 30, 2016 at 19:36
  • yes, its my mailing address. I have checked it again.
    – Rahul Bali
    Jun 30, 2016 at 19:44
  • Can you suggest where I need to place the PHP code in the WP? header.php ? or page.php?
    – Rahul Bali
    Jun 30, 2016 at 19:55

3 Answers 3

3

After fiddling around for hours, I found the problem.

It was the form not the PHP.

<div class="col-sm-6">
<input class="form-control" type="text" placeholder="ENTER DOG'S NAME" id="dname" /></div>
<div class="col-sm-6">
<input class="form-control" type="text" placeholder="ENTER OWNER'S NAME" id="name" /></div>

id attribute needs to be name.

Corrected

<div class="col-sm-6">
<input class="form-control" type="text" placeholder="ENTER DOG'S NAME" name="dname" /></div>
<div class="col-sm-6">
<input class="form-control" type="text" placeholder="ENTER OWNER'S NAME" name="name" /></div>
</div>
1

YIKESSSSSS. There is a lot of things wrong.

  1. You must sanitize $_POST data or very very bad things will happen...

  2. Its best to use single quotes for strings in WordPress

  3. Spacing and indentation does not follow best practices.

  4. Your not doing anything with $_POST['name'] ???

  5. Your calling $_POST["email"] but not passing it through the example form which will result in undefined variable errors ( USE WP DEBUG !!! MAKE IT TRUE )

  6. Its important to check all $_POST values not just 1 because someone can manipulate the form or a spammer could send email without the form missing a POST value causing undefined variable errors again.

  7. wp_mail should have a semi colon at the end

  8. Your missing a closing bracket, the PHP would never work.

  9. The else is not needed

  10. Your better off using wp_die if there is form tampering. If you don't want bad UX for the users then just use the required HTML tag in the input fields.

  11. No need for closing divs after input fields

  12. Best to use css and native bootstrap to handle the uppercase letters.

the php

<?php 

if ( isset( $_POST["button_pressed"] ) ) {
  // Checking For Blank Fields..
  if ( empty( $_POST['dname'] ) || empty( $_POST['name'] ) ) {
    wp_die( __( 'Please fill out all the fields next time.', 'your-text-domain' ) );
  }
  $message  = sanitize_text_field( $_POST['dname'] );
  // $email    = sanitize_email( $_POST["email"] );
  $name     = sanitize_text_field( $_POST['name'] );
  $headers  = 'From:' . "\r\n"; // Sender's Email
  $headers .= 'Cc:' . "\r\n"; // Carbon copy to Sender
  // Send mail
  wp_mail( 'PRIVATE-EMAIL-ADDRESS', $subject, $message, $headers );
  // Show Message and hide the form
  _e( 'Your mail has been sent successfully ! Thank you for your feedback.', 'your-text-domain' ); 
  echo '<style>#contact-form{ display: none; }</style>';
}

?>

and the html markup

<form action="" id="contact-form" method="post">
  <div class="row">
    <div class="form-group">
      <div class="col-sm-6">
        <input class="form-control text-uppercase" type="text" placeholder="<?php _e( 'Enter dogs\'S name', 'your-text-domain' ); ?>" name="dname" required>
      <div class="col-sm-6">
        <input class="form-control text-uppercase" type="text" placeholder="<?php _e( 'Enter owner\'s name', 'your-text-domain' ); ?>" name="name" required>
      </div>
    </div>
  </div>
  <input type="submit" value="SUBMIT" />
  <input type="hidden" name="button_pressed" value="1" />
</form>
0

I have checked your code. I think header not defined properly.Please check the code below:

$current_user  = wp_get_current_user();
$name          = sanitize_text_field($_POST["name"]);
$email_address = sanitize_email($_POST["email_address"]);
$detail        = esc_textarea($_POST["detail"]);
$message       = "Name : " . $name . "\n";
$message .= "Email Address : " . $email_address . "\n";
$message .= "Detail : " . $detail . "\n";
$send_to = '[email protected]';

$headers = 'From: ' . $current_user->user_firstname . ' ' . $current_user->user_lastname . ' <[email protected]>' . "\r\n";

if (wp_mail($send_to, $email_subject, $message, $headers)) {

  wp_redirect(admin_url('admin.php?page=contact-page&msg=success'));

  exit();
} else {

  wp_redirect(admin_url('admin.php?page=contact-page&msg=failed'));

  exit();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.