我的PHP电子邮件脚本不起作用?

时间:2014-10-29 09:29:48

标签: php post

所以我制作了一个发送电子邮件的脚本,它使用post方法,因为它使用另一种发布信息的编程语言,php接收并发送它。现在这个工作直到几次尝试之后还没有发送?没有收到任何内容,我也不记得更改它。

这是脚本:

    <?php
if(isset($_POST['username'])) {

    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['username']) ||
        !isset($_POST['emailto']) ||
        !isset($_POST['emailsubject']) ||
        !isset($_POST['emailmessage'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $username = $_POST['username']; // required
    $emailto = $_POST['emailto']; // required
    $emailsubject = $_POST['emailsubject']; // required
    $emailmessage = $_POST['emailmessage']; //  required

    $error_message = "";
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$username)) {
    $error_message .= 'The 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($emailsubject) < 2) {
    died($error_message);
  }
  if(strlen($emailmessage) < 2) {
    died($error_message);
  }

    $email_message = "This information was sent by a computercraft user:\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }


    $email_message .= "Username: ".clean_string($username)."\n";
    $email_message .= "Subject: " .clean_string($emailsubject)."\n";
    $email_message .= "Message: \n".clean_string($emailmessage)."\n";


// create email headers
$headers = 'From: ' . $username . "\r\n".
'Reply-To: '.$username."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($emailto, $emailsubject, $email_message, $headers);  
?>

{
    "True",
}

<?php
}
?>

Array

(     [type] =&gt; 8     [message] =&gt;未定义的变量:注释     [file] =&gt; /customers/c/5/1/dannysmc.com/httpd.www/ccdb/email.php     [line] =&gt; 31 )

// mail($ emailto,$ emailsubject,$ email_message,$ headers);
?&GT;

{     &#34;真&#34 ;, }

我在底部添加了一个真正的表,因为接收它的程序采用了这种类型的表。

如果你能提供帮助那就太棒了,谢谢

1 个答案:

答案 0 :(得分:1)

更改您的代码的最后一部分,以了解有关错误的更多信息

//创建电子邮件标题

$headers = 'From: your-email-address'."\r\n".
'Reply-To: your-email-address'."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($emailto, $emailsubject, $email_message, $headers)){
   echo 'TRUE';
}else{
   echo '<pre>';
   print_r(error_get_last()); //it will show you the error. Remove it on production 
}
?>