Log in

View Full Version : Resolved form info sent to email using php



pelaej
08-09-2009, 08:25 PM
I am currently working on a website and using wordpress as a cms for it. On the site I am supplying users with a form to sign-up for a newsletter. I am really new to php and am wondering if there is a way that I could have the email address that the user submits sent to my email using php. I found a couple of examples of what to do through a google search but none of them seem to work. I would really appreciate any help!! :)

bluewalrus
08-09-2009, 11:37 PM
Can you post the html you have?

Schmoopy
08-10-2009, 12:26 AM
Something like this should work for you, you'll just need to tweak it a bit to fit your current code:



<?php
$myEmail = 'youremail@here.com';

if(!isset($_POST['email']) || empty($_POST['email'])) {
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="email"/>
<input type="submit" value="Send">
</form>

<?php
}

else{
if(@mail($myEmail,'Email from your site', $_POST['email'])) { // If it fails to send, the @ sign stops errors from being displayed
echo 'Your email address has been sent to the website admin';
}
else {
echo 'Failed to send your email address, please try again later.';
}
}
?>


That should do what you want, but of course you'll have to add in some validation for the email address but I'm sure you can find more about that on google.

Hope this helps.

pelaej
08-11-2009, 01:53 AM
Schmoopy, thank you kindly. It worked!:D