从我的网页发送电子邮件时出错

时间:2015-11-26 18:06:59

标签: javascript php forms email popup

我已经创建了一个网页,在将有效的电子邮件地址插入表单后,使用Javascript和PHP发送电子邮件。

表单执行插入邮件的验证,如果出现错误或发送失败,则显示弹出窗口。它工作正常。

如果一切顺利,应该显示绿色弹出窗口,但不会发生。邮件已发送,我看到以下错误(如果我使用#button-send表单):

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

以下是代码:

PHP

<?php 
require 'mandrill.php';
$send_email_to = "contacts@email.it";
$email_subject = "Feedback landing page";

function send_email($email)
{
  global $send_email_to;
  global $email_subject;
  $headers = "MIME-Version: 1.0" . "rn";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
  $headers .= "From: ".$email. "rn";
  $message = "<strong>Email = </strong>".$email."<br>";


try{
$mandrill = new Mandrill("mandrillcode"); 

 //this is not useful
 /*$message = array(
        'html' => '<p><strong>Email = </strong>'.$email.'</p>',
        'text' => 'Example text content',
        'subject' => 'Feedback landing page ',
        'from_email' => $email,
        'from_name' => $email,
        'to' => array(
            array(
                'email' => 'contacts@email.it',
                'name' => 'Recipient Name',
                'type' => 'to'
            )
        )
);

$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
    print_r($result); */                   


$template_name = 'autoresponder';

 $template_content = array(
        array(
            'name' => 'example name',
            'content' => 'example content'
        )
    );

$message = array(
           'html' => '<p>this is a test message with Mandrills PHP wrapper!</p>',    
           'subject' => 'Feedback email',
    'from_email' => 'contacts@email.it',

    'to' => array(
                  array(
                        'email' => $email, 
                        'name' => 'Recipient 1'
                       )
                 ),
     'merge_vars' => array(
            array(
                'rcpt' => $email,
                'vars' => array(
                    array(
                        'name' => 'merge2',
                        'content' => 'merge2 content'
                    )
                )
            )
        )

    );        

print_r($mandrill->messages->sendTemplate($template_name, $template_content, $message));        
  return true;
}

catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}


}

function validate($email)
{
  $return_array = array();
  $return_array['success'] = '1';

  $return_array['email_msg'] = '';

  if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'inserire email';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'inserire email valida';  
    }
  }




  return $return_array;
}

$email = $_POST['email'];

$return_array = validate($email);

if($return_array['success'] == '1')
{       
        send_email($email);      
}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>

JS

$(document).ready(function(){   
$('#button-send').click(function(event){        

    event.preventDefault();                         
    $.ajax({

        type: 'POST',

        url: 'send_form_email.php',

        data: $('#email-form').serialize(),

        dataType: "json",

        success: function(html) {

            if(html.success == '1')
            {

                console.log("#button-send-1 html.success 1");                   
                $('.formdone').show("slow").delay(5000).hide("slow");                                       
            }

            else
            {
                console.log("#button-send-1 html.success not 1");
                $('.formfail').show("slow").delay(5000).hide("slow");                                                                               
            }                   

            console.log("success");

        },

        error: function(XMLHttpRequest, textStatus, errorThrown){

            console.log("#button-send-1 not html.success");

  console.log("textStatus = "+textStatus);
  console.log("XMLHttpRequest= "+XMLHttpRequest);
  console.log("errorThrown= "+errorThrown);

        }
    });     
});


$('#button-send-2').click(function(event){

    event.preventDefault();

    $.ajax({
        type: 'POST',
        url: 'send_form_email.php',
        data: $('#email-form-2').serialize(),

        success: function(html) {                           
            if(html.success == '1')
            {
                console.log("#button-send-2 html.success 1");                   
                $('.formdone2').show("slow").delay(5000).hide("slow");

            }

            else
            {

                console.log("#button-send-2 html.success not 1");
                $('.formfail2').show("slow").delay(5000).hide("slow");

            }                   
            console.log("#button-send-2 success");

        },

        error: function(){
            console.log("#button-send-2 not html.success");
        }

    });
}); 
});

编辑: 我在php文件中添加了error_reporting(E_ALL);,我发现有些变量未初始化。特别是$async $ip_pool $send_at。我&#39;我补充说:

$async = false;
$ip_pool = null;
$send_at = null;

之前

$result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);

但没有任何改变。查看我的提供程序的php错误日志,不存在关于php的新错误。

我还注意到第一次发送没有用$mandrill->messages->send($message, $async, $ip_pool, $send_at);。只需要第二个。我删除了第一个但没有任何变化。

正确代码:

<?php 
error_reporting(E_ALL);
require 'mandrill.php';
$send_email_to = "contacts@docgem.it";
$email_subject = "Feedback landing page DocGem";
function send_email($email)
{
  global $send_email_to;
  global $email_subject;
  $headers = "MIME-Version: 1.0" . "rn";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";


$headers .= "From: ".$email. "rn";
  $message = "<strong>Email = </strong>".$email."<br>";


try{
$mandrill = new Mandrill("mfSC_K7XBO8Kj5nW0VobjQ");        

$template_name = 'docgem autoresponder';

 $template_content = array(
        array(
            'name' => 'example name',
            'content' => 'example content'
        )
    );

$message = array(
           'html' => '<p>this is a test message with Mandrills PHP wrapper!</p>',    
           'subject' => 'Feedback landing page DocGem',
    'from_email' => 'contacts@docgem.it',

    'to' => array(
                  array(
                        'email' => $email, 
                        'name' => 'Recipient 1'
                       )
                 ),
     'merge_vars' => array(
            array(
                'rcpt' => $email,
                'vars' => array(
                    array(
                        'name' => 'merge2',
                        'content' => 'merge2 content'
                    )
                )
            )
        )

    );



json_encode($mandrill->messages->sendTemplate($template_name, $template_content, $message));


  return true;
}

catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}


}

function validate($email)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['email_msg'] = '';
  if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'inserire email';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'inserire email valida';  
    }
  }

  return $return_array;
}


$email = $_POST['email'];

$return_array = validate($email);

if($return_array['success'] == '1')
{

    send_email($email);

}
header('Content-type: text/json');
echo json_encode($return_array);

?>

1 个答案:

答案 0 :(得分:1)

在JSON相关输出中,特别是当它将被javascript读取时,你需要使用错误报告desabled,并删除die();在echo json_encode之后...

<?php
error_reporting(0);
...
...
...
header('Content-Type:application/json;');
echo json_encode($return_array);