title: How to add image to html body of an email (Go)
body: <p>I am using <a href="https://github.com/go-gomail/gomail" rel="nofollow noreferrer">Gomail</a></p>
  
  <p>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.</p>
  
  <p>First off, I am sending the <code>img</code> <code>src</code> through a <code>POST</code> request, which I then save into the following <code>struct</code>...</p>
  
  <pre><code>type test_struct struct {
      Test string `json:"image"`
  }
  </code></pre>
  
  <p>I then try to attach it in the email body like so...</p>
  
  <pre><code>mail := gomail.NewMessage()
  mail.SetHeader("From", "XXXX@gmail.com")
  mail.SetHeader("To", "XXXX@gmail.com")
  mail.SetHeader("Subject", "IMAGE!")
  
  mail.SetBody("text/html", `&lt;img src="cid:t.Test" alt="My Image"/&gt;`)
  </code></pre>
  
  <p>this did not work, so I tried to use <a href="https://godoc.org/gopkg.in/gomail.v2#Message.Embed" rel="nofollow noreferrer">Embed method</a>...</p>
  
  <pre><code>mail.Embed(t.Test)
  </code></pre>
  
  <p>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 <code>src</code> I got is from <code>HTML5</code> Canvas. I got the source of the image from Canvas...</p>
  
  <pre><code>localCanvas.toDataURL('image/png');
  </code></pre>
  
  <p>And this is the value I save in <code>t.Test</code>...</p>
  
  <pre><code>buf, err := ioutil.ReadAll(req.Body)
  reader := bytes.NewReader(buf)
  
  var t test_struct
  
  err = json.NewDecoder(reader).Decode(&amp;t)
  </code></pre>
  
  <p>The string being save in the <code>t.Test</code> is of the following format...</p>
  
  <pre><code>data:image/png:base64, iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgA...
  </code></pre>
  
  <p>How can I send this to my email? The documentation only gets me so far.</p>