gomail/example_test.go

224 lines
5.0 KiB
Go
Raw Normal View History

2015-07-17 05:06:29 +08:00
package gomail_test
import (
"fmt"
2015-08-21 02:07:45 +08:00
"html/template"
2015-07-17 05:06:29 +08:00
"io"
2015-08-21 02:07:45 +08:00
"log"
2015-07-26 00:54:59 +08:00
"time"
2015-07-17 05:06:29 +08:00
2015-09-02 19:41:40 +08:00
"gopkg.in/gomail.v2"
2015-07-17 05:06:29 +08:00
)
2015-07-26 00:54:59 +08:00
func Example() {
m := gomail.NewMessage()
m.SetHeader("From", "alex@example.com")
m.SetHeader("To", "bob@example.com", "cora@example.com")
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
2015-08-04 04:57:38 +08:00
m.Attach("/home/Alex/lolcat.jpg")
2015-07-26 00:54:59 +08:00
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
2015-07-26 00:54:59 +08:00
2015-08-24 00:55:53 +08:00
// Send the email to Bob, Cora and Dan.
2015-07-26 00:54:59 +08:00
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
2015-08-21 02:07:45 +08:00
// A daemon that listens to a channel and sends all incoming messages.
2015-07-26 00:54:59 +08:00
func Example_daemon() {
ch := make(chan *gomail.Message)
go func() {
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
2015-07-26 00:54:59 +08:00
var s gomail.SendCloser
var err error
open := false
for {
select {
case m, ok := <-ch:
if !ok {
return
}
if !open {
if s, err = d.Dial(); err != nil {
panic(err)
}
open = true
}
if err := gomail.Send(s, m); err != nil {
2015-08-21 02:07:45 +08:00
log.Print(err)
2015-07-26 00:54:59 +08:00
}
// Close the connection to the SMTP server if no email was sent in
// the last 30 seconds.
case <-time.After(30 * time.Second):
if open {
2015-08-21 02:07:45 +08:00
if err := s.Close(); err != nil {
panic(err)
}
2015-07-26 00:54:59 +08:00
open = false
}
}
}
}()
// Use the channel in your program to send emails.
// Close the channel to stop the mail daemon.
close(ch)
}
2015-08-21 02:07:45 +08:00
// Efficiently send a customized newsletter to a list of recipients.
func Example_newsletter() {
2015-09-02 19:38:54 +08:00
// The list of recipients.
2015-08-21 02:07:45 +08:00
var list []struct {
Name string
Address string
}
d := gomail.NewDialer("smtp.example.com", 587, "user", "123456")
2015-08-21 02:07:45 +08:00
s, err := d.Dial()
if err != nil {
panic(err)
}
m := gomail.NewMessage()
for _, r := range list {
m.SetHeader("From", "no-reply@example.com")
m.SetAddressHeader("To", r.Address, r.Name)
m.SetHeader("Subject", "Newsletter #1")
m.SetBody("text/html", fmt.Sprintf("Hello %s!", r.Name))
2015-09-02 19:38:54 +08:00
2015-08-21 02:07:45 +08:00
if err := gomail.Send(s, m); err != nil {
log.Printf("Could not send email to %q: %v", r.Address, err)
}
m.Reset()
}
}
// Send an email using a local SMTP server.
2015-07-26 00:54:59 +08:00
func Example_noAuth() {
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
2015-08-21 02:07:45 +08:00
d := gomail.Dialer{Host: "localhost", Port: 587}
2015-07-26 00:54:59 +08:00
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
2015-08-21 02:07:45 +08:00
// Send an email using an API or postfix.
2015-08-24 00:55:53 +08:00
func Example_noSMTP() {
2015-07-26 00:54:59 +08:00
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Hello!")
m.SetBody("text/plain", "Hello!")
2015-07-17 05:06:29 +08:00
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
// Implements you email-sending function, for example by calling
// an API, or running postfix, etc.
fmt.Println("From:", from)
2015-07-26 00:54:59 +08:00
fmt.Println("To:", to)
2015-07-17 05:06:29 +08:00
return nil
})
2015-07-26 00:54:59 +08:00
if err := gomail.Send(s, m); err != nil {
2015-07-17 05:06:29 +08:00
panic(err)
}
// Output:
2015-07-26 00:54:59 +08:00
// From: from@example.com
// To: [to@example.com]
2015-07-17 05:06:29 +08:00
}
2015-08-21 02:07:45 +08:00
var m *gomail.Message
2015-08-24 00:55:53 +08:00
func ExampleSetCopyFunc() {
m.Attach("foo.txt", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write([]byte("Content of foo.txt"))
return err
}))
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleSetHeader() {
h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
m.Attach("foo.jpg", gomail.SetHeader(h))
2015-08-21 02:07:45 +08:00
}
func ExampleRename() {
m.Attach("/tmp/0000146.jpg", gomail.Rename("picture.jpg"))
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_AddAlternative() {
m.SetBody("text/plain", "Hello!")
m.AddAlternative("text/html", "<p>Hello!</p>")
}
func ExampleMessage_AddAlternativeWriter() {
t := template.Must(template.New("example").Parse("Hello {{.}}!"))
m.AddAlternativeWriter("text/plain", func(w io.Writer) error {
return t.Execute(w, "Bob")
2015-08-21 02:07:45 +08:00
})
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_Attach() {
m.Attach("/tmp/image.jpg")
}
func ExampleMessage_Embed() {
m.Embed("/tmp/image.jpg")
m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
}
2015-08-21 02:07:45 +08:00
func ExampleMessage_FormatAddress() {
m.SetHeader("To", m.FormatAddress("bob@example.com", "Bob"), m.FormatAddress("cora@example.com", "Cora"))
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_FormatDate() {
m.SetHeaders(map[string][]string{
"X-Date": {m.FormatDate(time.Now())},
})
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_SetAddressHeader() {
m.SetAddressHeader("To", "bob@example.com", "Bob")
}
func ExampleMessage_SetBody() {
2015-08-21 02:07:45 +08:00
m.SetBody("text/plain", "Hello!")
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_SetDateHeader() {
m.SetDateHeader("X-Date", time.Now())
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_SetHeader() {
m.SetHeader("Subject", "Hello!")
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleMessage_SetHeaders() {
m.SetHeaders(map[string][]string{
"From": {m.FormatAddress("alex@example.com", "Alex")},
"To": {"bob@example.com", "cora@example.com"},
"Subject": {"Hello"},
})
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleSetCharset() {
m = gomail.NewMessage(gomail.SetCharset("ISO-8859-1"))
2015-08-21 02:07:45 +08:00
}
2015-08-24 00:55:53 +08:00
func ExampleSetEncoding() {
m = gomail.NewMessage(gomail.SetEncoding(gomail.Base64))
2015-08-21 02:07:45 +08:00
}
2015-12-13 00:01:51 +08:00
func ExampleSetPartEncoding() {
m.SetBody("text/plain", "Hello!", gomail.SetPartEncoding(gomail.Unencoded))
}