New to php and need very basic help with mail forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blumpalump
Forum Newbie
Posts: 1
Joined: Mon Jul 14, 2008 11:10 pm

New to php and need very basic help with mail forms

Post by blumpalump »

Hey guys,
I started learning php about 6 hours ago and tried to put together a basic mail script but can't get it to work. I was hoping that someone a little more php savvy than myself could help me out. The code I have on my index.html page is:

<form method="post" action="sendmail.php">
Email: <input type="text" name="newemail">

<input type="submit">
</form>

Then I created a file named sendmail.php which looks like this:

<?php
$ForwardTo="mail@hotmail.com";
$email = $ REQUEST['newemail'];

mail($ForwardTo, "Email Subscribers", "From: $email");
header("Location: thankyou.html");
?>

with the email address obviously different. When I try out the form and click the submit button I get redirected to a 'page not found' page and the submitted info doesn't go through to the specified email account. Any advice?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: New to php and need very basic help with mail forms

Post by jaoudestudios »

This is a common issue.

I would not use the php mail function just like that, as you are open to header injection. This will cause multiple problems, clog up your mail server so you wont be able to send or receive any emails, get your IP listed on many spam lists - so that when you do clear out your million of emails in the mail queue, no one will be able to receive it as your IP will be listed as spam.

Here is a mail class that removes such issues...there is an example of how to use it at the top of the code...
http://www.forum.jaoudestudios.com/view ... ?f=13&t=13
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: New to php and need very basic help with mail forms

Post by califdon »

Do you have a file called "thankyou.html"? It looks like that's what the "not found" is talking aboout.

In your script, is there an underscore (_) in the $_REQUEST[..] reference? Doesn't appear to be one in what you posted here. Also, you really should use $_POST[...] rather than the broader $_REQUEST[..] array. In any case, when you have a problem like this, the first thing you should do is determine for sure what values are in some of the key variables. Since an email is not being received as intended, is it possible that there is nothing in the variable $ForwardTo? To find out, add a temporary debugging line just after the $email=... line:

Code: Select all

 echo "The email variable contains: $email <br />  // DEBUG";
and see what, if anything, it contains. Obviously if there's no email address, it won't send the email. Then, check the manual for mail() to use the proper arguments (you're missing the message body, instead you are putting your "From" information into the body): http://us3.php.net/manual/en/function.mail.php Finally, you can force an error message to be sent to the browser by doing this:

Code: Select all

 mail($ForwardTo, "Email Subscribers", "From: $email") or die("mail function failed!");
Post Reply