title: SMTP email with CSV attachment - extra characters coming in at the end of file
body: <p>I'm creating an email client using Golang, to send email with a CSV file attached. Everything is working fine except that in the received email attachment, I can see some unwanted extra characters at the end of the file.</p>
  <p>My code snippet:</p>
  <pre><code>
  import (
      &quot;bytes&quot;
      &quot;encoding/base64&quot;
      &quot;fmt&quot;
      &quot;mime/multipart&quot;
      &quot;net/smtp&quot;
  
      ...
  )
  
  func SendEmail(cfg Config) error {
  
      body := bytes.NewBuffer(nil)
      body.WriteString(fmt.Sprintf(&quot;From: %s\n&quot;, cfg.EmailFrom))
      body.WriteString(fmt.Sprintf(&quot;To: %s\n&quot;, cfg.EmailTo))
      body.WriteString(fmt.Sprintf(&quot;Subject: %s\n&quot;, cfg.EmailSubject))
  
      // csv file to attach
      fileContents := `column1,column2,column3\nAAA,BBB,CCC\nDDD,EEE,FFF\n`
      fileContentBytes := []byte(fileContents)
  
      body.WriteString(&quot;MIME-Version: 1.0\n&quot;)
      writer := multipart.NewWriter(body)
      boundary := writer.Boundary()
  
      // attach file
      body.WriteString(&quot;Content-Type: text/plain\n&quot;)
      body.WriteString(&quot;Content-Transfer-Encoding: base64\n&quot;)
      body.WriteString(fmt.Sprintf(&quot;Content-Disposition: attachment; filename=%s\n&quot;, &quot;test-filename&quot;))
  
      encodedBytes := make([]byte, base64.StdEncoding.EncodedLen(len(fileContentBytes)))
      base64.StdEncoding.Encode(encodedBytes, fileContentBytes)
      body.Write(encodedBytes)
      body.WriteString(fmt.Sprintf(&quot;\n--%s--&quot;, boundary))
  
      err = smtp.SendMail(cfg.EmailSMTPHost+&quot;:&quot;+cfg.EmailSMTPPort,
          nil, cfg.EmailFrom, []string{cfg.EmailTo}, body.Bytes())
      if err != nil {
          return errors.Wrap(err, &quot;smtp.SendMail failed&quot;)
      }
  
      return nil
  }
  </code></pre>
  <p>Expected csv file:</p>
  <pre><code>column1,column2,column3
  AAA,BBB,CCC
  DDD,EEE,FFF
  </code></pre>
  <p>Obtained csv file:</p>
  <pre><code>column1,column2,column3
  AAA,BBB,CCC
  DDD,EEE,FFF
  5k§xõí»ã}8
  </code></pre>
  <p>Anything wrong in the file contents encoding? Any help will be appreciated, thanks!</p>