1
votes

Is there a way to hook into the sendMail method of the grails Mail plugin and modify the body text ? I need to insert something like a signature into every email sent out using the plugin.

I figure I can add an include gsp into every email template but ... ugh :) Is there another (lazier) way to do that?

Thanks

2

2 Answers

1
votes

Why do you need a hook? Why don't you create your own textParsing scheme and allow it to add a signature to your mail text and you could have as many instances of such parsing as you want. It has nothing to do with mailing.

Basic String manipulation and code reusability techniques should do the needful.

Edit: You though answered yourself i.e. by using gsp templates or any template api like thymleaf or sitemesh.

Another More basic technique could be to parse some text say you want to add a signature and username.

example

Hello #username#

mail_content

#signature#

Now, here you may use String.replaceAll and replace with required values.

But for sure java mail api by default doesn't provide such hooks.

If you are familiar with spring and AOP then Final solution could be: to use Spring Aspect Oriented Programming for intercepting any method and perform some tasks before/after or around it. I believe spring aspects should definitely help you out.

I have never tried aspects with grails though. This should help you

1
votes

You can append the signature to the email body, if you encapsulate a method in a service for sending mail.

public send(String emailTo, String emailSubject, String emailBody, String emailFrom, String customSignature = "") {

String signature = customSignature?: "<p>Default signature</p>"

        try {
            mailService.sendMail {
                from emailFrom 
                to emailTo
                subject emailSubject
                html (emailBody + signature)
            }
        } catch (Exception e) {
            log.error('There was an error sending the email')
            log.error e.getMessage()
        }
    }