Added examples

This commit is contained in:
Alexandre Cesaro 2015-07-25 18:54:59 +02:00
parent 42c5f98096
commit 394a6b2fcd
1 changed files with 83 additions and 13 deletions

View File

@ -3,31 +3,101 @@ package gomail_test
import ( import (
"fmt" "fmt"
"io" "io"
"strings" "time"
"github.com/go-gomail/gomail" "gopkg.in/gomail.v2-unstable"
) )
func ExampleSend() { func Example() {
msg := gomail.NewMessage() m := gomail.NewMessage()
msg.SetHeader("From", "alex@example.com") m.SetHeader("From", "alex@example.com")
msg.SetHeader("To", "bob@example.com", "cora@example.com") m.SetHeader("To", "bob@example.com", "cora@example.com")
msg.SetAddressHeader("Cc", "dan@example.com", "Dan") m.SetAddressHeader("Cc", "dan@example.com", "Dan")
msg.SetHeader("Subject", "Hello!") m.SetHeader("Subject", "Hello!")
msg.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!") m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
m.Attach(gomail.NewFile("/home/Alex/lolcat.jpg"))
d := gomail.NewPlainDialer("smtp.example.com", "user", "123456", 587)
// Send the email to Bob, Cora and Dan
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
func Example_daemon() {
ch := make(chan *gomail.Message)
go func() {
d := gomail.NewPlainDialer("smtp.example.com", "user", "123456", 587)
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 {
panic(err)
}
// 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 {
s.Close()
open = false
}
}
}
}()
// Use the channel in your program to send emails.
// Close the channel to stop the mail daemon.
close(ch)
}
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!")
d := gomail.Dialer{Host: "smtp.example.com", Port: 587}
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
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!")
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error { s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
// Implements you email-sending function, for example by calling // Implements you email-sending function, for example by calling
// an API, or running postfix, etc. // an API, or running postfix, etc.
fmt.Println("From:", from) fmt.Println("From:", from)
fmt.Println("To:", strings.Join(to, ", ")) fmt.Println("To:", to)
return nil return nil
}) })
if err := gomail.Send(s, msg); err != nil { if err := gomail.Send(s, m); err != nil {
panic(err) panic(err)
} }
// Output: // Output:
// From: alex@example.com // From: from@example.com
// To: bob@example.com, cora@example.com, dan@example.com // To: [to@example.com]
} }