开发者

Attachment + Email + HTML + Play Framework

I'm using play framework in this project and I'm trying to send an E-mail with a Logo attached but I want to show this logo as part of my HTML code!

My Mailer: EmailAttachment attachment = new EmailAttachment(); attachment.setDescription("Logo"); attachment.setName("logoMail.jpg"); attachment.setPath(Play.getFile("/public/images/email/logoMail.j开发者_如何学Pythonpg").getPath());

addAttachment(attachment);

My HTML


The e-mail is sent, my Logo is attached there, but the image is never showed as a background on my DIV.

What am I doing wrong?

Thank you very much!


It depends on the e-mail client you are using to read your test e-mail. Most of them ignore or remove the background-image css property.

Take a look at the following:

http://www.email-standards.org/

http://www.campaignmonitor.com/design-guidelines/


I've been looking into embedding images into emails using MVC templates, and I think at the moment it's not supported.

As far as I can see, in order to use embedded images, the image attachment needs to have a Content-ID header on it. Attaching the image using addAttachment generates an attachment without this header.

The underlying email framework, apache commons email, allows you to embed images using the HtmlEmail.embed method, and there is an example of this in the Play documentation, but only when using Commons Email directly. addAttachment() will add an ordinary attachment, not an embedded one.

The problem is that HtmlEmail.embed returns the content id for the embedded image. The first problem is that there would need to be a mechanism for passing that content id forward into the template, so that you could reference it in the relevant link.

The second problem is that the way the Mailer.send() method is coded, the email itself is not created until after the template is rendered, and the result of attempting to render an html body is used to decide whether to create an HtmlEmail or a SimpleEmail. This method would need to be re-written to decide the type of email before rendering the template, and, if it was html, to create the HtmlEmail and attach the embedded images prior to rendering the template, so that it could pass the content ids to the renderer.

It certainly isn't impossible to make this change, and I might attempt it if I can find the time on my current project.


The solution could be to render HTML content manually and then put it into email. This code worked for me:

public static String test() throws EmailException, MalformedURLException {
  HtmlEmail email = new HtmlEmail();
  email.setHostName("smtp.server.com");
  email.setAuthentication("username", "pwd");

  email.setSubject("subject");
  email.addTo("to@example.com");
  email.setFrom("from@example.com");

  URL url = new URL("https://example.com/image.png");
  String cid = email.embed(url, "IMG1");

  Template templateHtml = TemplateLoader.load("/Mails/test.html");
  final Map<String, Object> templateHtmlBinding = new HashMap<String, Object>();
  templateHtmlBinding.put("cid", cid);
  String body = templateHtml.render(templateHtmlBinding);
  email.setHtmlMsg(body);

  return email.send();
}   


I'm a bit late with my answer, but it is possible and integrates nicely with the MVC-Email tutorial. Assuming your mailer class is also notifiers.Mails, use this as a html template:

%{
    String logoSrc = notifiers.Mails.getEmbedddedSrc("public/images/logo.png", "cool logo");
}%
<html>
    <head>
    </head>
    <body>
        Look at this cool image! <br>
        <img src="${logoSrc}">
        <br>
        Amazing, no? 
    </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