Initial commit
This commit is contained in:
parent
39396ac0bf
commit
c01f050e15
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/mail"
|
||||
"net/smtp"
|
||||
"os"
|
||||
)
|
||||
|
||||
// "Send As" information
|
||||
|
||||
func main() {
|
||||
var (
|
||||
username = os.Getenv("PLUGIN_USER")
|
||||
password = os.Getenv("PLUGIN_PASSWORD")
|
||||
to = os.Getenv("PLUGIN_TO")
|
||||
subject = os.Getenv("PLUGIN_SUBJECT")
|
||||
body = os.Getenv("PLUGIN_TEXT")
|
||||
from_addr = os.Getenv("PLUGIN_FROM")
|
||||
from_name = os.Getenv("PLUGIN_FROM_NAME")
|
||||
)
|
||||
|
||||
const gmail = "smtp.gmail.com:587"
|
||||
|
||||
sanitizedBody := template.HTMLEscapeString(body)
|
||||
|
||||
fmt.Println("Authenticateing...")
|
||||
auth := smtp.PlainAuth("", username, password, "smtp.gmail.com")
|
||||
smtpConnection, err := smtp.Dial(gmail)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := smtpConnection.StartTLS(&tls.Config{ServerName: "smtp.gmail.com"}); err != nil {
|
||||
|
||||
panic(err)
|
||||
}
|
||||
if err := smtpConnection.Auth(auth); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
from := mail.Address{Name: from_name, Address: from_addr}
|
||||
rcpt := mail.Address{Name: "", Address: to}
|
||||
|
||||
header := make(map[string]string)
|
||||
header["From"] = from.String()
|
||||
header["To"] = rcpt.String()
|
||||
header["Subject"] = subject
|
||||
|
||||
// Convert the header map to a string and attach the body to it
|
||||
var message string
|
||||
for key, value := range header {
|
||||
message += fmt.Sprintf("%s: %s\r\n", key, value)
|
||||
}
|
||||
message += "\r\n" + sanitizedBody + "\r\n"
|
||||
|
||||
fmt.Println("Senting an email to user")
|
||||
err = smtp.SendMail(gmail, auth, from_addr, []string{rcpt.Address}, []byte(message))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
smtpConnection.Quit()
|
||||
}
|
Loading…
Reference in New Issue