Skip to content

Instantly share code, notes, and snippets.

@jim3ma
Forked from chrisgillis/ssl_smtp_example.go
Last active October 23, 2025 13:45
Show Gist options
  • Select an option

  • Save jim3ma/b5c9edeac77ac92157f8f8affa290f45 to your computer and use it in GitHub Desktop.

Select an option

Save jim3ma/b5c9edeac77ac92157f8f8affa290f45 to your computer and use it in GitHub Desktop.

Revisions

  1. jim3ma renamed this gist May 26, 2016. 1 changed file with 5 additions and 11 deletions.
    16 changes: 5 additions & 11 deletions ssl_smtp_example.go → starttls_smtp_example.go
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,14 @@ package main

    import (
    "fmt"
    "log"
    "log"
    "net"
    "net/mail"
    "net/smtp"
    "net/smtp"
    "crypto/tls"
    )

    // SSL/TLS Email Example
    // StartTLS Email Example

    func main() {

    @@ -44,18 +44,12 @@ func main() {
    ServerName: host,
    }

    // Here is the key, you need to call tls.Dial instead of smtp.Dial
    // for smtp servers running on 465 that require an ssl connection
    // from the very beginning (no starttls)
    conn, err := tls.Dial("tcp", servername, tlsconfig)
    c, err := smtp.Dial(servername)
    if err != nil {
    log.Panic(err)
    }

    c, err := smtp.NewClient(conn, host)
    if err != nil {
    log.Panic(err)
    }
    c.StartTLS(tlsconfig)

    // Auth
    if err = c.Auth(auth); err != nil {
  2. @chrisgillis chrisgillis renamed this gist Apr 16, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @chrisgillis chrisgillis created this gist Apr 16, 2014.
    92 changes: 92 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    package main

    import (
    "fmt"
    "log"
    "net"
    "net/mail"
    "net/smtp"
    "crypto/tls"
    )

    // SSL/TLS Email Example

    func main() {

    from := mail.Address{"", "[email protected]"}
    to := mail.Address{"", "[email protected]"}
    subj := "This is the email subject"
    body := "This is an example body.\n With two lines."

    // Setup headers
    headers := make(map[string]string)
    headers["From"] = from.String()
    headers["To"] = to.String()
    headers["Subject"] = subj

    // Setup message
    message := ""
    for k,v := range headers {
    message += fmt.Sprintf("%s: %s\r\n", k, v)
    }
    message += "\r\n" + body

    // Connect to the SMTP Server
    servername := "smtp.example.tld:465"

    host, _, _ := net.SplitHostPort(servername)

    auth := smtp.PlainAuth("","[email protected]", "password", host)

    // TLS config
    tlsconfig := &tls.Config {
    InsecureSkipVerify: true,
    ServerName: host,
    }

    // Here is the key, you need to call tls.Dial instead of smtp.Dial
    // for smtp servers running on 465 that require an ssl connection
    // from the very beginning (no starttls)
    conn, err := tls.Dial("tcp", servername, tlsconfig)
    if err != nil {
    log.Panic(err)
    }

    c, err := smtp.NewClient(conn, host)
    if err != nil {
    log.Panic(err)
    }

    // Auth
    if err = c.Auth(auth); err != nil {
    log.Panic(err)
    }

    // To && From
    if err = c.Mail(from.Address); err != nil {
    log.Panic(err)
    }

    if err = c.Rcpt(to.Address); err != nil {
    log.Panic(err)
    }

    // Data
    w, err := c.Data()
    if err != nil {
    log.Panic(err)
    }

    _, err = w.Write([]byte(message))
    if err != nil {
    log.Panic(err)
    }

    err = w.Close()
    if err != nil {
    log.Panic(err)
    }

    c.Quit()

    }