VitalyGo877
VitalyGo877

Reputation: 123

Golang: how to send html body email with image using base64 string as attachment content

I have the following msg which i receive in my golang handler:

{"fromDisplayName":"ForgottenPassword","to":["[email protected]"],"cc":[],"bcc":[],"subject":"ForgottenPassword","body":" \t<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html> \t<head> \t\t<meta http-equiv=\"Content-Type\" content=\"text/html charset=UTF-8\" /> \t\t<title>Forgot Password Email</title> \t</head> \t<body style=\"width:100%; border:0; padding:0; margin:0; text-align:center; background-color:#f3f3f3; font-family:Gotham, 'Helvetica Neue', Helvetica, Arial, 'sans-serif'; font-size:1em\">\t\t \t\t<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width: 100%; max-width: 680px; margin: auto; margin-top: 30px; box-shadow: 0 1px 2px rgba(0,0,0,0.3);\"> \t\t\t<tr> \t\t\t\t<td>\t\t\t\t\t \t\t\t\t\t<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width: 100%;\"> \t\t\t\t\t\t<tr> \t\t\t\t\t\t\t<td style=\"background-color:#ffffff; height:4em; padding:1.5em;\"> \t\t\t\t\t\t\t\t<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width: 100%;\"> \t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t<td style=\"padding-bottom: 20px; text-align: left;\"></td> \t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t<td style=\"width: 100%; height: 1px; background-color: #ccc;\"></td> \t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t</table> \t\t\t\t\t\t\t</td> \t\t\t\t\t\t</tr> \t\t\t\t\t</table> \t\t\t\t\t<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%;\"> \t\t\t\t\t\t<tbody> \t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t<td style=\"width:100%; padding:30px; text-align: center; background-color:#ffffff\"> \t\t\t\t\t\t\t\t\t<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"> \t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t<td style=\"padding: 40px 40px 0px 40px; text-align: center; font-size:2.5em; font-weight:lighter; mso-height-rule: exactly; line-height: 28px; color: #434795;\">Hi user</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t<td style=\"text-align: center; font-size:1em; padding-top:0.8em; padding-bottom:2em; color:#999999\">You're already a user, so there is no need to sign up.</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t<td style=\"text-align: center; font-size:1em; padding-top:0.8em; padding-bottom:2em; color:#999999\">If you forgot your password, you can reset it .</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t<td style=\"text-align: center;\"> \t\t\t\t\t\t\t\t\t\t\t\t<a><img src=\"cid:278da187427a4bdcaceb76781f4a3753\" alt=\"forgot password\" width=\"100\" height=\"60\" border=\"0\"></a> \t\t\t\t\t\t\t\t\t\t\t</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t</table> \t\t\t\t\t\t\t\t\t<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"> \t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t<td> \t\t\t\t\t\t\t\t\t\t\t\t<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"> \t\t\t\t\t\t\t\t\t\t\t\t\t<tr> \t\t\t\t\t\t\t\t\t\t\t\t\t\t<td style = \"font-size:0.75em; padding-top:3.5em; padding-bottom:1.25em; text-align: center; color:#999999\">If you received this email by mistake, you can just delete it.</td> \t\t\t\t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t\t\t\t</table> \t\t\t\t\t\t\t\t\t\t\t</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t</table> \t\t\t\t\t\t\t\t\t<table cellspacing=\"0\" cellpadding=\"0\" text-align=\"center\" align=\"center\" bgcolor=\"#f3f3f3\" width=\"100%\" style=\"max-width: 680px\"> \t\t\t\t\t\t\t\t\t\t <tr> \t\t\t\t\t\t\t\t\t\t\t<td style=\" font-size: 0.75em; text-align: center; mso-height-rule: exactly; line-height:17px; padding-top: 20px;\"> \t\t\t\t\t\t\t\t\t\t\t\t<span style=\"color: #999999;\">We don't check this mailbox, so please don't reply to this message.</span><br> \t\t\t\t\t\t\t\t\t\t\t\t<span class=\"mobile-link--footer;\" style=\"color: #999999;\"></span><br><br> \t\t\t\t\t\t\t\t\t\t\t</td> \t\t\t\t\t\t\t\t\t\t</tr> \t\t\t\t\t\t\t\t\t</table> \t\t\t\t\t\t\t\t</td> \t\t\t\t\t\t\t</tr> \t\t\t\t\t\t</tbody> \t\t\t\t\t</table>\t\t\t\t\t \t\t\t\t</td>\t \t\t\t</tr> \t\t</table>\t\t \t</body> </html>","isHtmlBody":true,"attachments":[{"fileName":"click_button.png","content":"iVBORw0KGgoAAAANSUhEUgAAAGQAAAA8CAYAAACQPx/OAAAACXBIWXMAAAsTAAALEwEAmpwYAAAE92lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDUgNzkuMTYzNDk5LCAyMDE4LzA4LzEzLTE2OjQwOjIyICAgICAgICAiPiA8cmRmOlJERiB4bWx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/RZwV7n2U9/8LQOuIAI1xO136WOBwPwu8BQggG8HOMuYtYVIuvrJsR9GEnKiDPeNobHNyDiQAewAphP3F53MWdgX4sOQk+voLivRf+auHU90QnNIsnvgReJ23lpx6eS5ClgExHuIskf6EkUTyuBK4BvErd7w2NTgClE+BVR25mNcQMnSDrF/OEA4x2iVKccebt1DO+wF/FRjJow2A581SPmYdxOzBaQ0IwPieJrNvAAYESYQK25z9h6obDT3iOFYCwTvu4DglebPTWHx7qmpwnNwFNbOC48ja+O8Pt38mVK8ceQUiR4ObUR2A9UU8Z4OngBaCJuh3enQUt5mOBd+yTie8TtZ/kypTCvtB1p1NjLGOuB4zG+TDKMRLdwfyedqC0FxmLMIUIsn2K4cvS3EABfsxGLMHaEY9eJwETi5rTPnS8Gx5BOpHJ8rUlbdzw2MGYMjiFdNKmK97iECFUkWZXpd0kKQd6mvYMUj6N/UD/CGBSkxBgUpMQYFKTEGBSkxPgfWUMgcG3DvbIAAAAASUVORK5CYII=","contentType":"image/png","contentDisposition":"inline","cid":"278da187427a4bdcaceb76781f4a3753"}]}

