doufei2662 2019-05-19 01:31
浏览 180

表单在提交时不会发送电子邮件。 我错过了什么? [重复]

This question already has an answer here:

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
Email: $senderEmail

$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!

</div>
  • 写回答

2条回答 默认 最新

  • douxing2156 2019-05-19 01:41
    关注

    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
    Email: $senderEmail
    
    $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!

    评论

报告相同问题?