PHP的电子邮件不工作,当我点击发送按钮

时间:2018-12-01 12:43:27

标签: php html html5 email html-email

这是我现在正在使用的代码,问题是当我点击“发送电子邮件”时,网站不执行任何操作,并且电子邮件也没有发送给任何人。我尝试了很多东西,但大多数都无法解决。如果有人愿意帮助我,我将不胜感激。谢谢!

<?php


        session_cache_limiter( 'nocache' );
        header( 'Expires: ' . gmdate( 'r', 0 ) );
        header( 'Content-type: application/json' );


        $to         = 'niamul@themerox.com';

        $email_template = 'simple.html';

        $subject    = strip_tags($_POST['subject']);
        $email       = strip_tags($_POST['email']);
        $phone      = strip_tags($_POST['phone']);
        $name       = strip_tags($_POST['name']);
        $message    = nl2br( htmlspecialchars($_POST['message'], ENT_QUOTES) );
        $result     = array();


        if(empty($name)){

            $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong> Name is empty.' );
            echo json_encode($result );
            die;
        } 

        if(empty($email)){

            $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong> Email is empty.' );
            echo json_encode($result );
            die;
        } 

        if(empty($message)){

             $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong> Message body is empty.' );
             echo json_encode($result );
             die;
        }



        $headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
        $headers .= "Reply-To: ". $email . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";


        $templateTags =  array(
            '{{subject}}' => $subject,
            '{{email}}'=>$email,
            '{{message}}'=>$message,
            '{{name}}'=>$name,
            '{{phone}}'=>$phone
            );


        $templateContents = file_get_contents( dirname(__FILE__) . '/email-templates/'.$email_template);

        $contents =  strtr($templateContents, $templateTags);

        if ( mail( $to, $subject, $contents, $headers ) ) {
            $result = array( 'response' => 'success', 'message'=>'<strong>Success!</strong> Mail Sent.' );
        } else {
            $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong> Cann\'t Send Mail.'  );
        }

        echo json_encode( $result );

        die;
    ?>

一切正常,甚至当我尝试输入虚假电子邮件,虚假用户名时出现错误……唯一的问题是当我点击发送消息时,它什么都不做。

0 个答案:

没有答案