【问题标题】:Error showing json_encode alert with phpmailer使用 phpmailer 显示 json_encode 警报时出错
【发布时间】:2018-06-05 18:48:39
【问题描述】:

我的 jquery 文件中有一个函数帖子,它将数据插入数据库并发送一封邮件。在没有 phpmailer 的情况下工作正常,显示警报,但是当我将 phpmailer 代码警报不起作用(但插入数据库并发送电子邮件)时,有人知道为什么会发生这种情况,抱歉英语不好,谢谢帮助。

更新:我已经收到更改 json_encode 以返回 web 服务并更改警报(响应)的警报;返回警报(响应);在 post 代码上,但需要在关闭 post 方法后发出警报,如果不这样做,则返回警报不会出现,有些想法为什么需要在方法 post 之后的第二个警报才能获得返回,谢谢。

我的邮政编码

$.post('insert',{val: message,name: $("#firstName").val(),desktop_id: $("#desktop_id").val(),permis: $("#permis").val(),phone: $("#phone").val(),mail: $("#mail").val()}, function (response) {
    //feedback
    alert(response);
});

我的网络服务

$app->post('/insert', function(){
require_once('db/dbconnect.php');
$name = $_POST['name'];
$phone= $_POST['phone'];
$phone=(int)$phone;
$mail =$_POST['mail'];
$desktop_id = $_POST['desktop_id'];
$desktop_id=(int)$desktop_id;
$permis=$_POST['permis'];
$date = date("Y-m-d");
$data=array("name"=>$name,"phone"=>$phone,"mail"=>$mail,"desktop_id"=>$desktop_id,"permis"=>$permis,"date"=>$date);
$client=$db->client();
$result = $client->insert($data);
if($result){
$response = 'Success';
}
else{
$response = 'Error';
}
//I remove the variables data
include_once("phpmailer/PHPMailerAutoload.php");

$To = $destinatarios;
$Subject = $assunto;
$Message = $value;
$Host = 'mail.'.substr(strstr($usuario, '@'), 1);
$Username = $usuario;
$Password = $senha;
$Port = "587";
$mail = new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = $Message;

$mail-> IsSMTP(); // telling the class to use SMTP

$mail-> Host = $Host; // SMTP server

$mail-> SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

$mail-> SMTPAuth = true; // enable SMTP authentication

$mail-> Port = $Port; // set the SMTP port for the service   server

$mail->addAttachment('mpdf/temp/doc.pdf');

$mail-> Username = $Username; // account username 

$mail-> Password = $Password; // account password

$mail-> SetFrom($usuario, $nomeDestinatario);
$mail-> Subject = $Subject;
$mail-> MsgHTML($body);
$mail-> AddAddress($To, "");
if(!$mail-> Send())
{
}
else 
{
}
echo json_encode($response, JSON_UNESCAPED_UNICODE);
});

【问题讨论】:

  • 错误信息是什么?
  • 电子邮件发送成功,但警报不再起作用

标签: php jquery web-services post phpmailer


【解决方案1】:

当您设置$response = 'Error: '. print($mail->ErrorInfo);$response = 'Success',然后设置echo json_encode($response, JSON_UNESCAPED_UNICODE) 时,您似乎正试图将格式不正确的字符串转换为JSON。

【讨论】:

  • 另外,print 输出到浏览器,它不返回字符串。我怀疑您想要 . 连接运算符,并将其格式化为有效的 JSON。
  • 好的,我只是删除了 phpmailer 的那一部分,以免混淆,当放置 phpmailer 时,带有 var $response 的 if 警报不再起作用,抱歉英语不好
  • 使用$response = array('Message' => 'Error: ' . $mail->ErrorInfo) 之类的东西来表示错误,$response = array('Message' => 'Success')
  • 我明天要试试,谢谢,但是为什么没有 phpmailer 的代码可以工作,很奇怪,谢谢
  • 它无需调用json_encode() 即可工作,因为您尝试编码的内容无法转换为有效的 json 字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 2011-10-22
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多