Jump to content

Simple email issue


SteveH

Recommended Posts

Hello

 

This has to be one of the simplest PHP scripts I have seen (a contact form). It has two simple files:

 

<HTML>
<HEAD>
</HEAD>
<BODY>

<form action="/cf.php" method="post">
<p><label for="input-email">Your Email: </label><br/><input type="text" name="email" id="input-email" size="40"/></p>
<p><label for="input-message">Your Message: </label><br/><textarea type="text" name="message" id="input-message" rows="10" cols="40"></textarea></p>
<p><button type="submit" name="submit">Send</button></p>
</form>

</BODY>
</HEAD>

 

and the PHP processing script called cf.php:

 

<?php

if (isset($_GET['success'])) {

?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php

} else {

if (!empty($_POST['email']) && !empty($_POST['message'])) {

	$to = 'my_personalAddress@yahoo.com'; 
	$subject = 'Contact from my_site.com';
	$message = $_POST['message']."\r\n\r\nSender IP: ".$_SERVER["REMOTE_ADDR"];
	$headers = 'From: '.$_POST['email']."\r\n".
				'Reply-To: '.$_POST['email']."\r\n";
	mail($to, $subject, $message, $headers);

	header("Location: /cf.php?success"); 

} else {

	?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php

}

}

?>

 

Can I ask, please, what might be the reason why emails are not sent (despite the 'success' message).

 

Thanks

 

Steve

Link to comment
Share on other sites

You would need an email server setup in order to use the mail() function. Been there, done that. Best thing to do would be to buy a .com address that actually has that mail server built into it. That's what I did, and I'll tell ya man, it was the BEST move I've made so far =)

Link to comment
Share on other sites

If I'm not mistaken:

 if (isset($_GET['success'])) {
//...
header("Location: /cf.php?success"); 

 

Will cause an infinite loop, You are saying 'IF success = true, go to ?success', and it will go 'if success=true, go to ?success'.

 

And no, Permissions will not be relevant to mail() unless your MX records for your domain are not set up (May require contact to support department to check)

Link to comment
Share on other sites

<?php

if (isset($_GET['success'])) {

   ?><p><span style="color:green;">Message successfully sent.</span> Thank you!</p><?php

} else {

   if (!empty($_POST['email']) && !empty($_POST['message'])) {

      $to = 'my_personalAddress@yahoo.com'; 
      $subject = 'Contact from my_site.com';
      $message = $_POST['message']."\r\n\r\nSender IP: ".$_SERVER["REMOTE_ADDR"];
      $headers = 'From: '.$_POST['email']."\r\n".
               'Reply-To: '.$_POST['email']."\r\n";
      mail($to, $subject, $message, $headers);

      header("Location: /cf.php?success"); 

   } else {

      ?><p><span style="color:red;">Error detected.</span> Please make sure you have filled all fields. Press back button to go back.</p><?php

   }

}

echo "isset::Success: ".isset($_GET['success'])."<br>\n";
echo "Success: ".$_GET['success']."<br>\n";
echo "Email: ".$_POST['email']."<br>\n";
echo "Message: ".$_POST['message']."<br>\n";
echo "To: ".$to."<br>\n";
echo "Subject: ".$subject."<br>\n";
echo "Message: ".$message."<br>\n";
echo "Headers: ".$headers."<br>\n";

?>

Link to comment
Share on other sites

Thanks for your replies again!

 

OK, I have deleted everything from my cf.php file and copied and pasted exactly what Shadowice posted here (but inserted the my correct email addresses).

 

The form, incidentally, is here:

 

http://proofreading4students.com/emailTest.html

 

Before I came to this forum, I deliberately typed in a wrong email address (missing the @ etc) and I was told to press the back button. Now, with ShadowIce's script, I do not get that - so it is not picking up on errors. But I do not get any server messages or on screen errors.

 

Thanks.

 

Steve

Link to comment
Share on other sites

Maybe I know what I'm talking about,  maybe I don't... but shouldn't it look like this?

<form action="cf.php" method="post"> (Without the /)

 

/ Means document root, so if it is not then for compatibility it should be removed. A valid example would be /usr/home/htdocs/folder/cf.php

Link to comment
Share on other sites

Hello

 

Many thanks for your posts again.

 

I have removed this:

 

echo "isset::Success: ".isset($_GET['success'])."<br>\n";echo "Success: ".$_GET['success']."<br>\n";echo "Email: ".$_POST['email']."<br>\n";echo "Message: ".$_POST['message']."<br>\n";echo "To: ".$to."<br>\n";echo "Subject: ".$subject."<br>\n";echo "Message: ".$message."<br>\n";echo "Headers: ".$headers."<br>\n";

 

and whether I use:

 

<form action="[b]/[/b]cf.php" method="post">

 

or

 

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

 

the 'success' screen tells me the message is sent, but it is not received. Aaahh!

 

Steve

 

 

Link to comment
Share on other sites

You are doing the header() call all the time as long as the $_POST['email'] and $_POST['message'] are not empty..

You need to actually TEST the mail function itself...

if (mail($to, $subject, $message, $headers)) {
      header("Location: /cf.php?success"); 
} else {
header("Location: /cf.php?failed"); 
}

Link to comment
Share on other sites

Hello

 

I have done that, Buddski:

 

//mail($to, $subject, $message, $headers);
//header("Location: /cf.php?success"); // redirects the sender to success page
//} else {

if (mail($to, $subject, $message, $headers)) {
      header("Location: /cf.php?success");
} else {header("Location: /cf.php?failed");
}

 

I have completed the simple form and was redirected to the 'success' page after clicking on 'submit'.

 

The form details should be sent to my own personal address at Yahoo!, but they are not (they are not in my 'Spam' folder, either.

 

Steve

Link to comment
Share on other sites

You may need have some server settings that need adjusting or your host may not accept the use of the mail() function. (I had that with my old host, it would only allow authenticated mail to be sent)

 

And as the PHP Manual says

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

 

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.