donglv6747 2019-02-14 21:41
浏览 86

PHP电子邮件表单几乎可以[重复]

This question already has an answer here:

I'm using an HTML5/CSS template with a built in contact form. My contact.php file is in place and the form is working as expected except for the most important part. The email is not being sent. I might be missing a necessary line or two. Here's the form and php code, with the recipient address "name@domain.com", but no mail ever reaches the inbox. The php version is 7.2 on a greengeeks host.

<?php
$field_name = $_POST['InputName'];
$field_email = $_POST['InputEmail'];
$field_message = $_POST['InputMessage'];

$mail_to = 'name@domain.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."
";
$body_message .= 'E-mail: '.$field_email."
";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."
";
$headers .= 'Reply-To: '.$field_email."
";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to webmaster@domain.com');
        window.location = 'index.html';
    </script>
<?php
}
?>
</div>
  • 写回答

1条回答 默认 最新

  • douqian9729 2019-02-14 22:03
    关注

    I tried your code and it worked for me? Just check everything is being posted correctly. You're probably missing a value e.g. email address.

    <?php
    
    $field_name = $_POST['InputName'];
    $field_email = $_POST['InputEmail'];
    $field_message = $_POST['InputMessage'];
    
    $mail_to = 'youremail@example.com';
    $subject = 'Message from a site visitor '.$field_name;
    
    $body_message = "From: [from]
    Email: [emailaddress]
    Message: [message]";
    
    $headers = 'From: email@example.com'."
    ";
    $headers .= 'Reply-To: email@example.com'."
    ";
    
    $mail_status = mail($mail_to,$subject,$body_message,$headers);
    
    if ($mail_status) { ?>
        <script language="javascript" type="text/javascript">
            alert('Thank you for the message. We will contact you shortly.');
            window.location = 'index.html';
        </script>
    <?php
    }
    else { ?>
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please, send an email to webmaster@domain.com');
            window.location = 'index.html';
        </script>
    <?php
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch