28 lines
644 B
Go
28 lines
644 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/smtp"
|
|
)
|
|
|
|
func sendEmail(hostname, username, password, to, from, subject, message string, port int) error {
|
|
// Set up authentication information.
|
|
auth := smtp.PlainAuth("", username, password, hostname)
|
|
|
|
// Connect to the server, authenticate, set the sender and recipient,
|
|
// and send the email all in one step.
|
|
|
|
err := smtp.SendMail(
|
|
fmt.Sprintf("%s:%d", hostname, port),
|
|
auth,
|
|
from,
|
|
[]string{to},
|
|
[]byte(fmt.Sprintf("Content-Type: text/plain; charset=UTF-8\r\nFrom: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s\r\n", from, to, subject, message)),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|