Added examples

This commit is contained in:
Alexandre Cesaro 2015-08-23 18:55:53 +02:00
parent 1e43a4157e
commit 03dbed963f
4 changed files with 64 additions and 42 deletions

View File

@ -21,7 +21,7 @@ func Example() {
d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
// Send the email to Bob, Cora and Dan
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
panic(err)
}
@ -113,7 +113,7 @@ func Example_noAuth() {
}
// Send an email using an API or postfix.
func Example_send() {
func Example_noSMTP() {
m := gomail.NewMessage()
m.SetHeader("From", "from@example.com")
m.SetHeader("To", "to@example.com")
@ -138,28 +138,16 @@ func Example_send() {
var m *gomail.Message
func ExampleSetCharset() {
m = gomail.NewMessage(gomail.SetCharset("ISO-8859-1"))
func ExampleSetCopyFunc() {
m.Attach("foo.txt", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write([]byte("Content of foo.txt"))
return err
}))
}
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 ExampleSetHeader() {
h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
m.Attach("foo.jpg", gomail.SetHeader(h))
}
func ExampleMessage_AddAlternative() {
@ -174,18 +162,6 @@ func ExampleMessage_AddAlternativeWriter() {
})
}
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")
}
@ -194,3 +170,45 @@ func ExampleMessage_Embed() {
m.Embed("/tmp/image.jpg")
m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
}
func ExampleMessage_FormatAddress() {
m.SetHeader("To", m.FormatAddress("bob@example.com", "Bob"), m.FormatAddress("cora@example.com", "Cora"))
}
func ExampleMessage_FormatDate() {
m.SetHeaders(map[string][]string{
"X-Date": {m.FormatDate(time.Now())},
})
}
func ExampleMessage_SetAddressHeader() {
m.SetAddressHeader("To", "bob@example.com", "Bob")
}
func ExampleMessage_SetBody() {
m.SetBody("text/plain", "Hello!")
}
func ExampleMessage_SetDateHeader() {
m.SetDateHeader("X-Date", time.Now())
}
func ExampleMessage_SetHeader() {
m.SetHeader("Subject", "Hello!")
}
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 ExampleSetCharset() {
m = gomail.NewMessage(gomail.SetCharset("ISO-8859-1"))
}
func ExampleSetEncoding() {
m = gomail.NewMessage(gomail.SetEncoding(gomail.Base64))
}

View File

@ -190,9 +190,10 @@ func (m *Message) SetBody(contentType, body string) {
}
}
// AddAlternative adds an alternative part to the message. Commonly used to
// send HTML emails that default to the plain text version for backward
// compatibility.
// AddAlternative adds an alternative part to the message.
//
// It is commonly used to send HTML emails that default to the plain text
// version for backward compatibility.
//
// More info: http://en.wikipedia.org/wiki/MIME#Alternative
func (m *Message) AddAlternative(contentType, body string) {
@ -208,7 +209,7 @@ func (m *Message) AddAlternative(contentType, body string) {
}
// AddAlternativeWriter adds an alternative part to the message. It can be
// useful with the text/template and html/template packages.
// useful with the text/template or html/template packages.
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) {
m.parts = []part{
{
@ -252,8 +253,8 @@ func SetHeader(h map[string][]string) FileSetting {
}
// SetCopyFunc is a file setting to replace the function that runs when the
// message is sent.
// It should copy the content of the file to the io.Writer.
// message is sent. It should copy the content of the file to the io.Writer.
//
// The default copy function opens the file with the given filename, and copy
// its content to the io.Writer.
func SetCopyFunc(f func(io.Writer) error) FileSetting {

View File

@ -21,6 +21,7 @@ type SendCloser interface {
}
// A SendFunc is a function that sends emails to the given adresses.
//
// The SendFunc type is an adapter to allow the use of ordinary functions as
// email senders. If f is a function with the appropriate signature, SendFunc(f)
// is a Sender object that calls f.

View File

@ -27,8 +27,10 @@ type Dialer struct {
}
// NewPlainDialer returns a Dialer. The given parameters are used to connect to
// the SMTP server via a PLAIN authentication mechanism. It fallbacks to the
// LOGIN mechanism if it is the only mechanism advertised by the server.
// the SMTP server via a PLAIN authentication mechanism.
//
// It fallbacks to the LOGIN mechanism if it is the only mechanism advertised by
// the server.
func NewPlainDialer(host string, port int, username, password string) *Dialer {
return &Dialer{
Host: host,