首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自适应邮件功能不起作用

自适应邮件功能不起作用
EN

Stack Overflow用户
提问于 2014-04-13 04:01:20
回答 2查看 47关注 0票数 0

我的问题非常令人沮丧,我拔掉了我的头发。我以php邮件函数为例,尝试对其进行调整以满足我的需要。该示例使用一个html表单,该表单将发布到一个php文件。该示例可在此链接http://www.freecontactform.com/email_form.php中找到

我采用了完全相同的代码,并尝试只更改输入的名称和数量。

这个例子在我的域名上进行在线测试时工作得很好,我多次将其发送到我的gmail中,然后快速连续地检查它是否没有被过滤掉是否有垃圾邮件。

我的简化改编版本如下。

HTML表单

代码语言:javascript
运行
复制
    <!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

代码语言:javascript
运行
复制
            <?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代码中删除这个字段和它的现有字段,并使用

代码语言:javascript
运行
复制
   if(isset ($_POST['submit']

但是,遗憾的是,这一切都没有用。老实说,我只是不明白为什么它对于这个例子来说是完美的,而对于我的版本来说却完全不是。我真的看不出变量的名称有什么不同。

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

EN

回答 2

Stack Overflow用户

发布于 2014-04-13 04:24:46

我想你可以通过gmail的邮件服务器smtp.gmail.com来发邮件。

代码语言:javascript
运行
复制
<?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
EN

Stack Overflow用户

发布于 2014-04-13 04:16:27

首先要做的是:

更改此设置:

代码语言:javascript
运行
复制
 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.');       

  }

代码语言:javascript
运行
复制
 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23035471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档