Added examples
This commit is contained in:
parent
1e43a4157e
commit
03dbed963f
|
@ -21,7 +21,7 @@ func Example() {
|
||||||
|
|
||||||
d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
|
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 {
|
if err := d.DialAndSend(m); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ func Example_noAuth() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send an email using an API or postfix.
|
// Send an email using an API or postfix.
|
||||||
func Example_send() {
|
func Example_noSMTP() {
|
||||||
m := gomail.NewMessage()
|
m := gomail.NewMessage()
|
||||||
m.SetHeader("From", "from@example.com")
|
m.SetHeader("From", "from@example.com")
|
||||||
m.SetHeader("To", "to@example.com")
|
m.SetHeader("To", "to@example.com")
|
||||||
|
@ -138,28 +138,16 @@ func Example_send() {
|
||||||
|
|
||||||
var m *gomail.Message
|
var m *gomail.Message
|
||||||
|
|
||||||
func ExampleSetCharset() {
|
func ExampleSetCopyFunc() {
|
||||||
m = gomail.NewMessage(gomail.SetCharset("ISO-8859-1"))
|
m.Attach("foo.txt", gomail.SetCopyFunc(func(w io.Writer) error {
|
||||||
|
_, err := w.Write([]byte("Content of foo.txt"))
|
||||||
|
return err
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleSetEncoding() {
|
func ExampleSetHeader() {
|
||||||
m = gomail.NewMessage(gomail.SetEncoding(gomail.Base64))
|
h := map[string][]string{"Content-ID": {"<foo@bar.mail>"}}
|
||||||
}
|
m.Attach("foo.jpg", gomail.SetHeader(h))
|
||||||
|
|
||||||
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() {
|
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() {
|
func ExampleMessage_Attach() {
|
||||||
m.Attach("/tmp/image.jpg")
|
m.Attach("/tmp/image.jpg")
|
||||||
}
|
}
|
||||||
|
@ -194,3 +170,45 @@ func ExampleMessage_Embed() {
|
||||||
m.Embed("/tmp/image.jpg")
|
m.Embed("/tmp/image.jpg")
|
||||||
m.SetBody("text/html", `<img src="cid:image.jpg" alt="My image" />`)
|
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))
|
||||||
|
}
|
||||||
|
|
13
message.go
13
message.go
|
@ -190,9 +190,10 @@ func (m *Message) SetBody(contentType, body string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAlternative adds an alternative part to the message. Commonly used to
|
// AddAlternative adds an alternative part to the message.
|
||||||
// send HTML emails that default to the plain text version for backward
|
//
|
||||||
// compatibility.
|
// 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
|
// More info: http://en.wikipedia.org/wiki/MIME#Alternative
|
||||||
func (m *Message) AddAlternative(contentType, body string) {
|
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
|
// 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) {
|
func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error) {
|
||||||
m.parts = []part{
|
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
|
// SetCopyFunc is a file setting to replace the function that runs when the
|
||||||
// message is sent.
|
// message is sent. It should copy the content of the file to the io.Writer.
|
||||||
// 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
|
// The default copy function opens the file with the given filename, and copy
|
||||||
// its content to the io.Writer.
|
// its content to the io.Writer.
|
||||||
func SetCopyFunc(f func(io.Writer) error) FileSetting {
|
func SetCopyFunc(f func(io.Writer) error) FileSetting {
|
||||||
|
|
1
send.go
1
send.go
|
@ -21,6 +21,7 @@ type SendCloser interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// A SendFunc is a function that sends emails to the given adresses.
|
// 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
|
// 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)
|
// email senders. If f is a function with the appropriate signature, SendFunc(f)
|
||||||
// is a Sender object that calls f.
|
// is a Sender object that calls f.
|
||||||
|
|
6
smtp.go
6
smtp.go
|
@ -27,8 +27,10 @@ type Dialer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPlainDialer returns a Dialer. The given parameters are used to connect to
|
// NewPlainDialer returns a Dialer. The given parameters are used to connect to
|
||||||
// the SMTP server via a PLAIN authentication mechanism. It fallbacks to the
|
// the SMTP server via a PLAIN authentication mechanism.
|
||||||
// LOGIN mechanism if it is the only mechanism advertised by the server.
|
//
|
||||||
|
// 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 {
|
func NewPlainDialer(host string, port int, username, password string) *Dialer {
|
||||||
return &Dialer{
|
return &Dialer{
|
||||||
Host: host,
|
Host: host,
|
||||||
|
|
Loading…
Reference in New Issue