| 1 | // This package provides a easy to use MIME email composer with support for |
| 2 | // attachments. |
| 3 | package mailyakscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ |
| 4 | |
| 5 | import ( |
| 6 | "bytesscip-go gomod github.com/golang/go/src go1.12 bytes/" |
| 7 | "crypto/tlsscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/" |
| 8 | "ioscip-go gomod github.com/golang/go/src go1.12 io/" |
| 9 | "net/smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/" |
| 10 | "text/templatescip-go gomod github.com/golang/go/src go1.12 `text/template`/" |
| 11 | ) |
| 12 | |
| 13 | func Examplescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/Example().() { |
| 14 | // Create a new email - specify the SMTP host:port and auth (or nil if not |
| 15 | // needed). |
| 16 | // |
| 17 | // If you want to connect using TLS, use NewWithTLS() instead. |
| 18 | mail := Newscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/New().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com")) |
| 19 | |
| 20 | mail.Toscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#To().("[email protected]") |
| 21 | mail.Fromscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#From().("[email protected]") |
| 22 | mail.FromNamescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#FromName().("Prince Anybody") |
| 23 | |
| 24 | mail.Subjectscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Subject().("Business proposition") |
| 25 | |
| 26 | // Add a custom header |
| 27 | mail.AddHeaderscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#AddHeader().("X-TOTALLY-NOT-A-SCAM", "true") |
| 28 | |
| 29 | // mail.HTMLWriter() and mail.PlainWriter() implement io.Writer, so you can |
| 30 | // do handy things like parse a template directly into the email body - here |
| 31 | // we just use io.WriteString() |
| 32 | if _, err := ioscip-go gomod github.com/golang/go/src go1.12 io/.WriteStringscip-go gomod github.com/golang/go/src go1.12 io/WriteString().(mail.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().(), "So long, and thanks for all the fish."); err != nil { |
| 33 | panic(" :( ") |
| 34 | } |
| 35 | |
| 36 | // Or set the body using a string helper |
| 37 | mail.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().("Get a real email client") |
| 38 | |
| 39 | // And you're done! |
| 40 | if err := mail.Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Send().(); err != nil { |
| 41 | panic(" :( ") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func Example_attachmentsscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/Example_attachments().() { |
| 46 | // This will be our attachment data |
| 47 | buf := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{} |
| 48 | _, _ = ioscip-go gomod github.com/golang/go/src go1.12 io/.WriteStringscip-go gomod github.com/golang/go/src go1.12 io/WriteString().(buf, "We're in the stickiest situation since Sticky the Stick Insect got stuck on a sticky bun.") |
| 49 | |
| 50 | // Create a new email - specify the SMTP host:port and auth (or nil if not |
| 51 | // needed). |
| 52 | mail := Newscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/New().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com")) |
| 53 | |
| 54 | mail.Toscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#To().("[email protected]") |
| 55 | mail.Fromscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#From().("[email protected]") |
| 56 | mail.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().("I am an email") |
| 57 | |
| 58 | // buf could be anything that implements io.Reader, like a file on disk or |
| 59 | // an in-memory buffer. |
| 60 | mail.Attachscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Attach().("sticky.txt", buf) |
| 61 | |
| 62 | if err := mail.Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Send().(); err != nil { |
| 63 | panic(" :( ") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func ExampleBodyPart_stringscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ExampleBodyPart_string().() { |
| 68 | // Create a new email - specify the SMTP host and auth |
| 69 | mail := Newscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/New().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com")) |
| 70 | |
| 71 | // Set the plain text email content using a string |
| 72 | mail.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().("Get a real email client") |
| 73 | } |
| 74 | |
| 75 | func ExampleNewWithTLSscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ExampleNewWithTLS().() { |
| 76 | // Create a new MailYak instance that uses an explicit TLS connection. This |
| 77 | // ensures no communication is performed in plain-text. |
| 78 | // |
| 79 | // Specify the SMTP host:port to connect to, the authentication credentials |
| 80 | // (or nil if not needed), and use an automatically generated TLS |
| 81 | // configuration by passing nil as the tls.Config argument. |
| 82 | mail, err := NewWithTLSscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/NewWithTLS().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com"), nil) |
| 83 | if err != nil { |
| 84 | panic("failed to initialise a TLS instance :(") |
| 85 | } |
| 86 | |
| 87 | mail.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().("Have some encrypted goodness") |
| 88 | if err := mail.Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Send().(); err != nil { |
| 89 | panic(" :( ") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func ExampleNewWithTLS_with_configscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ExampleNewWithTLS_with_config().() { |
| 94 | // Create a new MailYak instance that uses an explicit TLS connection. This |
| 95 | // ensures no communication is performed in plain-text. |
| 96 | // |
| 97 | // Specify the SMTP host:port to connect to, the authentication credentials |
| 98 | // (or nil if not needed), and use the tls.Config provided. |
| 99 | mail, err := NewWithTLSscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/NewWithTLS().( |
| 100 | "mail.host.com:25", |
| 101 | smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com"), |
| 102 | &tlsscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/.Configscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/Config#{ |
| 103 | // ServerName is used to verify the hostname on the returned |
| 104 | // certificates unless InsecureSkipVerify is given. It is also included |
| 105 | // in the client's handshake to support virtual hosting unless it is |
| 106 | // an IP address. |
| 107 | ServerNamescip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/Config#ServerName.: "mail.host.com", |
| 108 | |
| 109 | // Negotiate a connection that uses at least TLS v1.2, or refuse the |
| 110 | // connection if the server does not support it. Most do, and it is |
| 111 | // a very good idea to enforce it! |
| 112 | MinVersionscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/Config#MinVersion.: tlsscip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/.VersionTLS12scip-go gomod github.com/golang/go/src go1.12 `crypto/tls`/VersionTLS12., |
| 113 | }, |
| 114 | ) |
| 115 | if err != nil { |
| 116 | panic("failed to initialise a TLS instance :(") |
| 117 | } |
| 118 | |
| 119 | mail.Plainscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Plain().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().("Have some encrypted goodness") |
| 120 | if err := mail.Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Send().(); err != nil { |
| 121 | panic(" :( ") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func ExampleBodyPart_templatesscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ExampleBodyPart_templates().() { |
| 126 | // Create a new email |
| 127 | mail := Newscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/New().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com")) |
| 128 | |
| 129 | // Our pretend template data |
| 130 | tmplData := struct { |
| 131 | Language string |
| 132 | }{"Go"} |
| 133 | |
| 134 | // Compile a template |
| 135 | tmpl, err := templatescip-go gomod github.com/golang/go/src go1.12 `text/template`/.Newscip-go gomod github.com/golang/go/src go1.12 `text/template`/New().("html").Parsescip-go gomod github.com/golang/go/src go1.12 `text/template`/Template#Parse().("I am an email template in {{ .Language }}") |
| 136 | if err != nil { |
| 137 | panic(" :( ") |
| 138 | } |
| 139 | |
| 140 | // Execute the template directly into the email body |
| 141 | if err := tmpl.Executescip-go gomod github.com/golang/go/src go1.12 `text/template`/Template#Execute().(mail.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().(), tmplData); err != nil { |
| 142 | panic(" :( ") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func ExampleMailYak_AttachInlinescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/ExampleMailYak_AttachInline().() { |
| 147 | // Create a new email |
| 148 | mail := Newscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/New().("mail.host.com:25", smtpscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/.PlainAuthscip-go gomod github.com/golang/go/src go1.12 `net/smtp`/PlainAuth().("", "user", "pass", "mail.host.com")) |
| 149 | mail.Toscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#To().("[email protected]") |
| 150 | mail.Fromscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#From().("[email protected]") |
| 151 | |
| 152 | // Initialise an io.Reader that contains your image (typically read from |
| 153 | // disk, or embedded in memory). |
| 154 | // |
| 155 | // Here we use an empty buffer as a mock. |
| 156 | imageBuffer := &bytesscip-go gomod github.com/golang/go/src go1.12 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.12 bytes/Buffer#{} |
| 157 | |
| 158 | // Add the image as an attachment. |
| 159 | // |
| 160 | // To reference it, use the name as the cid value. |
| 161 | mail.AttachInlinescip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#AttachInline().("myimage", imageBuffer) |
| 162 | |
| 163 | // Set the HTML body, which includes the inline CID reference. |
| 164 | mail.HTMLscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#HTML().().Setscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/BodyPart#Set().(` |
| 165 | <html> |
| 166 | <body> |
| 167 | <img src="cid:myimage"/> |
| 168 | </body> |
| 169 | </html> |
| 170 | `) |
| 171 | |
| 172 | // Send it! |
| 173 | if err := mail.Sendscip-go gomod github.com/domodwyer/mailyak/v3 . `github.com/domodwyer/mailyak/v3`/MailYak#Send().(); err != nil { |
| 174 | panic(" :( ") |
| 175 | } |
| 176 | } |