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.