Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Insert an image into email body?

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.

like image 858
Dharani Dharan Avatar asked Mar 13 '23 14:03

Dharani Dharan


1 Answers

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", "[email protected]")
    m.SetHeader("To", "[email protected]")
    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)
    }
}
like image 55
Ale Avatar answered Mar 25 '23 10:03

Ale