PDA

View Full Version : PHP Form Mailer


technel
10-05-2003, 08:57 AM
I used the resma.net tutorial to make a form mailer a LONG time ago and it worked great, but today I redid it, but now it doesn't send. Here is the code:

<?

$recipient = "PRIVATE@PRIVATE.com";
$subject = "Nitrox.uni.cc Submission";
$message = "Name: $name
Email: $email
Message: $msg";

$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];

mail($recipient,$subject,$message,$email);

header("Location: index.php?s=contact:2");

?>

That's send.php, on the other form I just called it up to send it. Here is the URL of the page:

http://www.nitrox.uni.cc/index.php?s=contact

The thanks page and everything works fine, it just doesn't actually send the mail.


PS: Does anyone have a scrip that I can use to check the message and name field that will display:

http://www.nitrox.uni.cc/index.php?s=contact:3

When they aren't filled in? As you can see, I have a javascript one, but that alert is so annoying, a seperate page would be better or even if you could do it like on some web sites were they have it display the form and above it it says that you didn't complete all required fields or something...? Thanks!!

n8thegreat
10-05-2003, 09:03 AM
the final parameter for mail() is not the reply to email, its any additional headers. if you want to make it the reply to thing you have to do

$email = "Reply-to: ".$_POST['email'] ."\r\n";

scoutt
10-05-2003, 09:36 AM
also you message is all backwards. you define the posted variables after you use them. should look like this


$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];

$message = "Name: $name
Email: $email
Message: $msg";

technel
10-05-2003, 01:32 PM
I had a small problem with the Javascript form validator thing, but that's fixed now. I guses the java one is fine.

Anyway, now this is what I have:

<?

$recipient = "private@private.net";
$subject = "Nitrox.uni.cc Submission";
$email = "Reply-to: ".$_POST['email'] ."\r\n";

$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];

$message = "Name: $name
Email: $email
Message: $msg";

mail($recipient,$subject,$message,$email);

header("Location: interact.php?s=contact:2");

?>

But it still isn't sending. It actually proccesses it and goes to the thankyou page, but it doesn't actually send the email! My inbox is not even close to being full, I tried and the email works fine, but the code just won't send!


http://nitrox.uni.cc/index.php?s=contact


PS: Is there a way so I can slide the content over a pixel (a pixel, lol) so its not touching the side? Obviously the middle box is not a table so cellpadding/cellspacing doesn't work on TD. Any ideas?

scoutt
10-05-2003, 01:58 PM
for one you have $email getting overwritten.

$recipient = "private@private.net";
$subject = "Nitrox.uni.cc Submission";
$email = "Reply-to: ".$_POST['email'] ."\r\n";

$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];

$message = "Name: $name
Email: $email
Message: $msg";

mail($recipient,$subject,$message,$email);

header("Location: interact.php?s=contact:2");

that is why the mail isn't getting sent.

also cellpadding and cellspacing works inthe table, so you can use those.

so do this

<?php
$recipient = "private@private.net";
$subject = "Nitrox.uni.cc Submission";
$header = "Reply-to: ".$_POST['email'] ."\r\n";

$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];

$message = "Name: $name
Email: $email
Message: $msg";

mail($recipient,$subject,$message,$header);

header("Location: interact.php?s=contact:2");
?>

technel
10-05-2003, 03:47 PM
Duh you can do it in a table, but the content box isn't a table, its just a table row/column. I've tried, if you put cellpadding in a TD, it doesn't do anything. Look at the code and you should understand.

I tested it on two servers. It worked on the one that I got for free from my internet service provider. The one from B12 didn't work!! It was the exact same files.

Here are stats of the B12 server from the CPANEL:

PHP version 4.3.3
Kernel version 2.4.20-19.7
Apache version 1.3.28 (Unix)
Operating system Linux

Any ideas? :'(

Rydberg
10-05-2003, 04:09 PM
About the layout issue,
Try padding the TD with CSS. <td style="padding-left: 10px;"> would push the contents ten pixels to the right. padding-right, padding-top and padding-bottom can also be used. If you will use the same padding on all edges, padding: 10px; works too.

technel
10-05-2003, 04:18 PM
Exactly what I needed! Thanks! Don't remember learning about that tag when learning HTML/CSS :) Anyway, anyone know anything about the other post?

