How do I Insert an image into email body?

笑着哭i 提交于 2020-01-01 12:18:12

问题


I want to send a image in email body using go lang. Used this package from github

https://github.com/scorredoira/email

    err := m.Attach("image.png")
    if err1 != nil {
        fmt.Println(err1)
    }

Now i am able to send image file as attachment but my need is to send a image file in email body.

Thanks in advance.


回答1:


You can use Gomail (I'm the author). Have a look at the Embed method which allow you to embed images in the email body:

package main

import "gopkg.in/gomail.v2"

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "from@example.com")
    m.SetHeader("To", "to@example.com")
    m.SetHeader("Subject", "Hello!")
    m.Embed("image.png")
    m.SetBody("text/html", `<img src="cid:image.png" alt="My image" />`)

    d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}


来源:https://stackoverflow.com/questions/34790771/how-do-i-insert-an-image-into-email-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!