...

View Full Version : Need super simple method to send a form to an email address



eday_2010
03-05-2007, 09:03 PM
On my own site, I use a very long pearl script to send the forms on my site to my email. It works fine. However, I am redesigning a site for an organization who want a form. I don't want to use the long complicated script since they are not web saavy, plus I don't know if their hosts server can deal with it. And so far, it's not working correctly

So I need something super simple. PHP, CGI, whatever. I know it can be done in PHP, but I don't know how. It just needs to send the form to their email address. I would use <action="mailto: poo@pee.com" etc>, but that prompts the users email client to open, which I don't want.

So if anyone has something short and sweet (that works) to send a form to an email address, that would be great.

I found one at http://www.3rdcoast.com but it only grabs the first item in the fied and nothing else. Help please! This needs to be done very soon.

rafiki
03-05-2007, 09:19 PM
$email = "$_REQUEST['email']"; //get the email address of person filling out the form
$to = "someone@somedomain.com"; //email address to send to
$subject = "$_REQUEST['subject']"; // get the subject or predetermin one
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$body = "$_REQUEST['message'];"
mail($to, $subject, $body, $headers);
if mail($to, $subject, $body, $headers)
{
echo "mail sent";
}
else
{
echo "mail not sent";
}
need any help customising email me or add me on msn or something

mlseim
03-05-2007, 09:20 PM
Doesn't get much simpler than this ...

You need to use the variables you have defined in your form.
This is just an example. No security here either. But always
define the subject in your code. Don't let the visitor define the subject.



<?php

// define the variables exactly as you have
// named them in your form. This is just an example here:
$user_name = $_POST['user_name'];
$user_email = $_POST['user_email'];
$user_subject = $_POST['user_subject'];
$user_comment = $_POST['user_comment'];

//Who to send the email to (your email address):
$recipient = "youremail@aol.com";

//Email subject line ... hard-coded:
$esubject = "email from website";

//Where to go after the email has been sent:
$redirect="index.html";

$emessage="
$user_subject
--------------------------------
Name: $user_name
Email: $user_email
--------------------------------
Comment:\n $user_comment
--------------------------------

";

$extraheaders = "From: $user_email\nReply-To: $user_email\n\n";
if (! mail($recipient, $esubject, $emessage, $extraheaders))
echo "Mail did not send for some reason.";

header ("location: $redirect");

?>

Inigoesdr
03-05-2007, 11:47 PM
*snip*
if (! mail($recipient, $esubject, $emessage, $extraheaders))
echo "Mail did not send for some reason.";

header ("location: $redirect");
*snip*


If you use that code and sending does actually fail it would not forward and would give you an error about headers already being sent. You need to either die() with the error, forward back to the previous page with a variable that will tell that page to show an error, or use put the header into an "else".

if (!mail($recipient, $esubject, $emessage, $extraheaders))
{
echo "Mail did not send for some reason.";
}
else
{
header ("location: $redirect");
}

rafiki
03-06-2007, 02:10 PM
If you use that code and sending does actually fail it would not forward and would give you an error about headers already being sent. You need to either die() with the error, forward back to the previous page with a variable that will tell that page to show an error, or use put the header into an "else".

if (!mail($recipient, $esubject, $emessage, $extraheaders))
{
echo "Mail did not send for some reason.";
}
else
{
header ("location: $redirect");
}
i already had that in the first post :D

Inigoesdr
03-06-2007, 05:03 PM
i already had that in the first post :D
Ok, but I wasn't responding to your post. Oh, you have two instances of mail() and you forgot the parenthesis for your if() switch.

eday_2010
03-06-2007, 06:04 PM
This is the PHP script I have so far that causes a 500 Internal Server Error:


