Send an image in an email with Grails

后端 未结 3 1929
攒了一身酷
攒了一身酷 2020-12-30 16:28

I am using the Grails mail plugin to send emails and I want to send images in the body of the email, not as attachment. What I want is for images to be shown in the email bo

相关标签:
3条回答
  • 2020-12-30 17:06

    In general, most "newsletter" type emails are just HTML emails and don't in-line the images within the body of the emails. In fact, many email servers will outright reject emails that are too big, and putting the image data inside the body of the email can easily run this risk.

    Instead, the way to do with this is to just have HTML emails with the images you wish to include as normal HTML image links.

    Please refer to the Mail plugin documentation for more info on how to send HTML emails.

    0 讨论(0)
  • 2020-12-30 17:12

    Sorry I don't have enough Stackoverflow points to leave comment on others' answers.

    In @aiolos 's answer, it contains:

    inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')
    

    This specifies 'image/jpg' as the output, but the file is in png format. Is this intentional?

    Wikipedia provides a list of mime types: http://en.wikipedia.org/wiki/Internet_media_type#Type_image

    I tried 'image/png' and it worked, so I'm not sure if setting it to 'image/jpg' makes any difference.

    0 讨论(0)
  • 2020-12-30 17:19

    You have to do three things

    1. Declare mail as multipart
    2. Attach your image as inline image
    3. Reference you inline image from your mail template

    According to this mailing list thread it should work like this:

    sendMail{
        multipart true
        to "[hidden email]"
        subject "Subject goes here"
        html  g.render( template: '/emails/mailTemplate')
        inline 'springsourceInlineImage', 'image/jpg', new File('./web-app/images/springsource.png')
    }
    

    In your template you could refer the image like this

    <img src="cid:springsourceInlineImage" />
    

    Hope that helps.

    0 讨论(0)
提交回复
热议问题