tags:

views:

54

answers:

1

I've looked through the code and documentation for the Grails Mail plugin (version 0.9) and it doesn't have the support I'm looking for. You can only set a single body and then provide a mime attachment that points to a static file. I need to actual pass a model into a GSP and have it render both the HTML and plain text versions and then have those both available in the message. This will allow non-HTML-based e-mail clients to display the text/plain part and clients that support HTML to display the text/html part.

Has anybody done this with Grails? Is there an easy way to do it, or do I have to modify the mail plugin or just go to the Java Mail library directly?

A: 

We use multipart email with the standard email plugin. The following code snippet is located in a service class, that's why we're using 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')
        }

The crucial thing is that 'multipart true' appears at the beginning of the closure. If you add

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

to the closure above, I assume you'll get a text and html email with an attachment.

Stefan