go-gomail / gomail

The best way to send emails in Go.
MIT License
4.29k stars 569 forks source link

How to send mail using .EML file/content in Golang gomail? #151

Open bsandy999 opened 3 years ago

bsandy999 commented 3 years ago

I am using Standard gomail package for sending mails in Golang. The mail generation part is happing from some other component which i am storing it in a particular location (i.e /path/sample.eml file). And hence , i have pre-cooked mail body in .EML file which i just want process as a new mail. I am able to put/parse the .EML content by using the parsemail package of DusanKasan. There are so many custom headers which i have already set in raw sample.eml file content which i want to send. It will be really helpful if i get an example code saying just pass .eml file as a input so that mail body will generate/render based on .eml file content. Also if possible i want to show the multiple to list in to header but mail should go/send to a first recipient only. Is it possible?

You can fine sample EML content string on .EML

Here is my basic mail sending code using gomail package.

m := gomail.NewMessage()
m.SetHeader("From", "alex@example.com")
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
m.Attach("/home/Alex/lolcat.jpg")

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

// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
    panic(err)
}

Here is my eml parsing code using parsemail parsemail package

var reader io.Reader // this reads an email message
email, err := parsemail.Parse(reader) // returns Email struct and error
if err != nil {
    // handle error
}

fmt.Println(email.Subject)
fmt.Println(email.From)
fmt.Println(email.To)
fmt.Println(email.HTMLBody)