So i'm making a contact page. Of course it will have a form, which needs to be sent. I made the class to send email:
<?php
namespace Site\Mail;
use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail;
class SendEMail {
public function SendEMail($to, $from, $subject, $body) {
$message = new Message();
$message->addTo($to)
->addFrom($from)
->setSubject($subject)
->setBody($body);
$transport = new Sendmail();
try {
$transport->send($message);
return true;
} catch (\Exception $e) {
return false;
}
}
}
?>
In the controller i have
public function emailAction () {
$request = $this->getRequest();
$vm = new ViewModel();
$vm->setTerminal(true);
if ($request->isPost()) {
$to = "[email protected]";
$from = $request->getPost("email");
$subject = "Messaggio inviato da ".$request->getPost("nome")." (tel. ".$request->getPost("phone").")";
$body = $request->getPost("messaggio");
$mail = new SendEMail($to, $from, $subject, $body);
if (!$mail) {
$vm->setVariables(array(
"result" => "no"
));
return $vm;
} else {
$vm->setVariables(array(
"result" => "yes"
));
return $vm;
}
}
$vm->setVariables(array(
"result" => "This script needs POSTDATA."
));
return $vm;
}
I disabled the layout to allow an Ajax/Jquery script to load it, and it works like a charm (i'll show you it although i don't think it's causing my issue)
$(document).on('submit', 'form', function() {
$.ajax({
url:$("form").attr("action"),
method:"POST",
beforeSend: function() {
$("form").css("opacity", 0.5);
},
complete: function() {
$("form").css("opacity", 1);
},
success: function(responseText) {
$("body").append("<div id='popupmessage'>"+responseText+"</div>");
},
error: function(errorThrown) {
$("body").append("<div id='popupmessage'>"+errorThrown+"</div>");
}
}).always(function() {
$("#popupmessage").delay(1000).fadeOut(300, function () {
if($(this).length>0) {
$(this).remove();
}
});
});
return false;
});
The problem is that my SendEMail()
function looks like always return true, even if i put wrong settings in sendmail.ini (and i'm using sendmail).
What's my mistake? I appreciate any help. Thank you.
While I was implementing my email method I was never able to get the exception with:
For me it was not so important to get it though. But at some point I noticed that I was getting an exception when the domain did not exist or it was wrong so I narrowed down the exception to.
So if this is not what you want I would assume that making this change would provide you more than the runtimeException or you can keep it the way it is assuming this return you the result you want
I hope this helps. Good luck.
Update:
I noticed I am using SMTP Transport (more details) So that is why I am using the protocol Exception. If you check inside the Zend Library you will see another folder Zend\Mail\Exception. So try that inside the try catch e.g.
If this does not work try to narrow down the exception: Inside the folder you will see 6 exception classes test each one inside the try catch (\Zend\Mail\Exception\AnExceptionClass)
I hope this helps.