Multiple email addresses as override addresses in Grails

Posted By : Manish Kumar Narang | 30-Sep-2017

In one of my previous blogs, I talked about the mailing functionality in Grails framework. One may read it here   . In that blog i also mentioned how the parameters namely grails.mail.overrideAddress, grails.mail.default.from, grails.mail.default.to can only take one email address. But there may be scenarios where we would want multiple addresses to be contained in these parameters. Take, for example, if the project is tested by multiple qualitative analysts then we should be able to have multiple addresses to override the email addresses in the testing environment. Another scenario which we encountered was when we want to test email functionality for multiple addresses. Since the email addresses are overridden by only one email address, the functionality seems to work even if there-there are multiple email addresses in to, from, cc and bcc. But that may not be so.

So, now let me discuss how we can add multiple addresses in the above-mentioned parameters. For that, we may need to look into the inner workings of the mail plugin and specifically at the message builder factory. So, let's look at the MailMessageBuilder.groovy file. There are two functions which are of interest to us. 

 

The first method MailMessaageBuilder makes copies of the parameters defined in the config.groovy file. Here the local copies of overrideAddress, defaultFrom and default are of String type. The variables are then used for overriding the email addresses through the second method. So, if we modify the second method according to our requirements we are done.

 MailMessageBuilder(MailSender mailSender, ConfigObject config, MailMessageContentRenderer mailMessageContentRenderer = null) 
    {
        this.mailSender = mailSender
        this.mailMessageContentRenderer = mailMessageContentRenderer

        this.overrideAddress = config.overrideAddress ?: null
        this.defaultFrom = overrideAddress ?: (config.default.from ?: null)
        this.defaultTo = overrideAddress ?: (config.default.to ?: null)
    }
        
         protected String[] toDestinationAddresses(addresses) {
        if (overrideAddress) {
            addresses = addresses.collect { overrideAddress }
        }
        addresses.collect { it?.toString() } as String[]
    }
        

 

Since the to destination addresses method takes the email addresses as the argument and returns the array of overridden email addresses, if we change the return array, our job is done. Suppose I wanted to add email1 and email2 as overridden email addresses, I did it as follows -

 
     protected String[] toDestinationAddresses(addresses) {
       def overriddenAddresses = {email1, email2}
       def tempAddresses = addresses
        addresses = []
        if (overrideAddress) {
            overriddenAddresses.each{String overriddenAddress -> 
            tempAddresses = tempAddresses.collect { overriddenAddress }
            addresses.addAll(tempAddresses)
            }
        }
        addresses.collect { it?.toString() } as String[]
    }
        

 

About Author

Author Image
Manish Kumar Narang

Manish Narang has skills in software development. He is enthusiastic about web applications and loves to work in a team.

Request for Proposal

Name is required

Comment is required

Sending message..