适应邮件功能不起作用

时间:2014-04-12 20:01:20

标签: php html forms email

我的问题非常令人沮丧,我脱掉了我的头发。我采取了PHP邮件功能的例子,并尝试调整它以满足我的需要。该示例使用发布到php文件的html表单。该示例可在此链接http://www.freecontactform.com/email_form.php

中找到

我已经采用了这个确切的代码并试图改变输入的名称和它们的数量。

此示例在我的域上测试时非常有效,我已经多次将其发送到我的Gmail,并且连续快速检查它是否被过滤掉垃圾邮件。

我的简化改编版如下:

HTML表单

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    </head>
    <body>
    <form name="results" method="post" action="mailresultstest.php">
    <table>
    <tr>
    <td>email<input type"text" name="email" size="20"></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td>HOME TEAM</td>
    <td><input type="text" name="hometeam" size="20" maxlength="25"></td>
    <td>VS</td>
    <td>AWAY TEAM</td>
    <td><input type="text" name="awayteam" size="20" maxlength="25"></td>
    </tr>
    <tr>
    <td>Round 1</td>    
    <td><input type="text" name="playerhome1" size="20"></td>
    <td><input type="text" name="playeraway1" size="20"></td>
    <td>
    </td>
    </tr>
    <tr>
    <td><input type="submit" value="submit"> </td>

    </table>


    </form>
    </body>
    </head>
    </html>

和php mailresultstest.php

            <?php

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



        // EDIT THE 2 LINES BELOW AS REQUIRED

        $email_to = "mygmailaccount@gmail.com";

     $email_subject = "Match Results";





     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_message."<br /><br />";

          echo "Please go back and fix these errors.<br /><br />";

          die();

       }



      // validation expected data exists

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

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

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

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

          !isset($_POST['playeraway1']) || {

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

      }

       $hometeam = $_POST['hometeam']; // 

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

       $awayteam = $_POST['awayteam']; // 

        $playerhome1 = $_POST['playerhome1']; // 

     $playeraway1 = $_POST['playeraway1']; // 




     $error_message = "";

    if(strlen($hometeam) < 2) {

        $error_message .= 'The hometeam 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 .= "Home team: ".clean_string($hometeam)."\n";

      $email_message .= "Away team: ".clean_string($awayteam)."\n";

      $email_message .= "Round 1 Home player: ".clean_string($playerhome1)."\n";

      $email_message .= "Round 2 Away player: ".clean_string($playeraway1)."\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);  

    ?>



    <!-- include your own success html here -->



    Thank you for contacting us. We will be in touch with you very soon.



    <?php

    }

    ?>

我改编的代码原本要复杂得多,并且有更多的输入字段,包括选择而不是我在这里发布的内容,但无论我做什么来简化它,当我提交表单时,php返回任何内容并且电子邮件未发送

在我最初的改编中,我没有使用任何字符串长度检查或$ error_message,我的原始表单没有电子邮件字段我只是将其添加回来以满足初始if语句和标题。我已经尝试从PHP代码中删除此字段及其存在并使用

   if(isset ($_POST['submit']

但唉无济于事。老实说,我不知道为什么它适用于这个例子,而不是我的版本。我真的看不出任何差异barr变量的名称。

任何帮助将不胜感激。提前致谢

2 个答案:

答案 0 :(得分:1)

我想你可以通过smtp.gmail.com发送电子邮件,这是gmail的邮件服务器

<?php

require_once('../classes/class.phpmailer.php');


$mail = new PHPMailer(true); 

$mail->IsSMTP(); 

try {

 // $mail->Host       = "mail.yourdomain.com"; // SMTP server
 // $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)  NOTE:- remove comment to or for test
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
  $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
  $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
  $mail->Username   = "youremailid@gmail.com";  // GMAIL username
  $mail->Password   = "yourgmailpassword";            // GMAIL passwordAIL password
  $address = "send_to_email_address@gmail.com";
  $mail->AddAddress($address, "friend name");

  //$mail->AddReplyTo("youremail@domain.com","your name");

  $mail->Subject    = "Advanced gmail mailer smtp";


  $mail->MsgHTML(file_get_contents('contents.html'));
  //$mail->AddAttachment('images/1.jpg');      // attachment
  //$mail->AddAttachment('images/2.jpg'); // attachment
  if($mail->Send()){  
        echo "Message Sent OK</p>\n";
  }else{
       echo "Sorry some problem  OK</p>\n";
  }

} catch (phpmailerException $e) {
        echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
        echo $e->getMessage(); //error messages from anything else!
}
?>

您可以使用您的域名电子邮件服务器,或者如果您没有,那么您可以通过gmail smtp服务器尝试此操作。请注意,如果您在Gmail帐户中添加两步验证,则必须删除此两步验证才能发送电子邮件。

您可以从此链接Here

下载我的示例

我跳它会帮助你!

答案 1 :(得分:0)

首先要做的事情:

改变这个:

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

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

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

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

      !isset($_POST['playeraway1']) || {

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

  }

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

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

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

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

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

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

  }

您还需要查看function died()。它不会在函数中的任何位置使用$error参数。