Coder Social home page Coder Social logo

Dynamic parameters. about grails-mail HOT 2 OPEN

kuez avatar kuez commented on July 30, 2024
Dynamic parameters.

from grails-mail.

Comments (2)

helgew avatar helgew commented on July 30, 2024

You beat me to it! 👍
I posted in the slack #plugin channel yesterday with a similar business requirement to see if there had been any discussions or efforts on implementing this. It is mentioned as a TODO in the documentation.

I would be motivated to take this on but think it would be best if the authors pitch in with their thoughts on how this should be implemented.

Currently, the mail sender is a bean created in the plugin class and injected in the MailMessageBuilderFactory, which in turn creates a builder for every message sent. Options for implementing a more flexible configuration of dynamically configured servers could include

  • adding the ability to configure multiple senders together with a name-based specification of which sender to use in the sendMail closure.
  • adding a getter to the service that returns a clone of the current sender, which could be reconfigured and provided as an attribute to the sendMail closure
  • adding a configuration map as an attribute to the sendMail that then gets merged with the default configuration

Thoughts? Other options/preferences?

from grails-mail.

helgew avatar helgew commented on July 30, 2024

In the absence of any feedback so far, I have been able to implement a workaround by replacing the mailMessageBuilderFactory bean with the following:

import grails.config.Config
import grails.core.GrailsApplication
import grails.plugins.mail.MailMessageBuilder
import org.springframework.mail.MailSender
import org.springframework.mail.javamail.JavaMailSenderImpl

import javax.mail.Session
import java.security.Security

class MailMessageBuilderFactory extends grails.plugins.mail.MailMessageBuilderFactory {

    GrailsApplication grailsApplication

    @Override
    MailMessageBuilder createBuilder(Config config) {
        MailSender mailSender = this.mailSender
        if (config.props) {
            mailSender = getMailSender(config)
        }
        new MailMessageBuilder(mailSender, config, mailMessageContentRenderer)
    }

    MailSender getMailSender(config) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl()
        if (config.host) {
            mailSender.host = config.host
        } else if (!config.jndiName) {
            def envHost = System.getenv()['SMTP_HOST']
            if (envHost) {
                mailSender.host = envHost
            } else {
                mailSender.host = "localhost"
            }
        }

        if (config.encoding) {
            mailSender.defaultEncoding = config.encoding
        } else if (!config.jndiName) {
            mailSender.defaultEncoding = "utf-8"
        }

        if (config.port) {
            mailSender.port = config.port
        }

        if (config.username != null) {
            mailSender.username = config.username
        }

        if (config.password != null) {
            mailSender.password = config.password
        }

        if (config.protocol != null) {
            mailSender.protocol = config.protocol
        }

        if (config.props instanceof Config) {
            Session session = Session.getInstance(config.props.toProperties())
            session.setDebug(config.debug ?: false)
            mailSender.session = session
        }

        mailSender
    }
}

Basically, the above relies on the config object to contain a key props when the mailService.sendMail(config) { ... } method is used. Seems a bit hackish, but it works.

That was the easy part... figuring out how to use XOAUTH2 with a service account user for Google Apps, not so much :)

from grails-mail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.