<?
if ($_POST["Submit"]){

if ($_POST["Name"] and $_POST["Address"] and $_POST["Apt_Suite"] and $_POST["City"] and $_POST["Province"] and $_POST["Postal_Code"] and $_POST["Phone"] and $_POST["Email"] and $_POST["Publication_Title"] and $_POST["#_Copies_English"] and $_POST["#_Copies_French"] and $_POST["Details"]){

mail (
"eday2010@gmail.com",
"Publication Order Request (EN)",
"
Name: ".$_POST['Name']."
Address: ".$_POST['Address']."
Apt/Suite: ".$_POST['Apt_Suite']."
City: ".$_POST['City']."
Province or Territory: ".$_POST['Province']."
Postal Code: ".$_POST['Postal_Code']."
Phone: ".$_POST['Phone']."
E-mail: ".$_POST['Email']."
Publication Title: ".$_POST['Publication_Title']."
# of English Copies: ".$_POST['#_Copies_English']."
# of French Copies: ".$_POST['#_Copies_French']."
Details/Comments: ".$_POST['Details']."
",
"From: ".$_POST['Name']." <".$_POST['Email'].">");

header("Location: http://www.wnstudios.ca/en/thankyou2.html");
exit();
}
?>

and here is the page with the form on it: http://www.wnstudios.ca/en/order-commandez.html

rafiki
03-06-2007, 06:51 PM
you need to escape some""'s with \"$something\" otherwise the string stops i think
i got the 500 and
More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

check server logs

eday_2010
03-06-2007, 07:02 PM
I can't find anything in the error logs. The last errors logged are from the 28th of February. They seem to log once a month. The permission is set correctly on the file, so I am not sure what is wrong.

Inigoesdr
03-06-2007, 07:24 PM
Look for a file called "error_log" in the same folder as the file. You're also missing a } at the end of your code, which I'm guessing is just because you didn't post the whole page and missed it. But that shouldn't be causing a 500 error, is this part of a page with more code? Try setting error_reporting (http://www.php.net/manual/en/function.error-reporting.php)(E_ALL); at the start of your script too.

mlseim
03-07-2007, 02:41 PM
eday ...

I suspect you uploaded that script into your "cgi-bin" directory.

It needs to be in the same directory as your other web pages,
not the cgi-bin directory. That's for Perl scripts not PHP scripts.

eday_2010
03-09-2007, 05:01 PM
I don't get an error with this script being in the same directory as the form page, but it doesn't load the thank you page. it goes to itself and shows a blank page.

CFMaBiSmAd
03-09-2007, 05:30 PM
The code you posted is missing at least one }. Depending on your error_reporting setting, this will either output a fatal parse error message or a blank page. Go back and match up all of your { and }.

Edit: I guess I will be the "security cop" and tell you to not put any unchecked input from the visitor into the header field of the mail(...) function -

"From: ".$_POST['Name']." <".$_POST['Email'].">");

Doing this will allow a spammer to send email to anyone he wants through your mail server. This will both hurt the reputation of your domain/site and get your mail server on spam databases or banned by the major ISP's, like msn/hotmail, yahoo, gmail...

Either test the variables that are being put into the header field to make sure they contain only acceptable values and/or don't put them in the header field at all (place them in the message body and make the From: address your own, sending the email from yourself to yourself.)

eday_2010
03-09-2007, 07:11 PM
I did match up the curly brackets and put in the missing end one. It still only went to a blank page.

CFMaBiSmAd
03-09-2007, 07:50 PM
The following is your submit button from the form -

<input type="submit" name="submitbutton" id="submitbutton2" value="Submit" align="left" />

The following is from your form processing code -

if ($_POST["Submit"]){ There is no match between the name of the variable that is set by the form and what is tested in the code...

eday_2010
03-09-2007, 08:23 PM
It still gives me a blank page and doesn't send the form to my email.


<?
if ($_POST["submitbutton"]){

if ($_POST["Name"] and $_POST["Address"] and $_POST["Apt_Suite"] and $_POST["City"] and $_POST["Province"] and $_POST["Postal_Code"] and $_POST["Phone"] and $_POST["Email"] and $_POST["Publication_Title"] and $_POST["#_Copies_English"] and $_POST["#_Copies_French"] and $_POST["Details"]){

mail (
"eday2010@gmail.com",
"Publication Order Request (EN)",
"
Name: ".$_POST['Name']."
Address: ".$_POST['Address']."
Apt/Suite: ".$_POST['Apt_Suite']."
City: ".$_POST['City']."
Province or Territory: ".$_POST['Province']."
Postal Code: ".$_POST['Postal_Code']."
Phone: ".$_POST['Phone']."
E-mail: ".$_POST['Email']."
Publication Title: ".$_POST['Publication_Title']."
# of English Copies: ".$_POST['#_Copies_English']."
# of French Copies: ".$_POST['#_Copies_French']."
Details/Comments: ".$_POST['Details']."
",
"From: ".$_POST['Name']." <".$_POST['Email'].">");
header( "Location: http://www.wnstudios.ca/en/thankyou2.html" );
exit();
}}
?>

CFMaBiSmAd
03-09-2007, 08:36 PM
Take the following out of your <form tag -

enctype="text/plain"

eday_2010
03-09-2007, 10:08 PM
That did not help. Still leavign a blank page and not emailing anything.

CFMaBiSmAd
03-09-2007, 11:01 PM
What do you get when you view the source of the blank page in your browser? I was going to check this, but you apparently changed the file name from order2.php. If you let us know the name of this file, others could attempt to test it. I am starting to think that .php pages are not parsed on your server.

For debugging, put the following in after your first opening <? tag -

ini_set ("display_errors", "1");
error_reporting(E_ALL);
echo "<pre>";
print_r($_POST);
echo "</pre>";

Back to part of your title - "super simple". Super simple also means no error checking and reporting along with no security.

What you are attempting here is very simple. A form and email code. I actually copied and pasted the form part of your code from the web page and tested this. That is how I discovered that the enctype line was preventing the $_POST data from being set. There is a chance that your form validation javascript is causing this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum