Skip to content

Instantly share code, notes, and snippets.

@sym3tri
Last active June 10, 2016 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sym3tri/8a29ddecd65ec4f8ccfc to your computer and use it in GitHub Desktop.
Save sym3tri/8a29ddecd65ec4f8ccfc to your computer and use it in GitHub Desktop.
easily email an attachment without reading from disk
package main
import (
"fmt"
"bytes"
"io/ioutil"
"github.com/mailgun/mailgun-go"
)
func main() {
gun := mailgun.NewMailgun("valid-mailgun-domain", "private-mailgun-key", "public-mailgun-key")
// Create a new io.ReadCloser that reads an in-memory byte array buffer.
buf := &bytes.Buffer{}
buf.Write([]byte("these are the file contents"))
rc := ioutil.NopCloser(buf)
m := mailgun.NewMessage("from@example.com", "Subject", "message body", "to@example.com")
m.AddReaderAttachment("my-file.txt", rc)
response, id, _ := gun.Send(m)
fmt.Printf("Response ID: %s\n", id)
fmt.Printf("Message from server: %s\n", response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment