From 2bd7cf0cb31fe699c93554e1956c4c5d1de8c8f3 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 12 Mar 2022 15:19:32 +0100 Subject: [PATCH] Update README.md and removed example code in favor for doc_test.go example --- README.md | 32 +++----------------------------- doc_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 doc_test.go diff --git a/README.md b/README.md index 99fc13d..b3d402c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ mail and SMTP related tasks. ## Features Some of the features of this library: * [X] Only Standard Library dependant -* [X] Modern, idiotmatic Go +* [X] Modern, idiomatic Go * [X] Sane and secure defaults * [X] SSL/TLS support * [X] StartTLS support with different policies @@ -28,31 +28,5 @@ Some of the features of this library: * [ ] Support for attachments * [ ] Go template support -## Example -```go -package main - -import ( - "context" - "fmt" - "github.com/wneessen/go-mail" - "os" - "time" -) - -func main() { - c, err := mail.NewClient("mail.example.com", mail.WithTimeout(time.Millisecond*500), - mail.WithTLSPolicy(mail.TLSMandatory), mail.WithSMTPAuth(mail.SMTPAuthDigestMD5), - mail.WithUsername("tester@example.com"), mail.WithPassword("secureP4ssW0rd!")) - if err != nil { - fmt.Printf("failed to create new client: %s\n", err) - os.Exit(1) - } - defer c.Close() - - if err := c.DialAndSend(); err != nil { - fmt.Printf("failed to dial: %s\n", err) - os.Exit(1) - } -} -``` \ No newline at end of file +## Examples +The [package](https://pkg.go.dev/github.com/wneessen/go-mail) is shipped with example code. \ No newline at end of file diff --git a/doc_test.go b/doc_test.go new file mode 100644 index 0000000..6ee4985 --- /dev/null +++ b/doc_test.go @@ -0,0 +1,33 @@ +package mail + +import ( + "fmt" + "os" +) + +func ExampleClient_DialAndSend() { + from := "Toni Tester " + to := "Alice " + server := "mail.example.com" + + m := NewMsg() + if err := m.From(from); err != nil { + fmt.Printf("failed to set FROM address: %s", err) + os.Exit(1) + } + if err := m.To(to); err != nil { + fmt.Printf("failed to set TO address: %s", err) + os.Exit(1) + } + m.Subject("This is a great subject") + + c, err := NewClient(server) + if err != nil { + fmt.Printf("failed to create mail client: %s", err) + os.Exit(1) + } + if err := c.DialAndSend(m); err != nil { + fmt.Printf("failed to send mail: %s", err) + os.Exit(1) + } +}