scoutt
10-05-2003, 07:39 PM
Originally posted by technel
Duh you can do it in a table, but the content box isn't a table, its just a table row/column. I've tried, if you put cellpadding in a TD, it doesn't do anything. Look at the code and you should understand.


what???? it is in a table. you have 3 tables and the content box is in one of those tables. all you had to do it put cellpadding on the very first table and it would have worked.

Rydberg
10-06-2003, 05:46 AM
Using 'cellpadding' in the first table, will pad all the cells of that table, resulting in pretty ugly effects in all but the contents cell.
Using CSS padding gives you more control, and is the way to go.

I tried the script on my server, using the latest code scoutt gave you, together with the submission form, and it works just fine.

So what could be wrong?

1) Did you really change "$recipient = 'private@private.net';" to your e-mail address? In case private@private.net isn't yours.

2) Is the SMTP properly configured on your host machine? If you have no control, ask those who do.

Can't think of anything else right now...

Rydberg
10-06-2003, 06:16 AM
Here is a slighty improved version of the code:


<?php

$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];

$recipient = "your@address.com";
$subject = "Nitrox.uni.cc Submission";
$headers = "From: $name <$email>\nReply-to: $email\nContent-Type: text/plain\n";

$date = date("Y-m-d, H:i:s");

$message = "Sent by: $name\r\n
E-mail: $email\r\n
Message:\r\n
$msg\r\n
\r\n
Date of submission: $date\r\n
Remote IP: {$_SERVER['REMOTE_ADDR']}\r\n";

mail($recipient,$subject,$message,$headers);

header("Location: interact.php?s=contact:2");

?>


It has a few advantages, you can now see the name of the person submitting the form as sender and his/her e-mail address, and not your webhost's default e-mail address. You can also see when it was sent by the server, and the submitter's IP address, good for stopping abuse.
The variables from $_POST could of course be validated and so on, and additional headers could be added, but that's a whole other topic ;)

technel
10-06-2003, 04:27 PM
1) Did you really change "$recipient = 'private@private.net';" to your e-mail address? In case private@private.net isn't yours.

Yes, I changed it. At first when I copied it, I forgot it, but then I went and changed it.

---

2) Is the SMTP properly configured on your host machine? If you have no control, ask those who do

It's on B12 (http://b12.org) servers, so I'd assume that everything is set up correctly.

---

The code is awesome, I can see how it works, but it still doesn't work on the B12 servers. I obviously have PHP and all of that stuff...

---

I would contact B12 but..well..I'm having some issues with them, lol. The admin won't help me unless I use support tickets, with you have to register for and show your plan and stuff, but I have a special plan with everything that I got for free...it's a long story, but I'm trying to explain my situation to the admin :rolleyes:

Aaannnyywayyyss....:confused: :confused: :confused:

technel
10-06-2003, 05:04 PM
Well it's working now..that's weird. But anyway now my account on B12 is going to be terminated if I don't pay them like $210/month, lol. I knew I should have never talked to the admin :P So now I'm scrambling to get a good host. I suppose I will use my internet service provider's free 5 meg account, lol. If anyone knows any good hosting web sites (free), please post them. I would like to get a web site that is just not hosting, because those tend to have awful things. My web site doesn't use much bandwith or anything, but I prefer to have unlimited (eek!) (dont flame me :P). I have to go. Thanks for all the help, it works great.

-Michael

AaronCampbell
10-06-2003, 06:09 PM
Alright, I'm still learning all this php stuff. I have a little programming background in C++, but stuff is so different for the web. Anyway, I look at the code that Rydberg posted, and it makes perfect sense. Exept for one thing. I assume that this is a handler for an HTML form of some kind. Could someone post a little snippet of what that form should look like in order to properly utilize this?

technel
10-06-2003, 07:09 PM
The code was made for the form on my site. The code on my form looks like this:

