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
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[]
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manish Kumar Narang
Manish is an experienced Backend Developer with several years of industry experience in the IT field. He possesses a wide range of skills, including expertise in Backend languages like Core Java, J2EE, Hibernate, Spring/Spring Boot, and Python. Manish is also proficient in relational databases such as MySQL, PostgreSQL, and Oracle. He has hands-on experience in API implementations, web services development, testing, and deployments. Manish has contributed to various internal and client projects, including PMO, Catalyst, Communication-Scaffold, Oodles-Dashboard, and Devops Support, delivering significant business value. He is known for his innovative mindset and excellent problem-solving abilities. He keeps himself updated with new technologies by reading about them. He is skilled at collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and manage expectations. Manish conducts regular project status meetings, ensuring regular updates to clients and stakeholders regarding project progress, risks, and issues. Additionally, he serves as a mentor and coach to junior developers, offering guidance on project management best practices and fostering their skills development.