通过电子邮件发送此表单的内容

时间:2014-11-09 21:08:04

标签: php forms

这是否有用,我想将我的表单内容通过电子邮件发送到email@example.com,但是当我在localhost中运行它时它没有发送,我想知道结果是否会在网上有所不同,我不知道为什么它不能在网上工作。我只想通过电子邮件将名称和消息发送到指定的电子邮件地址。当我发送它没有错误,它确实将我重定向到声明感谢您与我们联系的页面。

<form name="contactform" method="post" action="send_form_email.php">
<input name="name" placeholder="Your Name"></input>
<br>
<textarea name="message" placeholder="Email Us." style="height:100px;">    </textarea>
<input type="submit" id="sub" value="Submit" placeholder="Submit" style=""></input>
 </form>



<?php
if(isset($_POST['message'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@example.com";
$email_subject = "Customer";
function died($error) {
 // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}
if(!isset($_POST['message'])){
 died('We are sorry, but there appears to be a problem with the form you submitted.');       
}
$name = $_POST['name']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
 $error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$message)) {
 $error_message .= 'The message you entered does not appear to be valid.<br />';
}
 $email_message = "Form details below.\n\n";
 function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
'X-Mailer: PHP/' . phpversion();
 ?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>

4 个答案:

答案 0 :(得分:0)

我没有看到对邮件功能的任何调用(http://php.net/manual/en/function.mail.php)。我在你的粘贴中是否错过了,或者这可能是问题?

mail($email_to, $email_subject, $email_message);

答案 1 :(得分:0)

  

我想知道结果是否会在网上不同

这取决于您的服务器配置是否不同,不同的Apache模块,不同版本的PHP等等。

如果不分析这两种环境,就不可能说出来。

  

当我在localhost中运行时,它不会发送

我猜测,根据您提出的问题,您没有设置本地邮件服务器,或设置一些SMTP邮件设置(例如Gmail)。

搜索堆栈以便在localhost上发送邮件,您可以在将其投入野外之前使其正常工作并进行测试。

这是一个开始的例子:
php send mail from localhost

或者,在您的实时服务器/托管上,密码保护站点(或目录,如果是实际站点),将代码上传到密码保护区域,使其不公开,并且自己测试!

  

它会将我重定向到感谢您与我们联系的页面。

它不会将您重定向到任何地方,您的脚本只会显示消息“感谢您与我们联系。我们会尽快与您联系”,因为它位于您脚本的底部。

因此,除了致命错误或没有会阻止脚本的$_POST['message]之外,该消息上方的任何代码都不会对正在显示的消息产生影响。它将始终显示该消息。

您需要进行一些简单的检查以确定邮件是否实际发送 - 例如邮件发送回信“ok”,否则回显“失败”。

答案 2 :(得分:0)

您需要使用mail()来发送数据。在此之前,您需要在localhost或正在测试的服务器上设置SMTP服务器。

答案 3 :(得分:0)

我查看了代码,添加了php邮件功能,在我的网络服务器上在线运行它运行得很好。您正在测试它的环境似乎缺少正在运行的邮件服务器,或者它没有正确设置。

使用此代码可以包含邮件功能,代码中的$ email_message变量存储要通过电子邮件发送的邮件:

<?php
mail("someone@example.com","My subject",$email_message);
?>