|
1 | 1 | package mail_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "github.com/wneessen/go-mail" |
6 | 7 | "os" |
| 8 | + "text/template" |
| 9 | + "time" |
7 | 10 | ) |
8 | 11 |
|
9 | 12 | // Code example for the NewClient method |
@@ -61,3 +64,73 @@ func ExampleClient_DialAndSend() { |
61 | 64 | os.Exit(1) |
62 | 65 | } |
63 | 66 | } |
| 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