无法在Wordpress Rest API中调用函数

时间:2018-10-22 12:04:52

标签: wordpress rest wordpress-rest-api

我的自定义Wordpress Rest API中包含以下代码。每当启用对send_mail2函数的调用时,我都会收到500个内部服务器错误。 评论说,Web服务可以正常工作。 如果将该功能与调用分开放在.php文件中并从浏览器打开,则该功能也可以很好地工作。

add_action( 'rest_api_init', function () {
register_rest_route( 'registrations/v1', '/add', array(
    'methods' => 'POST',
    'callback' => 'handle_reg_create'
) );
} );

function handle_reg_create( $request ) {

    $parameters = $request->get_json_params();
    $url = "myurl";
    $mail_object->to_email = $parameters['email_id'];
    $mail_object->subject = "Camp Registration Successful for ".$parameters['name']."Camp Id".$parameters['camp_id'];
    $mail_object->from = "sample@mailxyz.com";
    $mail_object->message = '<p>Thanks for registering .</p>
        <p>Following are the details you have entered&nbsp; :</p>
        <p>Name : '.$parameters['name'].'<br/>Age&nbsp; &nbsp; :
        '.$parameters['age'].'<br />Phone Number (Call):
        '.$parameters['phone_number_2'].'<br/>Phone Number (Whatsapp) : 
        '.$parameters['phone_number_2'].'</p>
        <p>You may edit your details by <a href="'.$url.'" target="_blank"rel="noopener">clicking here</a>.</p>
        <p>&nbsp;</p>
        <p>Thanking you,<br />Admin</p>';
    send_mail2($mail_object);

    //return $list;
}

function send_mail2( $parameters ) {
    echo "Started";
    var_dump($parameters);
    $mail = new PHPMailer;
    echo "Enable SMTP debugging. ";
    $mail->SMTPDebug = 3;
    echo "Set PHPMailer to use SMTP.";
    $mail->isSMTP();
    echo "Set SMTP host name"     ;
    $mail->Host = "hostname";
    echo "Set this to true if SMTP host requires authentication to send email";
    $mail->SMTPAuth = true;
    echo "Provide username and password     ";
    $mail->Username = "noreply@sample.com";
    $mail->Password = "samplepassword";
    $mail->Port = 587;
    $mail->From = "noreply@sample.com";
    $mail->FromName = "Sample";

    echo "To Email :".$parameters->to_email;
    echo "Subject :".$parameters->subject;
    $mail->addAddress($parameters->to_email, "Recepient Name");

    $mail->isHTML(true);

    $mail->Subject = $parameters->subject;
    $mail->Body = $parameters->message;
    $mail->AltBody = "HTML Email - No Support";
    echo "Attempt Send ";
    $mail->send();

}

0 个答案:

没有答案