PHP电子邮件表单几乎可以使用

时间:2019-02-14 21:41:40

标签: php html5 forms email contacts

我正在使用带有内置联系表单的HTML5 / CSS模板。我的contact.php文件到位,并且除最重要的部分外,该表格正在按预期工作。电子邮件未发送。我可能缺少必要的一两行。这是表单和php代码,收件人地址为“ name@domain.com”,但是没有邮件到达收件箱。在greengeeks主机上,php版本是7.2。

<?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."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

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

$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
}
?>

1 个答案:

答案 0 :(得分:0)

我尝试了您的代码,它对我有用吗?只需检查所有内容是否正确发布即可。您可能缺少值,例如电子邮件地址。

<?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]\nEmail: [emailaddress]\nMessage: [message]";

$headers = 'From: email@example.com'."\r\n";
$headers .= 'Reply-To: email@example.com'."\r\n";

$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
}
?>