【问题标题】:PHP code not sending e-mailPHP代码不发送电子邮件
【发布时间】:2014-11-18 17:12:24
【问题描述】:

我的网页中有这部分代码,但它不能正常工作。

<?php

 if(isset($_POST['email'])) {




$email_to = "sth@somewhere.com";

$email_subject = "Your email subject line";





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();

        }



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

        }



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$comments = $_POST['comments']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

   if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

      }

$string_exp = "/^[A-Za-z .'-]+$/";

 if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

     }

  if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

     }

  if(strlen($comments) < 2) {

$error_message .= 'The Comments you entered do not appear to be valid.<br />';

      }

  if(strlen($error_message) > 0) {

died($error_message);

     }

$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 .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Comments: ".clean_string($comments)."\n";





 // create email headers

   $headers = 'From: '.$email_from."\r\n".

   'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

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



    ?>

它应该向给定的电子邮件发送电子邮件。 另外,我想让它在发送电子邮件并显示“已发送消息”后显示清晰的字段。 请帮帮我!

添加错误报告后没有任何反应。 这是我的 HTML 表单:

<form name="contactform" method="post" action="send_form_email.php">

                                <p>
                                    <input type="text" name="first_name" maxlength="50" size="30" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-use-title-as-watermark" title="First name">
                                </p>
                                <p>
                                    <input type="text" name="last_name" maxlength="50" size="30" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required wpcf7-use-title-as-watermark" title="Last name">
                                </p>
                                <p>
                                    <input type="text" name="email" maxlength="80" size="30" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email wpcf7-use-title-as-watermark" title="E-mail">
                                </p>
                                <p>
                                    <textarea name="comments" maxlength="1000" cols="25" rows="6" class="wpcf7-form-control  wpcf7-textarea wpcf7-use-title-as-watermark" title="Write your message here.."></textarea>
                                </p>

                                <input type="submit" value="Submit" class="wpcf7-form-control">
                                <div class="wpcf7-response-output wpcf7-display-none"></div>


                            </form>

【问题讨论】:

  • 首先删除mail() 中的@ 符号。您正在掩盖errors 的潜力。你可能没有检查的东西。
  • @Fred-ii- 仍然无法正常工作
  • 您的代码执行依赖于设置的“电子邮件”。通过检查我给你的可点击链接添加错误报告。
  • @Fred-ii- 我知道必须设置我的电子邮件。我刚刚从这里的代码中删除了它。在网站文件中设置了它。
  • 不是你的邮箱,这个$_POST['email']

标签: php html email webforms


【解决方案1】:

mail() 后面缺少右大括号 }

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

?>

修改为:

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

}

?>

设置了error reporting,应该会显示如下内容:

解析错误:语法错误,第 134 行 /path/to/your/send_form_email.php 中的文件意外结束

在文件顶部添加错误报告,这将有助于发现错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应该只在暂存阶段完成,而不是在生产阶段。

【讨论】:

  • 非常感谢。它现在正在发送电子邮件。你能帮我让它显示消息框并清除字段吗?
  • 不客气。这是一个全新的问题,需要 JS。您可以通过勾选我的答案旁边的复选标记来接受答案。 JS 不是我在编码方面的强项。您可以查找“php ajax”。 @user3787300
  • 我知道 JS。我确切地知道我必须添加什么,我只是不知道我应该在哪里添加它。
  • 您可以自己尝试输入。 @user3787300 另外,可能会在表单标签内添加 JS,或者使用提交按钮触发它。
  • +1 你是一个涉水过所有代码的士兵
猜你喜欢
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
相关资源
最近更新 更多