title: how to return false when mail transport failed in zend framework 2?
body: <p>I am trying to return <code>fail</code> message with json if the mail sending is failed, here is my code:</p>
  
  <pre><code>$transport = new SmtpTransport();
              $options   = new SmtpOptions(array(
                      'name'              =&gt; 'localhost',
                      'host'              =&gt; 'localhost',
                      'connection_class'  =&gt; 'login',
                      'connection_config' =&gt; array(
                              'username' =&gt; 'cscsacsac',
                              'password' =&gt; 'csdca',
                              //'ssl'=&gt;'ssl',
                              //'port' =&gt; '465',
                      ),
              ));
  
              $transport-&gt;setOptions($options);
              $sent = true;
              try {
  
                  $transport-&gt;send($mail);
              }
              catch (\Zend\Mail\Transport\Exception\DomainException $e) {
                  $sent = false;
              }
  
              if($sent){
  
                  echo "success";
  
                  exit;
              }
  
              elseif(!$sent){
  
                  echo "fail";
  
                  exit;
              }
  </code></pre>
  
  <p>and here is the ajax:</p>
  
  <pre><code>$.ajax({
            type: "POST",
            url: '&lt;?php echo MAIN_URL?&gt;cases',
            data: { name: name, email: email, mobile: mobile, address: address, compliant: compliant },
            success:function(data){
  
              if(data =='success'){
  
                  $('.casesMailResponse').css({'color':'#00917D'});
                  $('.casesMailResponse').html('Your case has been sent successfully.');
              }
  
              else if(data == 'fail'){
  
                  $('.casesMailResponse').css({'color':'#F6565B'});
                  $('.casesMailResponse').html('Some errors occurred, please try again.');
              }
            },
          });
  </code></pre>
  
  <p>but in case of failure I got Internal server error due to <code>Incorrect authentication data</code>, so the failure message is not displayed</p>