<form name="cform" method="post" action="send.php">
<font color="red">*</font> Your First Name: <br>
<INPUT TYPE="text" NAME="name" SIZE="40" class="form"> <br><br>
Your Email (for response): <br>
<INPUT TYPE="text" NAME="email" SIZE="40" class="form"> <br><br>
<font color="red">*</font> Message: <br>
<TEXTAREA NAME="msg" ROWS=6 COLS=40 class="form"></TEXTAREA><br><br>
<input type=button class="form" value="Submit" onclick="verify();">
<input type=reset class="form" value="Reset"><br>
</form>

But the "verify" thing is part of the required field checker. That script goes in the head:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.cform.name.value=="") {
themessage = themessage + " - First Name";
}
if (document.cform.msg.value=="") {
themessage = themessage + " - Message";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.cform.submit();
}
else {
alert(themessage);
return false;
}
}
// End -->

</script>

Just take out the verify thing and make that the submit button if you don't want to use that.

BTW, more exact information on what kind of hosting I want can be found here (http://www.nitrox.uni.cc/index.php?subaction=showfull&id=1065484626&archive=&start_from=&ucat=14&).

-Michael

scoutt
10-06-2003, 07:50 PM
Aaron you can use the php script for any form. just remember to change the $_POST[] variables to match the form input names.

also Michael, it is a very bad habit to check in javascript (validate) if they had something in the field before they submitted. always check on the serverside as they can't turn that off.

technel
10-06-2003, 08:09 PM
Well first, there is nothing in the fields. Second, I'm aware that it won't validate if they have java dissabled, but it also will not submit. I would do it using PHP but I really have no clue how to do it in PHP :) I searched on Hotscripts for days straight and didn't come up with a scratch. Do YOU know how to do it? :P


Michael

I wonder why my topics always come out like 4 pages long, hahaha. Must be because of this :cool: But if it makes my site better :rocker:

AaronCampbell
10-06-2003, 10:49 PM
Thanks for the help. I have it working, but there are problems. I want it to tell people thanks for the E-Mail, and forward them to the main page (actually, I'd like to forward them back to where they were before the form, but main is fine). I have a problem though. First, here is the code I use:

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Campbell's</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="MSSmartTagsPreventParsing" content="TRUE" />
<meta http-equiv="refresh" content="5; url=/">
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];

$recipient = "aaroncampbell@sbcglobal.net";
$headers = "From: $name <$email>\nReply-to: $email\nContent-Type: text/plain\n";
$date = date("Y-m-d, H:i:s");
$message = "Sent by: $name\r\n
E-mail: $email\r\n
Message:\r\n
$msg\r\n
\r\n
Date of submission: $date\r\n
Remote IP: {$_SERVER['REMOTE_ADDR']}\r\n";

mail($recipient,$subject,$message,$headers);
header("Location: interact.php?s=contact:2");
?>
<table width="760" border="0" cellpadding="5" cellspacing="0">
<tr>
<td style="text-align:center;"><p class="body">Thank you for your E-Mail.<br />
It has been sucsessfully sent.<br />
You will now be forwarded to the main page. </p>
</td>
</tr>
<tr>
<td><div align="center">
<p class="footer"><a href="index.php">Home</a> | <a href="comingsoon.php">Aaron</a>
| <a href="christen.php">Christen</a> | <a href="babyx.php">Baby 'X'</a>
| <a href="vacations.php">Vacations</a><br />
Contact us at: <a href="mailto:aaroncampbell@sbcglobal.net">aaroncampbell@sbcglobal.net</a></p>
</div></td>
</tr>
</table>
</body>
</html>


And when I click submit, I get this:

Warning: Cannot modify header information - headers already sent by
(output started at /usr/home/sites/www.dikaios.net/web/campbells/php/send.php:1) in
/usr/home/sites/www.dikaios.net/web/campbells/php/send.php on line 24
Thank you for your E-Mail.
It has been sucsessfully sent.
You will now be forwarded to the main page.

Home | Aaron | Christen | Baby 'X' | Vacations
Contact us at: aaroncampbell@sbcglobal.net

Rydberg
10-07-2003, 12:59 AM
You can't use the PHP header function to redirect in this situation, as it would be an instant redirect(no time to display the 'thank you'), and also since you can't send any headers before that header, making any layout troublesome ;) Kind of the same problem as with your thumbnail-script earlier.
You want to use the (X)HTML redirect (fast and reliable), or possibly JavaScript.
This would be a suitable thank you page:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Example.com</title>
<meta http-equiv='refresh' content='5; url=/main.php' />
</head>
<body>
<font>Thank you! The mail was sent, and you're being redirected...</font>
</body>
</html>


