gomail/example_test.go

197 lines
4.5 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-07-26 00:54:59 +08:00
"gopkg.in/gomail.v2-unstable"
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.NewPlainDialer("smtp.example.com", 587, "user", "123456")
2015-07-26 00:54:59 +08:00
// Send the email to Bob, Cora and Dan
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.NewPlainDialer("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() {
// The list of recipient.
var list []struct {
Name string
Address string
}
d := gomail.NewPlainDialer("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))
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-07-26 00:54:59 +08:00
func Example_send() {
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
func ExampleSetCharset() {
m = gomail.NewMessage(gomail.SetCharset("ISO-8859-1"))
}
func ExampleSetEncoding() {
m = gomail.NewMessage(gomail.SetEncoding(gomail.Base64))
}
func ExampleMessage_SetHeaders() {
m.SetHeaders(map[string][]string{
"From": {m.FormatAddress("alex@example.com", "Alex")},
"To": {"bob@example.com", "cora@example.com"},
"Subject": {"Hello"},
})
}
func ExampleMessage_FormatAddress() {
m.SetHeader("To", m.FormatAddress("bob@example.com", "Bob"), m.FormatAddress("cora@example.com", "Cora"))
}
func ExampleMessage_SetDateHeader() {
m.SetDateHeader("X-Date", time.Now())
}
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")
})
}
func ExampleSetHeader() {
h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
m.Attach("foo.jpg", gomail.SetHeader(h))
}
func ExampleSetCopyFunc() {
m.Attach("foo.txt", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write([]byte("Content of foo.txt"))
return err
}))
}
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" />`)
}