PHP Contact & Email Form Not Sending

I have a Contact Box which calls a .htm file which calls a .php file which is then supposed to send a message via email (or in my case several) then pop up a Thank You Your Email Was Sent message.
I’m not getting errors, and the Thank You message page comes up, but it’s also not working - no email is actually sent (none received at 2 different test addresses).

Note: I replaced the domain with “xxxxxxx” and the email addresses with fakes - to prevent anyone from doing mischief down the road.

Sure would appreciate another set of eyes on this to spot my error(s) …

Thanks in advance!

This is the .htm file:

[code]

Required fields are bold

Your Name:
Subject:
E-mail:
Website:

Do you like this website? Yes No Not sure

How did you find us? -- Please select -- Search Link from a website Word of mouth Other

Your comments:

Powered by PHP form

[/code]

This is the PHP:

[php]<?php
/* Set e-mail recipient */
$myemail = "[email protected],secondemail@bluehostt est.net,[email protected]";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST[‘yourname’], “Enter your name”);
$subject = check_input($_POST[‘subject’], “Write a subject”);
$email = check_input($_POST[‘email’]);
$website = check_input($_POST[‘website’]);
$likeit = check_input($_POST[‘likeit’]);
$how_find = check_input($_POST[‘how’]);
$comments = check_input($_POST[‘comments’], “Write your comments”);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/", $email))
{
show_error(“E-mail address not valid”);
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?://+[\w-]+.[\w-]+)/i", $website))
{
$website = ‘’;
}

/* Let’s prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header(‘Location: http://www.xxxxxxxx.com/hbc-html/hbc-thanks.htm’);
exit();

/* Functions we used */
function check_input($data, $problem=’’)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>

Please correct the following error:

<?php echo $myError; ?> <?php exit(); } ?>[/php]

This is the thank you message:

[code]

<link rel="stylesheet" href="http://xxxxxxxx.com/hbc-css/hbc-styles.css" type="text/css" media="all"> xxxxxxxxxx

Your message was successfully sent!


Thank you for contacting us, we will reply
to your inquiry as soon as possible!


Please Click Here To Return To The xxxxxxx Home Page


[/code]

You are directing to the thank you page regardless of the success of the send. Place the mail function inside of an if

[php]if ( mail())
Header()
Else
Problem[/php]

Thank you for your reply …

So your code should preface the PHP lines 26-50?

That will prevent false assurance of a successful communication - that is good.

Any idea why the emails are not being sent at all?

I’ve got something wrong in there, I presume. :-[

Thanks again.

44-48 is fine actually.

Apparently I am still messing this up as what I did (see code) doesn’t work … :-[

[php]/* Test for error (add “if” and “Else”) then send using mail() function or post “There’s a problem”*/

if ( mail($myemail, $subject, $message));
{
/* Redirect visitor to the thank you page */

header('Location: http://www.xxxxxxx.com/hbc-html/hbc-thanks.htm');

}
Else
{
show_error(“There’s a problem”);
}
exit();[/php]

Doesn’t work how? Is it redirecting or is is showing your error? One of them has to be happening.

I tweaked it 10 different ways & sometimes it sent the Thank You message, sometimes not, but it never sent the Contact info to the list of several email addresses, and it never posted an error message.

So, I added this line at the top:
[php]
$res1 =(“There is a problem.”);[/php]

Then changed the code to this:

[php]/* Test for error (add “if” and “else”) then redirect visitor to the thank you page or post “There’s a problem”*/

if ( mail($myemail, $subject, $message) )

{
header(‘Location: http://www.hbc-bulloch.com/hbc-html/hbc-thanks.htm’);
}
else
{
echo $res1;
}
exit();[/php]

It now wrongly sends the Thank You message & still doesn’t send the emails to the specified email addresses.

WAIT!

I think I know why it doesn’t post the error message - the PHP is not embedded in the HTML nor is the PHP file even in the same folder as the contact.htm - I guess I need to create a separate .htm and .txt for the error message.

Meanwhile, no matter how I tweak this section, no emails are being sent to the listed emails that are supposed to receive the Contact information - which suggests to me that something remains broken there (as well).

What am I missing, please?

Sure appreciate the assist!

Arghhh! >:(

I’m a maroon! :-[

I was using the wrong PHP file - which had a different target address - switched that address to one I can check and the send IS working.

HOWEVER … when I go back to the multi-address version it’s only sending to one address - same as in the single target email address version - the rest are Bluehost email addresses … so I need to look there.

I’m probably not getting the Error because the PHP is working & has no way of knowing that the email at Bluehost isn’t. Sigh.

Thanks for the assist!

Do a test first…

Single file with

[php]<?php

mail(your_email, “test”, " test message");[/php]

And see if it works. Anticipate that these generally go to junk/spam folders.

Also, I would suggest using something like PHPMailer it works better than the default mail function.

All the more reason to use something like PHPMailer. You can then assign whatever email account you want to do the actual sending, including html mail and adding attachments.

I downloaded the zip file from https://github.com/PHPMailer/PHPMailer

I’m looking at this doc & basically see how it works but am unclear how much of the rest of PHPmailer I need to load & where:
https://github.com/PHPMailer/PHPMailer/blob/master/examples/mail.phps

BTW: These are listed on Bluehost as “PHP Extension(s) and Application(s) Available for Installation”:

Mail (1.2.0) Class that provides multiple interfaces for sending emails
Mail2 (0.1.0) Mail, but PHP5 friendly
Mail_IMAP (1.1.0RC2) Provides a c-client backend for webmail.
Mail_IMAPv2 (0.2.1) Provides a c-client backend for webmail.
Mail_Mbox (0.6.3) Read and modify Unix MBOXes
Mail_Mime (1.8.9) Mail_Mime provides classes to create MIME messages.
Mail_Mime2 () Mail_Mime provides classes to create MIME messages.
Mail_mimeDecode (1.5.5) Provides a class to decode mime messages.
Mail_Queue (1.2.7) Class for put mails in queue and send them later in background.

Do they do the same as PHPmailer?

Thanks.

I found this Web site tutorial which appears to answer the how to install question:

https://phpmatters.com/send-email-with-phpmailer/

This could get interesting …

Sponsor our Newsletter | Privacy Policy | Terms of Service