My purpose is to send the information in the msg as an email via google smtp.

im able so send the message but the image content in the message(base64 string) is not embeded in the email html body. when i send the same message using nodejs and nodemailer - the image is shown correctly.

how can i achive this?

this is an example of one of the approaches i tried and does not show the image in the email body ("github.com/jordan-wright/email"):

func EmailServiceBusinessLogic(receivedMsg emailModels.ReceivedMsgModel) error{


    mail := email.NewEmail()

    for _,v := range receivedMsg.Attachments{
        if v.FileName == "" {
            fmt.Println("received empty file name for attachment")
            continue
        }
        if v.Cid == "" {
            fmt.Println("received empty cid for file name: ", v.FileName)
            continue
        }
        if v.ContentType == "" {
            fmt.Println("received empty ContentType for file name: ", v.FileName)
            continue
        }
        if v.Content == "" {
            fmt.Println("received empty Content for file name: ", v.FileName)
            continue
        }

        mail.Attach(strings.NewReader(v.Content)  , v.Cid, v.ContentType)
    }

    if receivedMsg.FromDisplayName == "" {
        fmt.Println("received empty FromDisplayName: aborting send")
        return errors.New("received empty FromDisplayName: aborting send")

    }
    fromWithoutWhiteSpaces := strings.Join(strings.Fields(receivedMsg.FromDisplayName), "")


    mail.From = fromWithoutWhiteSpaces+"@dont-reply.com"
    mail.To = receivedMsg.To
    mail.Bcc = receivedMsg.Bcc
    mail.Cc = receivedMsg.Cc
    mail.Subject = receivedMsg.Subject
    if receivedMsg.IsHtmlBody {
        mail.HTML = []byte(receivedMsg.Body)
    }else {
        mail.Text = []byte(receivedMsg.Body)
    }


    err := mail.Send(emailGlobals.SmtpConf.Body[0].Value.Host+":587", smtp.PlainAuth("", emailGlobals.SmtpConf.Body[0].Value.Username, emailGlobals.SmtpConf.Body[0].Value.Password, emailGlobals.SmtpConf.Body[0].Value.Host))
    if err != nil {
        return err
    }
}

EDIT:

SOLVED

thanks to @Steffen Ullrich comments Because in my json the image content already base64 encoded and the library will put another base64 layer on top of it, so I had to base64 decode it first like so:

