0

I am working on a simple contact form and the php script is not sending an email to my email... The script is running succesfully and it produces the HTML output seen in the code below. I am trying to figure it out, yet I just cannot get it to send the email. I have looked at tutorials and appear to have the same exact code as a lot of the tutorials out there. I am using a form to get user's input seen here,

    <!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dummy Code - Contact</title>
</head>

<body>

<form action="send_simpleform.php" method="post">
<p>Your name<br />
<input name="sender_name" type="text" size="30" /></p>
<p>Email<br  />
<input name="sender_email" type="text" size="30" /></p>
<p>Message<br />
<textarea name="message" cols="30" rows="5"></textarea></p>
<input name="submit" type="submit" value="Submit" />
</form>

</body>
    </html>

My PHP script can be seen here as well,

<?php
$msg = "Email sent from www.dummycode.com\n";
$msg .= "Sender's Name:\t $_POST[sender_name]\n";
$msg .= "Sender's E-mail:\t $_POST[sender_email]\n";
$msg .= "Sender's Message:\t $_POST[message]\n";
$to = "[email protected]";
$subject = "DummyCode.com";
$headers = "From: My web site <www.dummycode.com>\n";
$headers .= "Reply to: $_POST[sender_email]\n";
mail($to, $subject, $msg, $headers);
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dummy Code Contact Sent</title>
</head>

<body>

<h1>The following email has been sent</h1>

<p>Your Name:<br />
<? echo "$_POST[sender_name]"; ?>
<p>Your Email Adress:<br />
<? echo "$_POST[sender_email]"; ?>
<p>Message:<br />
<? echo "$_POST[message]"; ?>
</p>
</body>
</html>

I know I am pretty knew at this and if this is a stupid error on my part, then I'm sorry. I am pretty dumb. Thanks in advance. Please don't hate.

-Henry

5
  • 1
    You can't do something like this $msg .= "Sender's Name:\t $_POST[sender_name]\n";, you have to use something like this $msg .= "Sender's Name:\t ".$_POST[sender_name]."\n"; Commented Mar 8, 2013 at 21:41
  • @HamZaDzCyberDeV: And will this fix the issue? Or is this unrelated? Commented Mar 8, 2013 at 21:44
  • 1
    @HamZaDzCyberDeV actually, you can also do this: "Sender's Name:\t {$_POST[sender_name]}\n" Commented Mar 8, 2013 at 21:53
  • I don't recommend using mail() anyway, the PEAR class Mail is more robust and reliable for this stuff. Commented Mar 8, 2013 at 21:56
  • Recommendation: PHP's mail() function is pretty awful. I suggest using a decent library like phpMailer if you want to send mail from a PHP program. It will make your code easier to work with, more reliable, and has many more features. Commented Mar 8, 2013 at 21:57

3 Answers 3

1

The only thing you can do in your script, is check the return value of the mail() function.

If true, the message was successfully accepted for delivery. However, that does not mean that the message was actually sent.

Simple example:

if (mail(...))
{
  // mail was accepted for delivery
}
else
{
  // not accepted
}
Sign up to request clarification or add additional context in comments.

5 Comments

And the best way to do this is?
var_dump(mail($to, $subject, $msg, $headers)); ?
@HamZaDzCyberDeV : What does that do? I am a bit new to PHP, could you explain that a little bit?
$result = mail(...); and use an if statement to distinguish between the 2 options. I have added a simple example.
@HenryHarris mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. var_dump() just give out information about the result whether it is true or false or anything else ...
0

You should take a look at swiftmailer. Setup is fast an simple and it's VERY well documented. Error handling is top notch!

Comments

0

Well, I have had situations where mails seemed sent but I can't see it in the inbox I'm sending it to. So here are a few things I do:

  1. Don't send from localhost except you have a mail server that can send outbound messages
  2. Preferably, send or test your mail script on your remote server
  3. Ensure the senders email address is same as the originating domain
  4. Don't use reply emails that are different from sender's email - it could be eaten up as a spam or junked by most email clients
  5. Verify the allowed smtp port of your server and set that
  6. Verify the smtp server and set that
  7. Provide actual credentials for the sending email
  8. Place the user's email somewhere in the body of the message with a mailto: link so you could quickly reply them. (If they are sending to you using the script). If you are sending to them, no need specifying a different reply-to
  9. Ensure to specify the encoding charset, newline character and possibly word-wrap features that should word
  10. Try using a tested PHP Class from phpclasses.org

I take them one at a time and usually get my mails sent doing what's right. In your code, try adjusting these lines and testing too:

$headers = "From: My web site <www.dummycode.com>\n"; //Same as originating domain or email (just for tweaks)
$headers .= "Reply to: $_POST[sender_email]\n"; //Should be same as sender's email for a try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.