1

I'm fairly new to php and I'm still learning the basics. I created a simple "contact us" form that should send the data to an email address. However, I'm not receiving the email. The "Thank you" message displays correctly, but the email is never sent.

Unfortunately my knowledge in php is slim so I'm having difficulty trouble shooting this one. I did successfully code a simpler form with only one field. That one is sending correctly. Since this form has multiple fields, it seems to be throwing something off.

<?php
if($_POST["submit"]) {
$recipient="myemail@gmail.com";
$subject="Contact Form";
$sender=$_POST["sender"];
$senderEmail=$_POST["senderEmail"];
$message=$_POST["message"];

$mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";

mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

$thankYou="<p>Thank you! Your message has been sent.</p>";}
?>

<?=$thankYou ?>
<form method="post" action="company.php">
<input class="contact" type="text" name="sender" 
placeholder="First Name" size="25">
<input class="contact" type="text" name="last" 
placeholder="Last Name" size="25">
<input class="contact" type="text" name="title" 
placeholder="Title" size="25">
<input class="contact" type="text" name="business" 
placeholder="Business" size="25">
<input class="contact" type="email" name="senderEmail" 
placeholder="Email" size="25">
<input class="contact" type="text" name="phone" 
placeholder="phone" size="25">
<textarea class="contact" name="message" 
placeholder="How can we help you?" rows="4" cols="56"></textarea>
<input class="blu-btn" type="submit" name="submit" 
value="Send Message">
</form>

It's not throwing any errors, I'm just not receiving the email. I've checked spam, tried a separate email, I'm missing something. Thank you so much for your help!

Jessica
  • 11
  • 2

2 Answers2

0

You should check first if your server is truly sending the mail, changing your code a bit:

if($_POST["submit"])
{
    $recipient="myemail@gmail.com";
    $subject="Contact Form";
    $sender=$_POST["sender"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"];

    $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";

    if (mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>"))
    {
        echo "<p>Thank you! Your message has been sent.</p>";
    }
    else
    {
        print_r(error_get_last()["message"]);
    }
}

Take a look into the PHP Documentation for mail() function

Return Values

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.

Probably the server itself isn't properly configured to send email. Is a shared hosting? Or something like?

Kind regards!

wiitohzjeh
  • 337
  • 1
  • 4
  • 15
  • hm.. this isn't returning "true" or "false". This is actually my second form on the site. The first form is working and I am receiving those emails. Since I'm getting those, I'm thinking it's not an issue with the server? The site is hosted through BlueHost. Thank you so much for your help! I really appreciate it. – Jessica May 19 '19 at 02:35
0

The environment where you run this makes all the difference. Mail may not be configured correctly or, some spam filter blocked it. In this case, nothing in your code can make a difference.

If you have control of the server, and you know how, you could check the mail program. If you are limited to only writing code, you have other options. You can use SMTP and send email through an external service. Then you can use mailtrap.io to capture the outbound email. This is a good way to go for debugging and making sure that your code is right.

You can use SwiftMailer if you want to try an alternative mail client.

ryantxr
  • 4,119
  • 1
  • 11
  • 25