How can I send a multi-part email with text / text / text parts with Grails?

I looked at the code and documentation for the Grails Mail plugin (version 0.9) and I don't have the support I'm looking for. You can install only one body, and then provide a mime attachment pointing to a static file. I need to pass the model to GSP so that it displays both HTML and text versions, and then they are available in the message. This will allow non-HTML email clients to display the text / regular part and clients that support HTML to display the text / html part.

Has anyone done this with Grail? Is there an easy way to do this, or do I need to change the mail plugin or just go directly to the Java Mail library?

+5
source share
3 answers

Starting with version 1.0, the mail plugin natively supports multi-page alternative content, as described at http://jira.grails.org/browse/GPMAIL-37

mailService.sendMail {
    multipart true
    to <recipient>
    subject <subject string>
    text 'my plain text'
    html '<html><body>my html text</body></html>'
}
+5
source

We use multi-page email with a standard email module. The following code snippet is in the service class, so we use the standard groovy templating instead of the gsp engine:

        Template template = groovyPagesTemplateEngine.createTemplate(<templatename>)
        Writable emailBody = template.make(<data model as map>)
        StringWriter bodyWriter = new StringWriter()
        emailBody.writeTo(bodyWriter)

        String xml = <some xml>  

        mailService.sendMail {
            multipart true
            to <recipient>
            subject <subject string>
            body bodyWriter
            attachBytes "filename.xml", "text/xml", xml.getBytes('UTF-8')
        }

Most importantly, "multipart true" appears at the beginning of the close. If you add

html '<b>Hello</b> World'

to closing above, I assume that you will receive the text and html email address with the attachment.

0
source

This seems to be potential content for version 1.0 of the Mail plugin, see this and this question. Looking at the patch of the first problem, I think that the html message and text multipart can simply be created as follows:

mailService.sendMail {
  multipart true
  to <recipient>
  subject <subject>
  dualBody(template:<template>, model:<model>)
}

It would be great! I don't know when / when it will be released.

0
source

All Articles