views:

33

answers:

2

hi,

I'm trying to send an email using PHP mail() in a wordpress site. I created an empty page http://www.exam-vision.com/request-flip-up-instructions which template includes the php code shown below. When I post the data, isset($_REQUEST['email']) is false, while I can in firebug that the data have been posted correctly. What can be wrong?

thanks

<?php

if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
    //send email

    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    mail( "<someemail>", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
   echo "<form method='post' action='http://www.exam-vision.com/request-flip-up-instructions'&gt;
   Email: <input name='email' type='text' /><br />
   Subject: <input name='subject' type='text' /><br />
   Message:<br />
  <textarea name='message' rows='15' cols='40'>
  </textarea><br />
  <input type='submit' />
</form>";
}
?>
+2  A: 

Instead of using $REQUEST, just use $_POST since the page is posting to itself and be sure to check if it's set or if it's empty. And rather than using PHP's mail() function use WordPress' wp_mail function.

Alternatively you can choose to use plugins to accomplish this as well. CformsII and Gravity Forms (premium plugin) are just a couple of examples. I've used CformsII and it's quite extensive and feature rich for a free plugin.

hsatterwhite
I've replaced REQUEST by POST, but it still does not work. $_POST['email'] is empty, while I can see it in the POST data in firebug...
jul
I don't know why this is not working correctly, but I will repeat what hsatterwhite said about plugins. In addition to CformsII and Gravity Forms, there's Contact Form 7, Formidable, and one that I developed called ONW Simple Contact Form. If you want a good contact form that's very flexible, go with Contact Form 7. If you want one that works out of the box, use ONW Simple Contact Form. :)
John P Bloch
Remove the logic where you're checking if $_REQUEST or $_POST variables are set/empty. Trying var_dump() on either $_REQUESST $_POST and if you see your data than build it out from there. wp_mail() should work just fine, since it's based off of mail()
hsatterwhite
A: 

Two things: to use your current form, you should 1) check with your host to see if you need to use smtp and authenticate rather than use php mail, and 2), sanitize your input: PHP Secure E-mail.

If you want to use a WP plugin for mail instead - which has many advantages over building your own form - you may also need to use WP Mail SMTP « WordPress Plugins to use smtp rather than php mail, as your host probably is restricting the use of php mail.

songdogtech
I did use a plugin, but since it didn't worked, I tried to do a simple php mail. The mail function works properly, but post data are not received. I posted a new question about this issue. Thanks
jul