0

im using grails 2.4.5 and thinking of a way to set a backup email if ever the main email failed to send.

im looking for a way to determine if the email was sent successfully but in doesnt include in the documentation https://grails.org/plugin/mail?skipRedirect=true

i tried using try catch but i doesnt work(especially in the build version).

here is my service function

class EmailService {

    def grailsApplication
    def mailService

    def sendMail(Map dataMap){

        Boolean sent = true
        // Store the default mail settings in variables
        def defaultFrom = grailsApplication.config.grails.mail.default.from
        String defaultHost = grailsApplication.config.grails.mail.host
        def defaultPort = grailsApplication.config.grails.mail.port
        String defaultUsername = grailsApplication.config.grails.mail.username
        String defaultPassword = grailsApplication.config.grails.mail.password
        def defaultProps = grailsApplication.config.grails.mail.props

        if(!dataMap?.model) dataMap.model = [:]
        if(dataMap?.to?.getClass() == String) dataMap.to = [dataMap?.to]

        try {
            mailService.sendMail {
                to(dataMap?.to?.toArray())
                subject(dataMap?.subject)
                body(view: dataMap?.view, model: dataMap?.model)
            }
        }
        catch (Exception e) { // Change the properties here; send the email
            def backUpMail = grailsApplication.config.grails.backUpMail

            grailsApplication.config.grails.mail.default.from = backUpMail.default.from
            grailsApplication.config.grails.mail.host = backUpMail.host
            grailsApplication.config.grails.mail.port = backUpMail.port
            grailsApplication.config.grails.mail.username = backUpMail.username
            grailsApplication.config.grails.mail.password = backUpMail.password
            grailsApplication.config.grails.mail.props = backUpMail.props

            try {
                mailService.sendMail {
                    to(dataMap?.to?.toArray())
                    subject(dataMap?.subject)
                    body(view: dataMap?.view, model: dataMap?.model)
                }
            } catch (Exception err) {
                sent = false
            }

        }
        // Set the original settings back
        finally {
            grailsApplication.config.grails.mail.host = defaultHost
            grailsApplication.config.grails.mail.port = defaultPort
            grailsApplication.config.grails.mail.default.from = defaultFrom
            grailsApplication.config.grails.mail.username = defaultUsername
            grailsApplication.config.grails.mail.password = defaultPassword
            grailsApplication.config.grails.mail.props = defaultProps
        }

        return sent
    }

}

i was able to catch the exception when I disabled my internet connection but it doesnt work on other errors like authentication error, etc.

ang another problem im experiencing is when the connection was cut for the first email(in the build version). it stops on DEBUG SMTP: trying to connect to host... its not going to enter the catch block because no error occured

mendz
  • 463
  • 2
  • 5
  • 16
  • grails mail plugin from what I recall defaults you to 1 config, take a look at the queuemail plugin - might be of use – V H Mar 13 '18 at 17:48
  • Maybe not of much help, but I have been using nothing but exception handling for several years without any (at least discovered) problems. – wwwclaes Mar 13 '18 at 18:34
  • What version of the mail plugin are you using? Starting with v1.0.5 you can pass a config to `sendMail()` like `sendMail(grailsApplication.config.grails.backupMail, { to( ... ) })`. – doelleri Mar 14 '18 at 06:10
  • wwwclaes i tried doing that. but its still connecting on my main mail properties. i tried to println the config parameter in sendMail(def config, Closure callable) the config was passed successfully but its not working. im using mail-1.0.7 – mendz Mar 15 '18 at 08:33
  • @wwwclaes the try catch is wrapped around the smtp protocol so if it connects on to smp then depending on smtp rules the timeouts apply. So therefore the rules around try catch are based on something remote outside of your application. Anyhow on the face of it that plugin may appear to be of much use. The technique concept however is something worth considering, The grails smpt service is passed into ThreadExecutor where then the defined timeouts for the executor applies. The plugin offers multiple smtp config so it can also support dual smtp accounts. – V H Mar 16 '18 at 09:24

0 Answers0