douye2111 2017-03-29 14:37
浏览 177

PHP类调用函数有一个返回值

Try to explain my problem, I hope someone give me an advice 'cause I'm stuck on that for a while.

I've 2 functions in my simple class. This class send an email. First function is called send_mail(). Meanwhile I'm using phpmailer class.

public function send_mail() {
    $mail = new \PHPMailer(true);
    $mail->Body = $this->template();
    $mail->isHTML(true);
    if(!$mail->send())
        {
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
        }

        return true;
}

I'm passing other extra configs. That's my code. Problem is here $mail->Body = $this->template();. Because template() function has a return.

When I make a request this class via ajax, It's not return what I want. It's return template of mail. How do I break template function return.

Here is template function content:

private function template() {
return '

        <table width="600" border="0" cellspacing="5" cellpadding="5">
          <tr>
            <td>Name</td>
            <td>:</td>
            <td>'.$this->name.'</td>
          </tr>
          <tr>
            <td width="100">Surname</td>
            <td width="10">:</td>
            <td>'.$this->surname.'</td>
          </tr>
        </table>';
}

This is my route for this action:

$app->post('/send_ticket', function () use ($app, $simpleEmail) {

    $post = $app->request()->post();
    $simpleEmail->create($post,"my@email.com");
    $simpleEmail->send_mail();

});

My ajax call:

$.ajax({
     method: "post",
     url: url,
     data: $(".form_demo").serialize(),
     success: function (response) {

           console.log(response);

           if (response === true) {
                alert("Thank you !");
           } else  {
                alert("Oopss error. Try again !");
           }
     },
     error: function(errorCode) {
           console.log(errorCode);
     }
});

I hope explain.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥15 MCNP里如何定义多个源?
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器