| 1 |
|
| 2 |
|
| 3 |
package mailyak |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"crypto/tls" |
| 8 |
"io" |
| 9 |
"net/smtp" |
| 10 |
"text/template" |
| 11 |
) |
| 12 |
|
| 13 |
func Example() { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 19 |
|
| 20 |
mail.To("[email protected]") |
| 21 |
mail.From("[email protected]") |
| 22 |
mail.FromName("Prince Anybody") |
| 23 |
|
| 24 |
mail.Subject("Business proposition") |
| 25 |
|
| 26 |
|
| 27 |
mail.AddHeader("X-TOTALLY-NOT-A-SCAM", "true") |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
if _, err := io.WriteString(mail.HTML(), "So long, and thanks for all the fish."); err != nil { |
| 33 |
panic(" :( ") |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
mail.Plain().Set("Get a real email client") |
| 38 |
|
| 39 |
|
| 40 |
if err := mail.Send(); err != nil { |
| 41 |
panic(" :( ") |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func Example_attachments() { |
| 46 |
|
| 47 |
buf := &bytes.Buffer{} |
| 48 |
_, _ = io.WriteString(buf, "We're in the stickiest situation since Sticky the Stick Insect got stuck on a sticky bun.") |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 53 |
|
| 54 |
mail.To("[email protected]") |
| 55 |
mail.From("[email protected]") |
| 56 |
mail.HTML().Set("I am an email") |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
mail.Attach("sticky.txt", buf) |
| 61 |
|
| 62 |
if err := mail.Send(); err != nil { |
| 63 |
panic(" :( ") |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func ExampleBodyPart_string() { |
| 68 |
|
| 69 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 70 |
|
| 71 |
|
| 72 |
mail.Plain().Set("Get a real email client") |
| 73 |
} |
| 74 |
|
| 75 |
func ExampleNewWithTLS() { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
mail, err := NewWithTLS("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"), nil) |
| 83 |
if err != nil { |
| 84 |
panic("failed to initialise a TLS instance :(") |
| 85 |
} |
| 86 |
|
| 87 |
mail.Plain().Set("Have some encrypted goodness") |
| 88 |
if err := mail.Send(); err != nil { |
| 89 |
panic(" :( ") |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func ExampleNewWithTLS_with_config() { |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
mail, err := NewWithTLS( |
| 100 |
"mail.host.com:25", |
| 101 |
smtp.PlainAuth("", "user", "pass", "mail.host.com"), |
| 102 |
&tls.Config{ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
ServerName: "mail.host.com", |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
MinVersion: tls.VersionTLS12, |
| 113 |
}, |
| 114 |
) |
| 115 |
if err != nil { |
| 116 |
panic("failed to initialise a TLS instance :(") |
| 117 |
} |
| 118 |
|
| 119 |
mail.Plain().Set("Have some encrypted goodness") |
| 120 |
if err := mail.Send(); err != nil { |
| 121 |
panic(" :( ") |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func ExampleBodyPart_templates() { |
| 126 |
|
| 127 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 128 |
|
| 129 |
|
| 130 |
tmplData := struct { |
| 131 |
Language string |
| 132 |
}{"Go"} |
| 133 |
|
| 134 |
|
| 135 |
tmpl, err := template.New("html").Parse("I am an email template in {{ .Language }}") |
| 136 |
if err != nil { |
| 137 |
panic(" :( ") |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if err := tmpl.Execute(mail.HTML(), tmplData); err != nil { |
| 142 |
panic(" :( ") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func ExampleMailYak_AttachInline() { |
| 147 |
|
| 148 |
mail := New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 149 |
mail.To("[email protected]") |
| 150 |
mail.From("[email protected]") |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
imageBuffer := &bytes.Buffer{} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
mail.AttachInline("myimage", imageBuffer) |
| 162 |
|
| 163 |
|
| 164 |
mail.HTML().Set(` |
| 165 |
<html> |
| 166 |
<body> |
| 167 |
<img src="cid:myimage"/> |
| 168 |
</body> |
| 169 |
</html> |
| 170 |
`) |
| 171 |
|
| 172 |
|
| 173 |
if err := mail.Send(); err != nil { |
| 174 |
panic(" :( ") |
| 175 |
} |
| 176 |
} |