Skip to content

Instantly share code, notes, and snippets.

@upikoth
Created May 24, 2024 08:02
Show Gist options
  • Save upikoth/31b0200185361958a086275692ef9bfc to your computer and use it in GitHub Desktop.
Save upikoth/31b0200185361958a086275692ef9bfc to your computer and use it in GitHub Desktop.
// Использовал вот этот пример
// https://gist.github.com/jim3ma/b5c9edeac77ac92157f8f8affa290f45
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/mail"
"net/smtp"
)
// StartTLS Email Example
func main() {
from := mail.Address{"", "noreply@starter.upikoth.dev"}
to := mail.Address{"", "ikpolux@mail.ru"}
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 := "postbox.cloud.yandex.net:25"
host, _, _ := net.SplitHostPort(servername)
auth := smtp.PlainAuth(
"",
"здесь id ключа сервисного аккаунта или Access Key ID",
"здесь Secret Access Key, который прогнал через generate.py в соответствии с докой https://yandex.cloud/ru/docs/postbox/quickstart",
host,
)
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
c, err := smtp.Dial(servername)
if err != nil {
log.Panic(err)
}
c.StartTLS(tlsconfig)
// 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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment