Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何使用PHPmailer从WordPress Rest API端点发送电子邮件 function my_send_email($name、$email、$current_user、$status、$subject、$Message){ //这里的大部分代码都来自https://github.com/PHPMailer/PHPMailer/tree/master/examples 需要_once ABSPATH.WPINC'/class phpmailer.php'; 需要_once ABSPATH.WPINC'/class smtp.php'; //生成消息体`在此处输入代码` $body= 我的电子邮件 '; 试一试{ //创建新的PHPMailer实例 $mail=新的PHPMailer; //告诉PHPMailer使用SMTP $mail->isSMTP(); //启用SMTP调试 $mail->SMTPDebug=2; //请求HTML友好的调试输出 $mail->Debugoutput='html'; //设置邮件服务器的主机名 $mail->Host=“mail.example.com”; //设置SMTP端口号-可能是25、465或587 $mail->Port=25; //是否使用SMTP身份验证 $mail->SMTPAuth=true; //用于SMTP身份验证的用户名 $mail->Username=”contact@example.com"; //用于SMTP身份验证的密码 $mail->Password=“1234”; //设置要从谁发送消息 $mail->setFrom('contact@example.com“,”发件人名称“); //将替代答复设置为地址 $mail->addReplyTo('备选方案_contact@example.com“,“备选联系人姓名”); //设置要向谁发送邮件 $mail->addAddress($email,$name); //设置主题行 $mail->Subject='PHPMailer SMTP测试'; //从外部文件读取HTML消息正文,将引用的图像转换为嵌入的, //将HTML转换为基本的纯文本替换体 //$mail->msgHTML(文件获取内容(dirname(uuu文件)。'/contents.html'); $mail->MsgHTML($body); //将纯文本正文替换为手动创建的正文 $mail->AltBody='这是一个纯文本消息正文'; $success=$mail->send(); //发送消息,检查错误 如果($成功){ 返回数组('mailError'=>false,'message'=>'email sent!'.$mail->ErrorInfo,'body'=>$body); }否则{ 返回数组('mailError'=>true,'message'=>'email error!'.$mail->ErrorInfo,'body'=>$body); } }捕获(phpmailerException$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->errorMessage(),'body'=>$body);//来自PHPMailer的漂亮错误消息 }捕获(例外$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->getMessage(),'body'=>$body);//来自其他任何内容的无聊错误消息! } } 函数my_register_endpoints(){ 登记休息路线( “我的命名空间/v1”, “/abc/”, 排列( '方法'=>'发布', “回调”=>“我的终点”, “args”=>数组( ..... ), 'permission\u callback'=>函数(WP\u REST\u Request$Request){ 如果(!用户是否已登录()){ ..... } 返回true; } ) ); } 添加操作('rest\u api\u init','my\u register\u endpoints'); //端点 函数my_end_point(WP_REST_Request$Request){ 全局$当前用户; $sender=wp_get_current_user(); $shortMsg=$request['shortMessage']; $subject=“一些文本”; $email\u result=my\u send\u email(“收件人姓名”、“收件人电子邮件”、$sender$status、$subject$shortMsg); 如果($email_resalt['mailError']==false){ 处理..$items,$item。。 $message='email sent!'; 返回数组('message'=>$message,'items'=>$items,'item'=>$item); }否则{ 返回新的WP_错误('email Error','email-Error','email-Error'),$request['id']); } }_Angularjs_Wordpress_Api_Rest_Phpmailer - Fatal编程技术网 Password=“1234”; //设置要从谁发送消息 $mail->setFrom('contact@example.com“,”发件人名称“); //将替代答复设置为地址 $mail->addReplyTo('备选方案_contact@example.com“,“备选联系人姓名”); //设置要向谁发送邮件 $mail->addAddress($email,$name); //设置主题行 $mail->Subject='PHPMailer SMTP测试'; //从外部文件读取HTML消息正文,将引用的图像转换为嵌入的, //将HTML转换为基本的纯文本替换体 //$mail->msgHTML(文件获取内容(dirname(uuu文件)。'/contents.html'); $mail->MsgHTML($body); //将纯文本正文替换为手动创建的正文 $mail->AltBody='这是一个纯文本消息正文'; $success=$mail->send(); //发送消息,检查错误 如果($成功){ 返回数组('mailError'=>false,'message'=>'email sent!'.$mail->ErrorInfo,'body'=>$body); }否则{ 返回数组('mailError'=>true,'message'=>'email error!'.$mail->ErrorInfo,'body'=>$body); } }捕获(phpmailerException$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->errorMessage(),'body'=>$body);//来自PHPMailer的漂亮错误消息 }捕获(例外$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->getMessage(),'body'=>$body);//来自其他任何内容的无聊错误消息! } } 函数my_register_endpoints(){ 登记休息路线( “我的命名空间/v1”, “/abc/”, 排列( '方法'=>'发布', “回调”=>“我的终点”, “args”=>数组( ..... ), 'permission\u callback'=>函数(WP\u REST\u Request$Request){ 如果(!用户是否已登录()){ ..... } 返回true; } ) ); } 添加操作('rest\u api\u init','my\u register\u endpoints'); //端点 函数my_end_point(WP_REST_Request$Request){ 全局$当前用户; $sender=wp_get_current_user(); $shortMsg=$request['shortMessage']; $subject=“一些文本”; $email\u result=my\u send\u email(“收件人姓名”、“收件人电子邮件”、$sender$status、$subject$shortMsg); 如果($email_resalt['mailError']==false){ 处理..$items,$item。。 $message='email sent!'; 返回数组('message'=>$message,'items'=>$items,'item'=>$item); }否则{ 返回新的WP_错误('email Error','email-Error','email-Error'),$request['id']); } },angularjs,wordpress,api,rest,phpmailer,Angularjs,Wordpress,Api,Rest,Phpmailer" /> Password=“1234”; //设置要从谁发送消息 $mail->setFrom('contact@example.com“,”发件人名称“); //将替代答复设置为地址 $mail->addReplyTo('备选方案_contact@example.com“,“备选联系人姓名”); //设置要向谁发送邮件 $mail->addAddress($email,$name); //设置主题行 $mail->Subject='PHPMailer SMTP测试'; //从外部文件读取HTML消息正文,将引用的图像转换为嵌入的, //将HTML转换为基本的纯文本替换体 //$mail->msgHTML(文件获取内容(dirname(uuu文件)。'/contents.html'); $mail->MsgHTML($body); //将纯文本正文替换为手动创建的正文 $mail->AltBody='这是一个纯文本消息正文'; $success=$mail->send(); //发送消息,检查错误 如果($成功){ 返回数组('mailError'=>false,'message'=>'email sent!'.$mail->ErrorInfo,'body'=>$body); }否则{ 返回数组('mailError'=>true,'message'=>'email error!'.$mail->ErrorInfo,'body'=>$body); } }捕获(phpmailerException$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->errorMessage(),'body'=>$body);//来自PHPMailer的漂亮错误消息 }捕获(例外$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->getMessage(),'body'=>$body);//来自其他任何内容的无聊错误消息! } } 函数my_register_endpoints(){ 登记休息路线( “我的命名空间/v1”, “/abc/”, 排列( '方法'=>'发布', “回调”=>“我的终点”, “args”=>数组( ..... ), 'permission\u callback'=>函数(WP\u REST\u Request$Request){ 如果(!用户是否已登录()){ ..... } 返回true; } ) ); } 添加操作('rest\u api\u init','my\u register\u endpoints'); //端点 函数my_end_point(WP_REST_Request$Request){ 全局$当前用户; $sender=wp_get_current_user(); $shortMsg=$request['shortMessage']; $subject=“一些文本”; $email\u result=my\u send\u email(“收件人姓名”、“收件人电子邮件”、$sender$status、$subject$shortMsg); 如果($email_resalt['mailError']==false){ 处理..$items,$item。。 $message='email sent!'; 返回数组('message'=>$message,'items'=>$items,'item'=>$item); }否则{ 返回新的WP_错误('email Error','email-Error','email-Error'),$request['id']); } },angularjs,wordpress,api,rest,phpmailer,Angularjs,Wordpress,Api,Rest,Phpmailer" />

Angularjs 如何使用PHPmailer从WordPress Rest API端点发送电子邮件 function my_send_email($name、$email、$current_user、$status、$subject、$Message){ //这里的大部分代码都来自https://github.com/PHPMailer/PHPMailer/tree/master/examples 需要_once ABSPATH.WPINC'/class phpmailer.php'; 需要_once ABSPATH.WPINC'/class smtp.php'; //生成消息体`在此处输入代码` $body= 我的电子邮件 '; 试一试{ //创建新的PHPMailer实例 $mail=新的PHPMailer; //告诉PHPMailer使用SMTP $mail->isSMTP(); //启用SMTP调试 $mail->SMTPDebug=2; //请求HTML友好的调试输出 $mail->Debugoutput='html'; //设置邮件服务器的主机名 $mail->Host=“mail.example.com”; //设置SMTP端口号-可能是25、465或587 $mail->Port=25; //是否使用SMTP身份验证 $mail->SMTPAuth=true; //用于SMTP身份验证的用户名 $mail->Username=”contact@example.com"; //用于SMTP身份验证的密码 $mail->Password=“1234”; //设置要从谁发送消息 $mail->setFrom('contact@example.com“,”发件人名称“); //将替代答复设置为地址 $mail->addReplyTo('备选方案_contact@example.com“,“备选联系人姓名”); //设置要向谁发送邮件 $mail->addAddress($email,$name); //设置主题行 $mail->Subject='PHPMailer SMTP测试'; //从外部文件读取HTML消息正文,将引用的图像转换为嵌入的, //将HTML转换为基本的纯文本替换体 //$mail->msgHTML(文件获取内容(dirname(uuu文件)。'/contents.html'); $mail->MsgHTML($body); //将纯文本正文替换为手动创建的正文 $mail->AltBody='这是一个纯文本消息正文'; $success=$mail->send(); //发送消息,检查错误 如果($成功){ 返回数组('mailError'=>false,'message'=>'email sent!'.$mail->ErrorInfo,'body'=>$body); }否则{ 返回数组('mailError'=>true,'message'=>'email error!'.$mail->ErrorInfo,'body'=>$body); } }捕获(phpmailerException$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->errorMessage(),'body'=>$body);//来自PHPMailer的漂亮错误消息 }捕获(例外$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->getMessage(),'body'=>$body);//来自其他任何内容的无聊错误消息! } } 函数my_register_endpoints(){ 登记休息路线( “我的命名空间/v1”, “/abc/”, 排列( '方法'=>'发布', “回调”=>“我的终点”, “args”=>数组( ..... ), 'permission\u callback'=>函数(WP\u REST\u Request$Request){ 如果(!用户是否已登录()){ ..... } 返回true; } ) ); } 添加操作('rest\u api\u init','my\u register\u endpoints'); //端点 函数my_end_point(WP_REST_Request$Request){ 全局$当前用户; $sender=wp_get_current_user(); $shortMsg=$request['shortMessage']; $subject=“一些文本”; $email\u result=my\u send\u email(“收件人姓名”、“收件人电子邮件”、$sender$status、$subject$shortMsg); 如果($email_resalt['mailError']==false){ 处理..$items,$item。。 $message='email sent!'; 返回数组('message'=>$message,'items'=>$items,'item'=>$item); }否则{ 返回新的WP_错误('email Error','email-Error','email-Error'),$request['id']); } }

Angularjs 如何使用PHPmailer从WordPress Rest API端点发送电子邮件 function my_send_email($name、$email、$current_user、$status、$subject、$Message){ //这里的大部分代码都来自https://github.com/PHPMailer/PHPMailer/tree/master/examples 需要_once ABSPATH.WPINC'/class phpmailer.php'; 需要_once ABSPATH.WPINC'/class smtp.php'; //生成消息体`在此处输入代码` $body= 我的电子邮件 '; 试一试{ //创建新的PHPMailer实例 $mail=新的PHPMailer; //告诉PHPMailer使用SMTP $mail->isSMTP(); //启用SMTP调试 $mail->SMTPDebug=2; //请求HTML友好的调试输出 $mail->Debugoutput='html'; //设置邮件服务器的主机名 $mail->Host=“mail.example.com”; //设置SMTP端口号-可能是25、465或587 $mail->Port=25; //是否使用SMTP身份验证 $mail->SMTPAuth=true; //用于SMTP身份验证的用户名 $mail->Username=”contact@example.com"; //用于SMTP身份验证的密码 $mail->Password=“1234”; //设置要从谁发送消息 $mail->setFrom('contact@example.com“,”发件人名称“); //将替代答复设置为地址 $mail->addReplyTo('备选方案_contact@example.com“,“备选联系人姓名”); //设置要向谁发送邮件 $mail->addAddress($email,$name); //设置主题行 $mail->Subject='PHPMailer SMTP测试'; //从外部文件读取HTML消息正文,将引用的图像转换为嵌入的, //将HTML转换为基本的纯文本替换体 //$mail->msgHTML(文件获取内容(dirname(uuu文件)。'/contents.html'); $mail->MsgHTML($body); //将纯文本正文替换为手动创建的正文 $mail->AltBody='这是一个纯文本消息正文'; $success=$mail->send(); //发送消息,检查错误 如果($成功){ 返回数组('mailError'=>false,'message'=>'email sent!'.$mail->ErrorInfo,'body'=>$body); }否则{ 返回数组('mailError'=>true,'message'=>'email error!'.$mail->ErrorInfo,'body'=>$body); } }捕获(phpmailerException$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->errorMessage(),'body'=>$body);//来自PHPMailer的漂亮错误消息 }捕获(例外$e){ 返回数组('mailError'=>false,'message'=>'email error!'.$e->getMessage(),'body'=>$body);//来自其他任何内容的无聊错误消息! } } 函数my_register_endpoints(){ 登记休息路线( “我的命名空间/v1”, “/abc/”, 排列( '方法'=>'发布', “回调”=>“我的终点”, “args”=>数组( ..... ), 'permission\u callback'=>函数(WP\u REST\u Request$Request){ 如果(!用户是否已登录()){ ..... } 返回true; } ) ); } 添加操作('rest\u api\u init','my\u register\u endpoints'); //端点 函数my_end_point(WP_REST_Request$Request){ 全局$当前用户; $sender=wp_get_current_user(); $shortMsg=$request['shortMessage']; $subject=“一些文本”; $email\u result=my\u send\u email(“收件人姓名”、“收件人电子邮件”、$sender$status、$subject$shortMsg); 如果($email_resalt['mailError']==false){ 处理..$items,$item。。 $message='email sent!'; 返回数组('message'=>$message,'items'=>$items,'item'=>$item); }否则{ 返回新的WP_错误('email Error','email-Error','email-Error'),$request['id']); } },angularjs,wordpress,api,rest,phpmailer,Angularjs,Wordpress,Api,Rest,Phpmailer,我使用angularJS$http.post调用API端点,发送电子邮件,我可以在收件箱中看到它们,但出于某种原因,我收到了未定义的响应: Chrome控制台上的相关错误: 1) SyntaxError:JSON中位于位置0的意外标记 2) TypeError:无法读取未定义的属性“message” 我在客户端代码中使用response.message,但如上所述,由于响应是空的,我得到了错误 我的WordPress PHP代码中有其他端点,看起来相同,但不包括使用PHPmailer发送电子邮件

我使用angularJS$http.post调用API端点,发送电子邮件,我可以在收件箱中看到它们,但出于某种原因,我收到了未定义的响应:

Chrome控制台上的相关错误: 1) SyntaxError:JSON中位于位置0的意外标记

2) TypeError:无法读取未定义的属性“message”

我在客户端代码中使用response.message,但如上所述,由于响应是空的,我得到了错误

我的WordPress PHP代码中有其他端点,看起来相同,但不包括使用PHPmailer发送电子邮件,效果很好。即使这个没有发送电子邮件功能的端点也可以正常工作


我试着调试了两天,但仍然不确定代码中的问题在哪里?

我发现了问题所在,在我的发送电子邮件功能中,我有以下几行代码:

//启用SMTP调试

$mail->SMTPDebug=2

$mail->Debugoutput='html'

因为调试是打开的,所以在我注释了这两行之后,我的端点的响应与angular要处理的响应是匹配的,所以angular能够解析它


解决这个问题的提示在下面的帖子中

检查dev tools网络中的实际请求,并查看到底发送了什么。请注意,
return
echo
不同,谢谢,也许我忘了提到,但我调试了客户端和服务器端代码,我使用了chrome developer工具进行客户端调试,在$http回调中得到了response=undefined,我使用sublime和XDebuge调试PHP代码,并达到了我的结束点行:returnnewwp\u Error('email Error','email-Error','email-Error',$request['id')。这似乎返回response.message,但客户端显示未定义。
function my_send_email($name,$email, $current_user, $status, $subject, $Message) {
    //most of the code here is from https://github.com/PHPMailer/PHPMailer/tree/master/examples

    require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
    require_once ABSPATH . WPINC . '/class-smtp.php';

    // build message body`enter code here`
   $body = '
    <html>
        <body>
        <h1>My Email</h1>
        </body>
    </html>
    ';

    try {
        //Create a new PHPMailer instance
        $mail = new PHPMailer;

        //Tell PHPMailer to use SMTP
        $mail->isSMTP();

        //Enable SMTP debugging
        $mail->SMTPDebug = 2;

        //Ask for HTML-friendly debug output
        $mail->Debugoutput = 'html';

        //Set the hostname of the mail server
        $mail->Host = "mail.example.com";

        //Set the SMTP port number - likely to be 25, 465 or 587
        $mail->Port = 25;

        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;

        //Username to use for SMTP authentication
        $mail->Username = "contact@example.com";

        //Password to use for SMTP authentication
        $mail->Password = "1234";

        //Set who the message is to be sent from
        $mail->setFrom('contact@example.com', 'sender_name');

        //Set an alternative reply-to address
        $mail->addReplyTo('alternative_contact@example.com', 'alternative_contact_name');

        //Set who the message is to be sent to
        $mail->addAddress($email, $name);
        //Set the subject line
        $mail->Subject = 'PHPMailer SMTP test';
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        //$mail->msgHTML(file_get_contents(dirname(__FILE__) .'/contents.html'));
        $mail->MsgHTML($body);
        //Replace the plain text body with one created manually
        $mail->AltBody = 'This is a plain-text message body';

        $success=$mail->send();
        //send the message, check for errors
        if ($success) {
            return array('mailError' => false, 'message' => 'email sent! ' . $mail->ErrorInfo , 'body'=>$body);
        } else {
            return array('mailError' => true, 'message' => 'email error! ' . $mail->ErrorInfo , 'body'=>$body);
        }
    } catch (phpmailerException $e) {
        return array('mailError' => false, 'message' => 'email error! ' . $e->errorMessage() , 'body'=>$body);  //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        return array('mailError' => false, 'message' => 'email error! ' . $e->getMessage() , 'body'=>$body); //Boring error messages from anything else!
    }

}

function my_register_endpoints(){
    register_rest_route(

        'my_namespace/v1',
        '/abc/',
        array(
            'methods' => 'POST',
            'callback' => 'my_end_point',
            'args' => array(
                    .....
            ),
            'permission_callback' => function (WP_REST_Request $request) {

                if(!is_user_logged_in()){
                .....
                }
                return true;

            }
        )

    );
}

add_action('rest_api_init','my_register_endpoints'); 

//endpoint
function my_end_point(WP_REST_Request $request){

        global $current_user;
        $sender = wp_get_current_user();
        $shortMsg=$request['shortMessage'];
        $subject="Some text";

        $email_resualt=my_send_email("reciver_name","reciver_email",$sender,$status,$subject,$shortMsg);

        if($email_resualt['mailError']==false){

            process.. $itemes, $item ..    
            $message='email sent!';
            return array('message' => $message,'items' => $items, 'item'=>$item);

        } else {

             return new WP_Error('email error',__('some message','email-error'),$request['id']);

        }   

}