Skip to content

Commit d10dc92

Browse files
author
Winni Neessen
authored
Merge pull request #16 from wneessen/14-exampledocs
#14: First effort of adding more example code
2 parents 97e1386 + 345ad6c commit d10dc92

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

doc_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package mail_test
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/wneessen/go-mail"
67
"os"
8+
"text/template"
9+
"time"
710
)
811

912
// Code example for the NewClient method
@@ -61,3 +64,73 @@ func ExampleClient_DialAndSend() {
6164
os.Exit(1)
6265
}
6366
}
67+
68+
// This code example shows how to use Msg.SetBodyString to set a string as message body with
69+
// different content types
70+
func ExampleMsg_SetBodyString_differentTypes() {
71+
m := mail.NewMsg()
72+
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain")
73+
m.SetBodyString(mail.TypeTextHTML, "<p>This is a mail body that with content type: text/html</p>")
74+
}
75+
76+
// This code example shows how to use Msg.SetBodyString to set a string as message body a PartOption
77+
// to override the default encoding
78+
func ExampleMsg_SetBodyString_withPartOption() {
79+
m := mail.NewMsg(mail.WithEncoding(mail.EncodingB64))
80+
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain",
81+
mail.WithPartEncoding(mail.EncodingQP))
82+
}
83+
84+
// This code example shows how to use a text/template as message Body.
85+
// Msg.SetBodyHTMLTemplate works anolog to this just with html/template instead
86+
func ExampleMsg_SetBodyTextTemplate() {
87+
type MyStruct struct {
88+
Placeholder string
89+
}
90+
data := MyStruct{Placeholder: "Teststring"}
91+
tpl, err := template.New("test").Parse("This is a {{.Placeholder}}")
92+
if err != nil {
93+
panic(err)
94+
}
95+
96+
m := mail.NewMsg()
97+
if err := m.SetBodyTextTemplate(tpl, data); err != nil {
98+
panic(err)
99+
}
100+
}
101+
102+
// This code example shows how to utilize the Msg.WriteToSendmail method to send generated mails
103+
// using a local sendmail installation
104+
func ExampleMsg_WriteToSendmail() {
105+
m := mail.NewMsg()
106+
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
107+
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
108+
panic(err)
109+
}
110+
if err := m.To("gandalf.tester@example.com"); err != nil {
111+
panic(err)
112+
}
113+
if err := m.WriteToSendmail(); err != nil {
114+
panic(err)
115+
}
116+
}
117+
118+
// This code example shows how to send generated mails using a custom context and sendmail-compatbile command
119+
// using the Msg.WriteToSendmailWithContext method
120+
func ExampleMsg_WriteToSendmailWithContext() {
121+
sendmailPath := "/opt/sendmail/sbin/sendmail"
122+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
123+
defer cancel()
124+
125+
m := mail.NewMsg()
126+
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
127+
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
128+
panic(err)
129+
}
130+
if err := m.To("gandalf.tester@example.com"); err != nil {
131+
panic(err)
132+
}
133+
if err := m.WriteToSendmailWithContext(ctx, sendmailPath); err != nil {
134+
panic(err)
135+
}
136+
}

0 commit comments

Comments
 (0)