dongtui8593 2016-12-13 04:10
浏览 148
已采纳

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

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", "XXXX@gmail.com")
mail.SetHeader("To", "XXXX@gmail.com")
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.

  • 写回答

1条回答 默认 最新

  • doucan8521 2016-12-13 08:47
    关注

    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 data:mime_type;base64,image_encoded_base64, if we look the example, is data:image/png;base64,iVBORw0K...

    EDIT:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)