如何从WordPress Rest API端点使用PHPmailer发送电子邮件

卡姆兰
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 = "[email protected]";

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

        //Set who the message is to be sent from
        $mail->setFrom('[email protected]', 'sender_name');

        //Set an alternative reply-to address
        $mail->addReplyTo('[email protected]', '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']);

        }   

}

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

Chrome控制台上的相关错误:1)SyntaxError:JSON位置0上的意外令牌S

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

我在客户端代码中使用response.message,但是如上所述,但是由于响应为空,因此出现错误。

我的WordPress PHP代码中还有其他端点,这些端点看起来相同,但不包括使用PHPmailer发送电子邮件,并且工作正常。甚至没有发送电子邮件功能的端点也可以正常工作。

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

卡姆兰

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

//启用SMTP调试

$ mail-> SMTPDebug = 2;

$ mail-> Debugoutput ='html';

似乎是因为在我注释了这两行之后,由于调试是从我的端点打开的,所以才匹配角度以进行处理,因此角度的响应结构良好,可以解析它。

解决该问题的提示是在以下帖子语法错误中:Object.parse,PHP到AngularJS处的意外数字

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot发送电子邮件API REST

如何使用Java发送电子邮件?

如何通过Gmail API发送电子邮件?走

如何使用JavaScript使用Mandrill发送电子邮件?

通过Google http rest api发送电子邮件

如何使用Gmail API快速发送电子邮件

如何使用MailKit发送电子邮件?

如何使用PowerShell发送电子邮件

如何使用的PHPMailer发送电子邮件的x个?

Keycloak-使用Keycloak的Admin REST API发送电子邮件时,如何选择电子邮件验证模板?

如何使用django“ Rest Framwork”发送电子邮件?

提交表单后如何发送电子邮件[Django-rest-framework]

如何使用Python使用Gmail发送电子邮件

如何使用cURL在php中使用Google gmail API发送电子邮件?

如何使用Gmail REST API发送电子邮件

我如何使用angular 5和asp.net core从angular向端点发送电子邮件

如何使send_mail函数发送密件抄送电子邮件-Django REST API

如何使用activemq发送电子邮件

使用Django Rest Framework和Angular从API端点发送电子邮件

如何使用GPG公钥发送电子邮件?

如何在Google Cloud端点中使用sendgrid发送电子邮件?

如何从Wordpress页面发送电子邮件附件?

如何使用PhpMailer和WAMP发送电子邮件

使用JIRA REST Client发送电子邮件

如何使用 eiffel 内核库发送电子邮件

如何从wordpress php发送电子邮件?

如何在 Django rest 框架中加快发送电子邮件的速度?

如何使用 JakartaMail 从共享电子邮件(或委托电子邮件)发送电子邮件

到现在 Google 停止了不太安全的应用程序,如何使用 PHPMailer 从 GMail 帐户发送电子邮件?