-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
doc_test.go
141 lines (125 loc) · 3.92 KB
/
doc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package mail_test
import (
"context"
"fmt"
"os"
"text/template"
"time"
"github.com/wneessen/go-mail"
)
// Code example for the NewClient method
func ExampleNewClient() {
c, err := mail.NewClient("mail.example.com")
if err != nil {
panic(err)
}
_ = c
// Output:
}
// Code example for the Client.SetTLSPolicy method
func ExampleClient_SetTLSPolicy() {
c, err := mail.NewClient("mail.example.com")
if err != nil {
panic(err)
}
c.SetTLSPolicy(mail.TLSMandatory)
fmt.Println(c.TLSPolicy())
// Output: TLSMandatory
}
// Code example for the NewMsg method
func ExampleNewMsg() {
m := mail.NewMsg(mail.WithEncoding(mail.EncodingQP), mail.WithCharset(mail.CharsetASCII))
fmt.Printf("%s // %s\n", m.Encoding(), m.Charset())
// Output: quoted-printable // US-ASCII
}
// Code example for the Client.DialAndSend method
func ExampleClient_DialAndSend() {
from := "Toni Tester <toni@example.com>"
to := "Alice <alice@example.com>"
server := "mail.example.com"
m := mail.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 := mail.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)
}
}
// This code example shows how to use Msg.SetBodyString to set a string as message body with
// different content types
func ExampleMsg_SetBodyString_differentTypes() {
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain")
m.SetBodyString(mail.TypeTextHTML, "<p>This is a mail body that with content type: text/html</p>")
}
// This code example shows how to use Msg.SetBodyString to set a string as message body a PartOption
// to override the default encoding
func ExampleMsg_SetBodyString_withPartOption() {
m := mail.NewMsg(mail.WithEncoding(mail.EncodingB64))
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain",
mail.WithPartEncoding(mail.EncodingQP))
}
// This code example shows how to use a text/template as message Body.
// Msg.SetBodyHTMLTemplate works anolog to this just with html/template instead
func ExampleMsg_SetBodyTextTemplate() {
type MyStruct struct {
Placeholder string
}
data := MyStruct{Placeholder: "Teststring"}
tpl, err := template.New("test").Parse("This is a {{.Placeholder}}")
if err != nil {
panic(err)
}
m := mail.NewMsg()
if err := m.SetBodyTextTemplate(tpl, data); err != nil {
panic(err)
}
}
// This code example shows how to utilize the Msg.WriteToSendmail method to send generated mails
// using a local sendmail installation
func ExampleMsg_WriteToSendmail() {
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
panic(err)
}
if err := m.To("gandalf.tester@example.com"); err != nil {
panic(err)
}
if err := m.WriteToSendmail(); err != nil {
panic(err)
}
}
// This code example shows how to send generated mails using a custom context and sendmail-compatbile command
// using the Msg.WriteToSendmailWithContext method
func ExampleMsg_WriteToSendmailWithContext() {
sendmailPath := "/opt/sendmail/sbin/sendmail"
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
panic(err)
}
if err := m.To("gandalf.tester@example.com"); err != nil {
panic(err)
}
if err := m.WriteToSendmailWithContext(ctx, sendmailPath); err != nil {
panic(err)
}
}