附件 +电子邮件 + HTML +游戏框架

发布于 2024-11-05 19:40:24 字数 402 浏览 2 评论 0原文

我在这个项目中使用 play 框架,并且我正在尝试发送一封附有徽标的电子邮件,但我想将此徽标显示为我的 HTML 代码的一部分!

我的邮件程序: EmailAttachment 附件 = new EmailAttachment(); Attachment.setDescription(“标志”); Attachment.setName("logoMail.jpg"); Attachment.setPath(Play.getFile("/public/images/email/logoMail.jpg").getPath());

添加附件(附件);

我的 HTML


电子邮件已发送,我的徽标附在其中,但图像从未在我的 DIV 上显示为背景。

我做错了什么?

非常感谢!

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.jpg").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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℡寂寞咖啡 2024-11-12 19:40:24

这取决于您用来阅读测试电子邮件的电子邮件客户端。他们中的大多数都会忽略或删除background-image css 属性。

看一下以下内容:

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

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

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/

殊姿 2024-11-12 19:40:24

我一直在研究使用 MVC 模板将图像嵌入到电子邮件中,但我认为目前还不支持它。

据我所知,为了使用嵌入图像,图像附件需要有一个 Content-ID 标头。使用 addAttachment 附加图像会生成不带此标头的附件。

底层电子邮件框架 apache commons email 允许您使用 HtmlEmail.embed 方法嵌入图像,Play 文档中有一个示例,但仅限于直接使用 Commons Email 时。 addAttachment() 将添加普通附件,而不是嵌入附件。

问题是 HtmlEmail.embed 返回嵌入图像的内容 ID。第一个问题是需要一种机制将该内容 ID 转发到模板中,以便您可以在相关链接中引用它。

第二个问题是 Mailer.send() 方法的编码方式,直到呈现模板之后才会创建电子邮件本身,并且尝试呈现 html 正文的结果用于决定是否创建 HtmlEmail 或一个简单的电子邮件。需要重写此方法,以便在渲染模板之前确定电子邮件的类型,如果是 html,则在渲染模板之前创建 HtmlEmail 并附加嵌入的图像,以便它可以传递内容 id到渲染器。

做出这样的改变当然不是不可能的,如果我能在当前的项目上找到时间,我可能会尝试一下。

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.

梨涡 2024-11-12 19:40:24

解决方案可能是手动呈现 HTML 内容,然后将其放入电子邮件中。这段代码对我有用:

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("[email protected]");
  email.setFrom("[email protected]");

  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();
}   

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("[email protected]");
  email.setFrom("[email protected]");

  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();
}   
此刻的回忆 2024-11-12 19:40:24

我的回答有点晚了,但这是可能的,并且与 MVC-电子邮件教程很好地集成。假设您的邮件程序类也是 notifiers.Mails,请使用它作为 html 模板:

%{
    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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文