Sending Bulk Email #73
-
Hi, I'm planning to use m := mail.NewMsg()
for _, v := range mail {
if err := m.FromFormat("Toni Tester", "[email protected]"); err != nil {
fmt.Printf("failed to set FROM address: %s\n", err)
os.Exit(1)
}
if err := m.To(`"v.Name <v.Email>"`); err != nil {
fmt.Printf("failed to set TO address: %s\n", err)
os.Exit(1)
}
m.CcIgnoreInvalid("[email protected]", "invalidaddress+example.com")
m.Subject("This is a great email")
m.SetDate()
m.SetMessageID()
m.SetBulk()
m.SetImportance(mail.ImportanceHigh)
m.SetBodyString(mail.TypeTextPlain, "This is a great message body text.")
m.AttachFile("/home/ttester/test.txt", mail.WithFileName("attachment.txt"))
host := "relay.example.com"
c, err := mail.NewClient(host,
mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername("ttester"),
mail.WithPassword("V3rySecUr3!Pw."), mail.WithTLSPolicy(mail.TLSMandatory))
if err != nil {
fmt.Printf("failed to create new mail client: %s\n", err)
os.Exit(1)
}
if err := c.DialAndSend(m); err != nil {
fmt.Printf("failed to send mail: %s\n", err)
os.Exit(1)
}
fmt.Println("Mail successfully sent.")
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @ljfreelancer88, that's actually a good point. We currently don't have a proper bulk-mail example. I'll add one to the documentation in the next days. For now, a few changes I would suggest for your code:
I've composed an example code that should fit your needs better below. Hope this helps! package main
import (
"fmt"
"github.com/wneessen/go-mail"
"os"
)
func main() {
type user struct {
Name string
Email string
}
userList := []user{
{"Toni Tester", "[email protected]"},
{"Tina Tester", "[email protected]"},
{"John Doe", "[email protected]"},
}
var mailList []*mail.Msg
for _, v := range userList {
m := mail.NewMsg()
if err := m.FromFormat("Stephen Sender", "[email protected]"); err != nil {
fmt.Printf("failed to set FROM address: %s\n", err)
os.Exit(1)
}
if err := m.AddToFormat(v.Name, v.Email); err != nil {
fmt.Printf("failed to set TO address: %s\n", err)
os.Exit(1)
}
m.Subject("This is a great email")
m.SetDate()
m.SetMessageID()
m.SetBodyString(mail.TypeTextPlain, "This is a great message body text.")
mailList = append(mailList, m)
}
host := "relay.example.com"
c, err := mail.NewClient(host,
mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername("ttester"),
mail.WithPassword("V3rySecUr3!Pw."), mail.WithTLSPolicy(mail.TLSMandatory))
if err != nil {
fmt.Printf("failed to create new mail client: %s\n", err)
os.Exit(1)
}
if err := c.DialAndSend(mailList...); err != nil {
fmt.Printf("failed to send mail: %s\n", err)
os.Exit(1)
}
fmt.Printf("%d mails successfully sent.", len(mailList))
} |
Beta Was this translation helpful? Give feedback.
Hi @ljfreelancer88,
that's actually a good point. We currently don't have a proper bulk-mail example. I'll add one to the documentation in the next days. For now, a few changes I would suggest for your code:
m.To(
"v.Name <v.Email>");
you can usem.AddToFormat(v.Name, v.Email)
which will take care of the proper formating for you.*mail.Msg
in thec.DialAndSend()
call. Therefore I suggest to create an empty mail message slice[]*mail.Msg
and only append the prepared mails into that in the for loop and eventually provide the message slice t…