使用Scala和Play WS将内嵌图像添加到Mailgun电子邮件中

时间:2015-01-29 21:40:40

标签: scala playframework playframework-2.3 mailgun

我可以成功地向Mailgun发出POST请求并按预期接收电子邮件。我正在尝试将图像内嵌到电子邮件中,但无法确定如何操作。

查看https://documentation.mailgun.com/user_manual.html#sending-via-api并选择Java,我可以看到给出的示例构建了FileDataBodyPart "inline"File引用和MediaType。看一下curl示例,这似乎是不必要的,因为它只引用了一个文件。

以下是我发送电子邮件的方法:

  def send(message:EmailMessage) = {
    val postMessage = Map("from" -> Seq(message.from), "to" -> Seq(message.to), "subject" -> Seq(message.subject), "text" -> Seq(message.text), "html" -> Seq(message.html.toString()))
    val logo = FileBody(Play.getExistingFile("/public/images/logo.png").get)
    WS.url(apiUrl).withAuth("api", myKey, WSAuthScheme.BASIC).withBody(logo).post(postMessage)
  }

message.html.toString如下所示:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body style="background-color:#9B59B6; padding:10px">
    <img src="cid:logo.png">
    <h1 style="color:#FFF">Activate!</h1>
</body>
</html>

发送电子邮件时发现logo.png文件,电子邮件很好,但没有图像。这是电子邮件源到达gmail后的样子:

Mime-Version: 1.0
Content-Type: text/html; charset="ascii"
Content-Transfer-Encoding: 7bit

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body style="background-color:#9B59B6; padding:10px">
        <img src="cid:logo.png">
        <h1 style="color:#FFF">Activate!</h1>
    </body>
</html>

我在电子邮件中看不到图像的任何base64编码。由于curl示例似乎只是作为POST的一部分传递文件,我虽然我尝试了。这是我做的:

  def send(message:EmailMessage) = {

    val logoFile = Play.getExistingFile("/public/images/logo.png").get
    val source = Files.readAllBytes(Paths.get(logoFile.getAbsolutePath))
    val logoBase64 = Base64.encodeBase64String(source)

    val postMessage = Map("from" -> Seq(message.from), "to" -> Seq(message.to), "subject" -> Seq(message.subject), "text" -> Seq(message.text), "html" -> Seq(message.html.toString()), "inline" -> Seq(logoBase64))
    WS.url("https://api.mailgun.net/v2/sandboxaa9afcea1f2e4d5db5e2c080f7784b74.mailgun.org/messages").withAuth("api", "key-f165695d4c72e929ff8215115e648c95", WSAuthScheme.BASIC).post(postMessage)
  }

我将徽标转换为base64并将其转换为其他参数。仍然没有快乐。

我在这里缺少什么?我是否需要在体内传递它,但不知何故指定这是一个“内联”文件?

1 个答案:

答案 0 :(得分:2)

我使用Jersey解决了这个问题,如库中的部分所述:https://documentation.mailgun.com/libraries.html#java

我使用以下内容在sbt中导入Jersey:

libraryDependencies += "com.sun.jersey" % "jersey-core" % "1.18.3"

libraryDependencies += "com.sun.jersey" % "jersey-client" % "1.18.3"

libraryDependencies += "com.sun.jersey.contribs" % "jersey-multipart" % "1.18.3"

然后创建我的电子邮件发送对象:

object Email {

  val client = Client.create()
  client.addFilter(new HTTPBasicAuthFilter("api", current.configuration.getString("mailgun.api.key").get))
  val webResource = client.resource(current.configuration.getString("mailgun.api.url").get)

  def send(message:EmailMessage) = {
    val form = new FormDataMultiPart
    form.field("from", message.from)
    form.field("to", message.to)
    form.field("subject", message.subject)
    form.field("text", message.text)
    form.field("html", message.html.toString())
    val logo = Play.getExistingFile("/public/images/logo.png").get
    form.bodyPart(new FileDataBodyPart("inline", logo, MediaType.APPLICATION_OCTET_STREAM_TYPE))

    webResource.`type`(MediaType.MULTIPART_FORM_DATA_TYPE).post(form)
  }
}

我希望这有助于某人。