Send a simple email message by native smtp client
package main
import (
"fmt"
"log"
"net/smtp"
"github.com/p-kunkel/easymail"
"github.com/p-kunkel/easymail/message"
)
func main() {
var (
sender = "[email protected]"
password = "yourPassword"
smtpHost = "smtp.host.com"
smtpPort = "587"
smtpAddress = smtpHost + ":" + smtpPort
)
mail := easymail.New()
mail.From(sender)
mail.To("[email protected]")
mail.Subject("Example of use easymail")
mail.AppendPart(message.New("It was easy!"))
auth := smtp.PlainAuth("", sender, password, smtpHost)
if err := mail.SmtpSend(smtpAddress, auth); err != nil {
log.Fatal(err.Error())
}
fmt.Println("Email Sent Successfully!")
}
Add more recipients
mail.To("[email protected]", "Receiver 2 <[email protected]>")
mail.Cc("Receiver 3 <[email protected]>", "[email protected]")
mail.Bcc("[email protected]", "[email protected]")
Get all recipients
mail.To("[email protected]")
mail.Cc("Receiver 3 <[email protected]>")
mail.Bcc("[email protected]")
r := mail.Headers.GetRecipients()
Send HTML content
html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>HTML</h1>
<h2>It was easy!</h2>
</body>
</html>
`
msg = message.New(html)
mail.AppendPart(msg)
Add an attachment
// local file as an attachment
localFile := attachment.New()
localFile.ReadFile("path/to/your/file.txt")
mail.AppendPart(localFile)
// write byte and send as an attachment
otherFile := attachment.New()
otherFile.Write([]byte("everything what you want"))
otherFile.ContentType = "text/plain"
otherFile.Filename = "text_file.txt"
mail.AppendPart(other)
Prepare raw MIME format ready to send
raw, err := mail.Raw()