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 <[email protected]> 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 [email protected].
> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.