public function send()
{
$result = new MailResult();
try {
// Trigger pre send event
$this->getEventManager()->triggerEvent($this->createMailEvent());
// Attach files before sending the email
$this->attachFiles();
// Try to send the message
$this->transport->send($this->message);
// Trigger post send event
$this->getEventManager()->triggerEvent($this->createMailEvent(MailEvent::EVENT_MAIL_POST_SEND, $result));
} catch (\Exception $e) {
$result = $this->createMailResultFromException($e);
// Trigger send error event
$this->getEventManager()->triggerEvent($this->createMailEvent(MailEvent::EVENT_MAIL_SEND_ERROR, $result));
// If the exception produced is not a Zend\Mail exception, rethrow it as a MailException
if (!$e instanceof ZendMailException) {
throw new MailException('An non Zend\\Mail exception occurred', $e->getCode(), $e);
}
}
return $result;
}