...

View Full Version : contact form trouble



metathirteen
01-19-2010, 04:12 PM
im very new to php and im having problems with this contact form. i found a tutorial on youtube for it. im not getting an error when i try it out but im not receiving the email :(. halp?


heres the code:


<?php

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];


mail('email@email.com', 'message', $message, 'From: ' . $name . ' <' . $email . '>');
header('location: sent.html');


?>

and heres where im putting everything
http://switcharoo.agilityhoster.com/contact.html

masterofollies
01-19-2010, 04:48 PM
Do this, create a new script, name it mailform.php
Add this code to it and save, upload and test it. Change $to to your email.


<?php
if ($_POST['submit']) {

$pullemail = $_POST['email'];
$pullmess = $_POST['message'];

$to = "someone@example.com";
$subject = "Test mail";
$message = "$pullmess";
$from = "$pullemail";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
else {
?>
<form method='post' action='mailform.php'>
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>
<?php
}
?>

metathirteen
01-19-2010, 05:23 PM
i tried out the code but it still wont work :(
it wont echo either

sir.jones
01-19-2010, 05:32 PM
Modified from masterofollies code:
try this:

<?php
if ($_POST['submit']) {

$pullemail = $_POST['email'];
$pullmess = $_POST['message'];
$subject = $_POST['subject'];

$to = "someone@example.com";
$subject = "$subject";
$message = "$pullmess";
$from = "$pullemail";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
else {
?>
<form method='post' action=''>
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>
<?php
}
?>

masterofollies
01-19-2010, 06:48 PM
Modified from masterofollies code:
try this:

<?php
if ($_POST['submit']) {

$pullemail = $_POST['email'];
$pullmess = $_POST['message'];
$subject = $_POST['subject'];

$to = "someone@example.com";
$subject = "$subject";
$message = "$pullmess";
$from = "$pullemail";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
else {
?>
<form method='post' action=''>
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>
<?php
}
?>

What was wrong? other than changing the action direction? It'd still load it fine.

sir.jones
01-19-2010, 07:53 PM
Nothing wrong at all my friend, i didn't say any wrong before :)

metathirteen
01-19-2010, 08:17 PM
heres the code you gave me:
http://switcharoo.agilityhoster.com/contact2.php

im still not getting an email :(
does it just not work with gmail?

Egg
01-19-2010, 08:20 PM
<?php
if ($_POST['submit']) {

$pullemail = $_POST['email'];
$pullmess = $_POST['message'];
$subject = $_POST['subject'];

$to = "someone@example.com";
$subject = "$subject";
$message = "$pullmess";
$from = "$pullemail";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
else {
?>
<form method='post' action=''>
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' name='submit' />
</form>
<?php
}
?>

<input type='submit' name='submit' />

the php couldn't check whether the form has been submitted or not because the submit button wasn't named. therefore, if you click the submit button, no action is taken.

metathirteen
01-19-2010, 08:45 PM
still no work :(

sir.jones
01-19-2010, 08:51 PM
Yeah you'r right Egg, i forgot to put name='submit' before.... oh foolish

metathirteen
01-19-2010, 10:39 PM
could it be the host im using wont allow this to work for some reason?
other php things ive made worked on here before though, dont see y this wouldnt. im still not getting an email from any of these scripts :(.

tomws
01-19-2010, 11:04 PM
im still not getting an email from any of these scripts :(.

Are you getting the echo?

masterofollies
01-19-2010, 11:36 PM
Crap haha forgot the name for it. I get the echo "Mail Sent".

metathirteen
01-20-2010, 01:06 AM
yea i get the echo but i dont get the email.
i dont understand why

tomws
01-20-2010, 01:15 AM
If you get the echo, then that confirms it's entering the if statement. Are you using a free account with agilityhoster? Their documentation states that free accounts can't send mail. Many other free web hosting accounts also restrict sending mail.

If you're paying for your account, test the return value for mail(). Something like this:

if ( mail($to,$subject,$message,$headers) )
{
echo "Mail accepted for delivery.";
}
else
{
echo "Mail rejected.";
}

metathirteen
01-20-2010, 04:31 PM
fack :(
thanks for the help though

JasonReynolds
01-20-2010, 04:39 PM
Hey, i just want to point you towards a simple contact form Click Here! (http://codingforums.com/showthread.php?p=912791#post912791)

tomws
01-20-2010, 04:42 PM
Hey, i just want to point you towards a simple contact form

And how is that going to help when the host disallows SMTP connections? :rolleyes:

Egg
01-20-2010, 07:25 PM
<?php
$form = "<form method='post' action='?'>
E-mail: <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' name='submit' />
</form>";
if(!isset($_POST['submit'])) {
echo $form;
} else {
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "XXXXX";
$header = "From: " . $email . "";
if(mail($to, $subject, $message, $header)) {
echo "Message was sent successfully.";
} else {
echo "" . $form . "
<br />
Message could not be sent.";
}
}
?>

this works. i tested it. i recommend you add form validation to increase the general effectiveness of the form.

tomws
01-20-2010, 07:33 PM
this works. i tested it.

How is it going to work if SMTP connections are blocked? That's what happens with the free account at his host.

masterofollies
01-20-2010, 07:36 PM
mail doesn't work on free hosts.

Egg
01-20-2010, 07:58 PM
oops. double post.

Egg
01-20-2010, 08:00 PM
How is it going to work if SMTP connections are blocked? That's what happens with the free account at his host.


<a href="mailto: email@domain.ext">Contact me</a>

I'd recommend they get a host with SMTP unblocked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum