如何将图像添加到电子邮件的html正文

如何将图像添加到电子邮件的html正文

本文介绍了如何将图像添加到电子邮件的html正文(Go)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用





首先,我发送 img src 通过 POST 请求,然后保存到以下 struct
$ p $ 类型t​​est_struct结构{
测试字符串`json:image`
}

然后,我尝试将它附加到电子邮件正文中,就像这样...

  mail:= gomail.NewMessage()
mail.SetHeader(From,[email protected]
mail.SetHeader(To,[email protected]
mail.SetHeader(Subject,IMAGE!)

mail.SetBody(text / html,`< img src =cid:t.Testalt =My Image/>`)

这不起作用,所以我尝试使用 ...

  mail.Embed(t.Test)

这给了我一个空白的图像,错误的文件名或扩展名太长。不知道这是否很重要,但请注意,我得到的 src 来自 HTML5 Canvas。我从Canvas获得了图像的源代码...

  localCanvas.toDataURL('image / png'); 

这是我保存在 t.Test $ b

  buf,err:= ioutil.ReadAll(req.Body)
reader:= bytes.NewReader(buf)

var t test_struct

err = json.NewDecoder(reader).Decode(& t)

保存在 t.Test 中的字符串具有以下格式...

  data:image / png:base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgA ... 

如何将此邮件发送给我的电子邮件?这个文档只是让我到目前为止。

解决方案

首先,检查t.Test中的数据是否正确(如果包含文件名,路径,或任何你需要引用图像)

好吧,你有一些选择,首先,如果没关系只是附加图像,你应该做的:

  mail.Attach(/ path / of / your / image / image.png)

code>

但是,如果您需要在邮件中使用图片,则可以选择其他方式。



Alt 1:使用嵌入方法

您必须在您的文件系统中拥有该映像,因为该方法是以这种方式定义的。您必须执行以下操作:

  mail.SetBody(text / html,Image< img src = \ cid:tux.png \/>)
mail.Embed(/ path / of / your / image / tux.png)
pre>

在这种情况下,您必须使用图像名称 tux.png html body与 img 标记,您必须定义 src ,例如 cid:tux。 png



Alt 2:使用base64编码



为此,您需要知道图像文件的MIME类型,在我的例子中是一个PNG文件,所以MIME是 image / png

  img,err:= ioutil.ReadFile(/ path / of /你的/ image / tux.png)
if err!= nil {
//检查错误
}
out:= base64.StdEncoding.EncodeToString(img)

或者,如果您在文件系统中没有映像,但是您的映像位于变量中( [] byte type),您可以将它与 EncodeToString

  var imageData [] byte 
//在某些部分,您将图像数据加载到imageData
out:= base64.StdEncoding.EncodeToString(imageData)

您可以像使用它一样

  mail.SetBody(text / html,fmt.Sprintf(Image< img src = \data:image / png; base64,%s\/>,out)) 

所以,当你使用 img 时,你如果我们看例子,必须在 src 中嵌入一个带有 data:mime_type; base64,image_encoded_base64 的图片。 data:image / png; base64,iVBORw0K ...



编辑:



我用JSON请求和处理程序创建了一个示例


I am using Gomail

I am attempting to send an email to myself. I have figured out how to do so, but now I want to add an image, either in the html body or as an attachment (does not really matter). I just need to be able to see the image in my email.

First off, I am sending the img src through a POST request, which I then save into the following struct...

type test_struct struct {
    Test string `json:"image"`
}

I then try to attach it in the email body like so...

mail := gomail.NewMessage()
mail.SetHeader("From", "[email protected]")
mail.SetHeader("To", "[email protected]")
mail.SetHeader("Subject", "IMAGE!")

mail.SetBody("text/html", `<img src="cid:t.Test" alt="My Image"/>`)

this did not work, so I tried to use Embed method...

mail.Embed(t.Test)

This gave me a blank image with the error the filename or extension is too long. Not sure if this is important, but note that the src I got is from HTML5 Canvas. I got the source of the image from Canvas...

localCanvas.toDataURL('image/png');

And this is the value I save in t.Test...

buf, err := ioutil.ReadAll(req.Body)
reader := bytes.NewReader(buf)

var t test_struct

err = json.NewDecoder(reader).Decode(&t)

The string being save in the t.Test is of the following format...

data:image/png:base64, iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgA...

How can I send this to my email? The documentation only gets me so far.

解决方案

First of all, check if the data in t.Test is correct (if contains the filename, path, or whatever you need to reference the image)

Well, you have some options, first, if it's ok just attaching the image, you only should do:

mail.Attach("/path/of/your/image/image.png")

But, if you need to use the image in the message, you have some alternatives.

Alt 1: Use Embed method

You must to have the image in your filesystem, because the method is defined in that way. You have to do something like:

mail.SetBody("text/html", "Image <img src=\"cid:tux.png\"/>")
mail.Embed("/path/of/your/image/tux.png")

You must to use the image name, in this case tux.png, when you add it in the html body with the img tag, you have to define the src like cid:tux.png

Alt 2: Use a base64 encoding

For this you need to know the mime type of the image file, in my example is a png file, so the mime is image/png. Also, you have to encode the image as base64.

img, err := ioutil.ReadFile("/path/of/your/image/tux.png")
if err != nil {
    // check errors
}
out := base64.StdEncoding.EncodeToString(img)

Or, if you don't have the image in the filesystem, but you have it in a variable ([]byte type), you can use it with EncodeToString

var imageData []byte
// in some part, you load the image data into imageData
out := base64.StdEncoding.EncodeToString(imageData)

And you use it like

mail.SetBody("text/html", fmt.Sprintf("Image <img src=\"data:image/png;base64,%s\"/>", out))

So, when you use img, you have to set in src an embedded image with , if we look the example, is

EDIT:

I created an example with the JSON request and the handler Example

这篇关于如何将图像添加到电子邮件的html正文(Go)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:48