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!

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