Comments (2)
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.
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)
- JDK 10 IllegalArgumentException due to ordering of ThreadPoolExecutor setting
- Grails 3.3.4 Boot Issue HOT 1
- Documetation Issues with Multipart HOT 1
- 'to' value limit? send multiple emails HOT 1
- Grails 4 M1, OpenJDK 11.0.2 throws exception with basic usage HOT 9
- Support for External Credentials Provider
- Intellij IDEA: "default" is a Groovy 3.0 keyword HOT 2
- org.grails.plugins:mail:3.0.0 failed when adding grails { pathingJar = true } HOT 6
- Update Documentation
- Grails 4.0.3: <g:applyLayout>: Unable to resolve view HOT 1
- How to send a conference invitation email?
- GrailsMailException: message has no content, use text(), html() or body() methods to set content HOT 1
- Authentication support via OAuth2 for Gmail/Microsoft mail in addition to basic authentication HOT 2
- links to user guide are broken
- Support for smtp connection pool
- Did i miss something ? HOT 2
- Support for Grails 5 and 6 HOT 1
- Plugin site gets 404 HOT 1
- Repository should be detached from gpc/grails-mail
- From Address Not Correct HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grails-mail.