Enonic version: 6.11.0
OS: OSX
I want to send an email when a form is submitted with Ajax. If anything goes wrong with sending the email then I want to log the form responses so they’re not lost. I therefore put the mail sending code in a try-catch, but the catch never happens.
I don’t have a local SMTP server configured so I expected the mailLib to throw an error. I do see an error in the log, but it is not caught by the “catch” and so the log.warning doesn’t happen. Is this how it’s supposed to work?
var emailSent = false;
try {
emailSent = mailLib.send({
from: config.contactFormFromEmail,
to: config.contactFormToEmail,
subject: "New contact",
body: message
});
if (!emailSent) {
log.info("If you're reading this then the email did not send due to some kind of error and I would expect the catch block to run but it doesn't.");
}
} catch (e) {
log.warning('Contact form email did not send. \n' + message);
}
Notice I added the if(!emailSent) so now I can still get the form responses in the log when there is a problem with sending the email. So this is not high priority by any means, just not how I expected it to work.