So have a separate page with the form, whose 'action' points to a processing-php-file with PHP only, that sends the mail, and then have that php-file send a header() to redirect to the 'thank you' page. And then the thank-you page in turn redirects you back to the main page. So you need in total three or four files. BTW it's no problem to redirect them back to the form page.

In short:

1) Form page, no PHP, submits to ->
2) Porcessing page, PHP only, redirects to ->
3) Thank you page, optionally displays only for a few seconds, and redirects to ->
4) (optional) Main page, maybe back to the Form, anything you'd like.

I hope that makes it clear.

scoutt
10-07-2003, 02:11 AM
Michael,

to validate in php you do it all the time.

<?php
if($_POST['name'] != ""){
$name = addslashes($_POST['name']);
} else {
echo" you forgot a field, hit your back button to redo it.";
}

?>

just like that :) of course you can add any text you want to let them know they forgot a field. that is generic, you can do a lot more than that.

Rydberg
10-07-2003, 05:44 AM
When using the data in e-mails, I don't know why you'd want to addslashes()... It's a plain text e-mail, and no text (as far as I know) can do any harm. addslashes would add annoying slashes throughout the text.. But perhaps the example was just a more general example. :)

scoutt
10-07-2003, 07:46 AM
yeah it was just a general idea, mainly out of habit of using addslashes. ;)

AaronCampbell
10-07-2003, 12:51 PM
Originally posted by scoutt

<?php
if($_POST['name'] != ""){
$name = addslashes($_POST['name']);
} else {
echo" you forgot a field, hit your back button to redo it.";
}

?>



So, would you put the above validation (and validation for the other elements) into a file such as validate.php, and have the submit button run vaidate.php? If so, how can I then tell it to submit the form if it's all ok? I assume that I need something like:

<?php
if($_POST['name'] != ""){
if($_POST['email'] != ""){
if($_POST['subject'] != ""){
if($_POST['message'] != ""){

***code to submit form***

} //end if message
else {
echo" You forgot to fill in your message, hit your back button to redo it.";
} //end else message
} //end if subject
else {
echo" You forgot to fill in your subject, hit your back button to redo it.";
} //end else subject
} //end if Email
else {
echo" You forgot to fill in your E-Mail, hit your back button to redo it.";
} //end else Email
} //end if name
else {
echo" You forgot to fill in your name, hit your back button to redo it.";
} //end else name
?>


Could you tell me if A. I'm right (or even close) and B.what should go where the ***'s are?

scoutt
10-07-2003, 02:47 PM
well you could do it that way but I wouldn't.

if you want to have a generic error message then you could do this

<?php
if($_POST['name'] != "") or ($_POST['email'] != "") or ($_POST['subject'] != "") or ($_POST['message'] != ""){

***code to submit form***

} else {
echo" You forgot to fill some info, hit your back button to redo it.";
exit;
} //end if
?>

then to submit would be whatever you want to do with the form. it all depends on you.

or do them seperate

<?php
if ($_POST['name'] == ""){
echo" You forgot to fill in your name, hit your back button to redo it.";
exit;
}

if ($_POST['email'] == ""){
echo" You forgot to fill in your E-Mail, hit your back button to redo it.";
exit;
}
if ($_POST['subject'] == ""){
echo" You forgot to fill in your subject, hit your back button to redo it.";
exit;
}

if ($_POST['message'] == ""){
echo" You forgot to fill in your message, hit your back button to redo it.";
exit;
}

// if all those return false then run the rest of the code here
//


?>

AaronCampbell
10-07-2003, 05:16 PM
Thanks scoutt. It works wonders. I even figured out how to combine it with a bit of javascript so that if they left a field blank, it tells them, and then 2 seconds later it sends them back. I added this:

echo "<script type=\"text/javascript\"
language=\"javascript\">setTimeout(\"history.go(-1)\",2000);</script>";

below the other echo scripts.

pooof
10-24-2003, 07:27 AM
about that hosting... http://www.b-one.net is good. I use them, and they works fine..

-------------
the pooof