DewinDell
DewinDell

Reputation: 1028

form to email PHP script

I have this simple script I found online but it does not work. What am I missing?

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];


$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message".

$to = "[email protected]";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');
} catch(Exception $e){
    //problem, redirect to sorry page
    header('Location: sorry.html');
}       
?> 

I am always redirected to the thank-you page but I do not get any emails. All HTML stuff are correct.

Upvotes: 0

Views: 863

Answers (3)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Assuming you have a form similar to the following with named fields: (input fields must be named).

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<form method="post" action="handler.php">

Name: 
<input type="text" name="name" /> <br/>

Your Email: 
<input type="text" name="email" /> <br/>

Message: <br>
<textarea id="body" name="message" cols="100" rows="20"></textarea><br/>
<input type="submit" name="submit" value="Send Email" />

</body>
</html>

This line would cause you problems because of the dot at the end of $message.

$email_body = "You have received a new message from $name.\n".
"\n\n $message".

The dot should be a semi-colon ; such as:

$email_body = "You have received a new message from $name.\n".
"\n\n $message";

In testing, the message I typed did not come through until changed to a semi-colon.

This line echo "error; you need to submit the form!"; should be a die() directive in order to stop executing.

Such as: die("Error. You need to submit the form."); or you could use exit; under your echo also.

Such as:

echo "error; you need to submit the form!";
exit;

PHP (handler.php)

Tested and working on my end using the form shown above.

<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
// echo "Error. You need to submit the form.";
// exit;
    die("Error. You need to submit the form.");
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = $visitor_email;
$email_subject = "New Message";
$email_body = "You have received a new message from $name.\n".
    "\n\n $message";

$to = "[email protected]";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

try{
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
// header('Location: thank-you.html');

echo "Success"; // My echo test

} catch(Exception $e){
    //problem, redirect to sorry page
// header('Location: sorry.html');

echo "Sorry"; // My echo test
}       
?>

Upvotes: 1

leocl1
leocl1

Reputation: 161

Have you checked your spam filter in your e-mail?

Upvotes: 0

Kevin Desai
Kevin Desai

Reputation: 317

If you are on the localhost, you cannot send an email. Only when your code is online the information will be sent.

You can try an easier method called the PHPmailer. Here is an example,

  require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("[email protected]","First Last");

$mail->SetFrom('[email protected]', 'First Last');

$mail->AddReplyTo("[email protected]","First Last");

$address = "[email protected]";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {

echo "Message sent!"; }

Php mailer made it easier for the coder by calling methods and properties, also embedding images to a mail is so easy.

Still if you are on local host you cannot send an email.

Upvotes: 0

Related Questions