func EmailServiceBusinessLogic(receivedMsg emailModels.ReceivedMsgModel) error {

    mail := email.NewEmail()

    for _, v := range receivedMsg.Attachments {
        if v.FileName == "" {
            fmt.Println("received empty file name for attachment")
            continue
        }
        if v.Cid == "" {
            fmt.Println("received empty cid for file name: ", v.FileName)
            continue
        }
        if v.ContentType == "" {
            fmt.Println("received empty ContentType for file name: ", v.FileName)
            continue
        }
        if v.Content == "" {
            fmt.Println("received empty Content for file name: ", v.FileName)
            continue
        }

        // Base64 Standard Decoding <-this is the part that was missing!!
        sDec, err := base64.StdEncoding.DecodeString(v.Content)
        if err != nil {
            fmt.Printf("Error decoding Content: %s ", err.Error())
            continue
        }

        mail.Attach(strings.NewReader(string(sDec)), v.Cid, v.ContentType)
    }

    if receivedMsg.FromDisplayName == "" {
        fmt.Println("received empty FromDisplayName: aborting send")
        return errors.New("received empty FromDisplayName: aborting send")

    }
    fromWithoutWhiteSpaces := strings.Join(strings.Fields(receivedMsg.FromDisplayName), "")

    mail.From = fromWithoutWhiteSpaces + "@dont-reply.com"
    mail.To = receivedMsg.To
    mail.Bcc = receivedMsg.Bcc
    mail.Cc = receivedMsg.Cc
    mail.Subject = receivedMsg.Subject
    if receivedMsg.IsHtmlBody {
        mail.HTML = []byte(receivedMsg.Body)
    } else {
        mail.Text = []byte(receivedMsg.Body)
    }

    err := mail.Send(emailGlobals.SmtpConf.Body[0].Value.Host+":587", smtp.PlainAuth("", emailGlobals.SmtpConf.Body[0].Value.Username, emailGlobals.SmtpConf.Body[0].Value.Password, emailGlobals.SmtpConf.Body[0].Value.Host))
    if err != nil {
        return err
    }
}

Upvotes: 1

Views: 5462

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123461

To reference an attached image from inside the HTML it must have a Content-Id, i.e. in the HTML it needs to look like this:

`<img src="cid:000001.png">

Looking at the source of Attach in "github.com/jordan-wright/email which I assume you use it shows that it is using the given filename as Content-Id:

func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) {
    ....
    at.Header.Set("Content-ID", fmt.Sprintf("<%s>", filename))

Thus, make sure that the v.FileName you use as name for your attachment is actually used as the cid in the HTML part.


After more information were added to the question:

... the only information i have regarding the attachments is: file name, content(string), content type(image/png) and contentDisposition atribute set to inline and cid

In this case you the cid and not file name.


With even more information added to the question:

have the following msg which i receive in my golang handler:

{"fromDisplayName":"Forg...

It looks like that your image is already base64 encoded, but the library you use does an additional base64 encoding, so you have to decode it first. Also the library explicitly uses a Content-Type of multipart/mixed but it needs to be multipart/related so that the images are associated with the HTML.

The following code works for me with the JSON from your question stored as mail.json. Note that I omitted error checking in several places in order to focus on the solution. And I don't directly send the mail but store it only as a string to store it as an EML file and check it. You likely need to change the Bytes function to set multipart/related directly there:

package main

import (
    "github.com/jordan-wright/email"
    "encoding/json"
    "encoding/base64"
    "fmt"
    "os"
    "bytes"
    "strings"
)

type Attachments struct {
    Filename string `json:"filename"`
    Content string `json:"content"`
    ContentType string `json:"contentType"`
    ContentDisposition string `json:"contentDisposition"`
    Cid string `json:"cid"`
}

type Mail struct {
    To []string `json:"to"`
    Subject string `json:"subject"`
    Body string `json:"body"`
    Parts []Attachments `json:"attachments"`
}

func main() {
    f,_ := os.Open("mail.json")
    d := json.NewDecoder(f)
    m := &Mail{}
    d.Decode(&m)
    // fmt.Println(m)

    e := email.NewEmail()
    e.To = m.To
    e.Subject = m.Subject
    e.HTML = []byte(m.Body)
    for _,p := range m.Parts {
        s,_ := base64.StdEncoding.DecodeString(string(p.Content))
        e.Attach(bytes.NewReader(s), p.Cid, p.ContentType)
    }
    s,_ := e.Bytes()
    mail := strings.Replace(string(s),": multipart/mixed;",": multipart/related;",1)
    fmt.Println(mail)
}
                                                        

Upvotes: 2

Related Questions