Last active
January 17, 2025 15:17
-
-
Save jniltinho/d90034994f29d7d25e59c9e0fe5548d2 to your computer and use it in GitHub Desktop.
Postfix and Golang - Sending emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/mail" | |
"net/smtp" | |
"strings" | |
) | |
var host = "127.0.0.1" | |
var port = "25" | |
var username = "" | |
var password = "" | |
var auth = smtp.PlainAuth("", username, password, host) | |
var addr = host + ":" + port | |
func main() { | |
fromName := "Fred" | |
fromEmail := "[email protected]" | |
toNames := []string{"Ted"} | |
toEmails := []string{"[email protected]"} | |
subject := "This is the subject of your email" | |
body := "This is the body of your email" | |
// Build RFC-2822 email | |
toAddresses := []string{} | |
for i, _ := range toEmails { | |
to := mail.Address{toNames[i], toEmails[i]} | |
toAddresses = append(toAddresses, to.String()) | |
} | |
toHeader := strings.Join(toAddresses, ", ") | |
from := mail.Address{fromName, fromEmail} | |
fromHeader := from.String() | |
subjectHeader := subject | |
header := make(map[string]string) | |
header["To"] = toHeader | |
header["From"] = fromHeader | |
header["Subject"] = subjectHeader | |
header["Content-Type"] = `text/html; charset="UTF-8"` | |
msg := "" | |
for k, v := range header { | |
msg += fmt.Sprintf("%s: %s\r\n", k, v) | |
} | |
msg += "\r\n" + body | |
bMsg := []byte(msg) | |
// Send using local postfix service | |
c, err := smtp.Dial(addr) | |
if err != nil { | |
return | |
} | |
defer c.Close() | |
if err = c.Mail(fromHeader); err != nil { | |
return | |
} | |
for _, addr := range toEmails { | |
if err = c.Rcpt(addr); err != nil { | |
return | |
} | |
} | |
w, err := c.Data() | |
if err != nil { | |
return | |
} | |
_, err = w.Write(bMsg) | |
if err != nil { | |
return | |
} | |
err = w.Close() | |
if err != nil { | |
return | |
} | |
err = c.Quit() | |
// Or alternatively, send with remote service like Amazon SES | |
// err = smtp.SendMail(addr, auth, fromEmail, toEmails, bMsg) | |
// Handle response from local postfix or remote service | |
if err != nil { | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Been looking for this, thanks a lot