Hi Andrey, Thank you. I have been trying to figure out how to use your example. But the email that I receive from it are plain text even the encoded attached file. It does not appear as attached but as encoded characters in the mail body. And there is still no attachment seen. I tried modifying your example but to no avail. I end up using your example code per code but still does not work. Please see below excerpt of my code where I call your example in a function I called "attachToEmail". I have a send mail function "sendEmailReceipt" using smpt:
I do not know which part I am missing. I have been trying to figure this out for more than a week now. func sendEmailReceipt(email string, typeOfEmail int, lastInsertedId int) { addr := EmailHostName + ":" + EmailServerPort fromName := "FYLD" fromEmail := "f...@fyld.com" toNames := []string{"Client"} toEmails := []string{email} subject := "FYLD Email Receipt" body := "This is to confirm that we received your email." if IncludeEmailDump { body = body + getStats(email, lastInsertedId) } // Build RFC-2822 email toAddresses := []string{} for i := range toEmails { mAdd := mail.Address{} mAdd.Name = toNames[i] mAdd.Address = toEmails[i] toAddresses = append(toAddresses, mAdd.String()) } toHeader := strings.Join(toAddresses, ", ") fAdd := mail.Address{} fAdd.Name = fromName fAdd.Address = fromEmail fromHeader := fAdd.String() subjectHeader := subject header := make(map[string]string) header["To"] = toHeader header["From"] = fromHeader header["Subject"] = subjectHeader header["Content-Type"] = `text/html; charset="UTF-8"` msg := "" for headerKey, headerValue := range header { msg += fmt.Sprintf("%s: %s\r\n", headerKey, headerValue) } msg += "\r\n" + body bMsg := []byte(msg) // Send using local postfix service smtpPostfix, err := smtp.Dial(addr) if err != nil { logError(err.Error()) } defer smtpPostfix.Close() if err = smtpPostfix.Mail(fromHeader); err != nil { logError(err.Error()) } for _, addr := range toEmails { if err = smtpPostfix.Rcpt(addr); err != nil { logError(err.Error()) } } smtpData, err := smtpPostfix.Data() if err != nil { logError(err.Error()) } _, err = smtpData.Write(bMsg) if err != nil { logError(err.Error()) } attachToEmail(smtpData, "file.go", "boundary1234567890") err = smtpData.Close() if err != nil { logError(err.Error()) } err = smtpPostfix.Quit() if err != nil { logError(err.Error()) } //attachToEmail(email) } func attachToEmail(writeToEmail io.Writer, filename string, boundary string) { fmt.Fprintf(writeToEmail, "\n--%s\n", "\n\n") fmt.Fprintf(writeToEmail, "\n--%s\n", boundary) contents, err := os.Open(filename) if err != nil { fmt.Fprintf(writeToEmail, "Content-Type: text/plain; charset=utf-8\n") fmt.Fprintf(writeToEmail, "could not open file: %v\n", err) } else { defer contents.Close() //fmt.Fprintf(writeToEmail, "Content-Type: application/octet-stream\n") fmt.Fprintf(writeToEmail, "Content-Type: application/octet-stream; charset=utf-8\n") fmt.Fprintf(writeToEmail, "Content-Transfer-Encoding: base64\n") fmt.Fprintf(writeToEmail, "Content-Disposition: attachment;filename=\"%s\"\n\n", filepath.Base(filename)+".gz") fmt.Fprintf(writeToEmail, "\n--%s\n", boundary) fmt.Fprintf(writeToEmail, "\n\n\n") b64 := base64.NewEncoder(base64.StdEncoding, writeToEmail) gzip := gzip.NewWriter(b64) io.Copy(gzip, contents) //io.Copy(b64, contents) gzip.Close() b64.Close() } fmt.Fprintf(writeToEmail, "\n--%s\n", boundary) } On Sun, Jul 16, 2017 at 12:34 PM, andrey mirtchovski <mirtchov...@gmail.com> wrote: > the code below which gzips and attaches a file to an email io.Writer > has worked for me for close to 5 years. it's not idiomatic go code so > don't use it verbatim, only as an example. > > the boundary is something random the client chooses. i use a sha256 > baked into the client. the same for every attachment. > > func attach(w io.Writer, file, boundary string) { > fmt.Fprintf(w, "\n--%s\n", boundary) > contents, err := os.Open(file) > if err != nil { > fmt.Fprintf(w, "Content-Type: text/plain; charset=utf-8\n") > fmt.Fprintf(w, "could not open file: %v\n", err) > } else { > defer contents.Close() > fmt.Fprintf(w, "Content-Type: application/octet-stream\n") > fmt.Fprintf(w, "Content-Transfer-Encoding: base64\n") > fmt.Fprintf(w, "Content-Disposition: attachment; > filename=\"%s\"\n\n", filepath.Base(file)+".gz") > > b64 := base64.NewEncoder(base64.StdEncoding, w) > gzip := gzip.NewWriter(b64) > io.Copy(gzip, contents) > gzip.Close() > b64.Close() > } > fmt.Fprintf(w, "\n--%s\n", boundary) > } > > On Sat, Jul 15, 2017 at 7:48 PM, jesse junsay <jessej3...@gmail.com> > wrote: > > Hi, > > > > Been trying do email attachments in golang using net/smtp and mail > packages > > but to no avail. I tried attaching email and extracting attachments from > > email using multipart but I just cant figure out how that works in > golang. I > > have tried to look for samples online and even posting on golang forum > and > > stackoverflow. It seems like no one has ever done this issue before. Any > > help will be greatly appreciated. > > > > > > -- > > You received this message because you are subscribed to the Google Groups > > "golang-nuts" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to golang-nuts+unsubscr...@googlegroups.com. > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.