【问题标题】:PHP EMail Script response message shows html tagsPHP 电子邮件脚本响应消息显示 html 标记
【发布时间】:2015-04-08 19:03:56
【问题描述】:

我有一个 PHP 脚本,可以从 html 联系表单发送电子邮件。电子邮件发送没有错误,但写入网页的响应消息包含 html 以及消息:

{"状态":1,"html":" 谢谢!

"}

任何帮助将不胜感激。这是脚本:

<?php
/*
* Contact Form Class
*/


header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'fred.smith@ntlworld.com'; // Your Email
$message_min_length = 5; // Min Message Length


class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Contact from Your Website'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }


    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }


    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid e-mail address</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }
    }


    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "From: ".$this->name." <".$this->email.">\r\n"
            ."Reply-To: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . phpversion());

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = "<p>Thank You!</p>";
        }
    }


    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>

【问题讨论】:

  • sendRequest() 的最后一行是“echo json_encode($response);”。您看到的输出是 $response 变量的 JSON 格式。这看起来像是为 web api 构建的。

标签: php html email


【解决方案1】:

您自己在以下行添加了这些段落标签:

$this->response_html = "<p>Thank You!</p>";

所以在解析 json 数据时将其从那里删除并添加回来

【讨论】:

【解决方案2】:

像这样使用单引号而不是双引号:

$this->response_html = '<p>Thank You!</p>';

What is the difference between single-quoted and double-quoted strings in PHP?

【讨论】:

  • 感谢您的回复,但使用单引号会导致:{"status":1,"html":"Thank You!\/p>"}
猜你喜欢
  • 2012-08-27
  • 2016-04-25
  • 2020-04-22
  • 1970-01-01
  • 2014-01-20
  • 2021-12-25
  • 1970-01-01
  • 2021-02-06
  • 2012-10-16
相关资源
最近更新 更多