commit c9bc72bc1564466088336fef8c5961a33cd03dea Author: rmanach Date: Sun Oct 9 17:05:38 2022 +0200 add a mailsrv repo diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c0d1826 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module mailsrv + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a3594aa --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "net/smtp" + "os" + "strings" +) + +const ( + SMTPServer string = "smtp.ionos.fr" + SMTPUser string = "admin@thegux.fr" + SMTPPort string = "25" +) + +type Email struct { + Sender string + Receivers []string + Subject string + Content string +} + +func NewEmail(sender string, receivers []string, subject, content string) Email { + return Email{ + Sender: sender, + Receivers: receivers, + Subject: subject, + Content: content, + } +} + +func (e Email) Generate() []byte { + mail := fmt.Sprintf( + "To: %s\nFrom: %s\nContent-Type: text/html\nSubject: %s\n\n%s", + strings.Join(e.Receivers, ","), + e.Sender, + e.Sender, + e.Content, + ) + return []byte(mail) +} + +func main() { + fmt.Println("sending Thegux email test...") + + content := fmt.Sprintf("Hi!

This is an e-mail test, please do not reply.

Thegux Administrator
%s", `
visit the website: thegux.fr`) + email := NewEmail(SMTPUser, []string{"receiver@receiver.com"}, "e-mail test", content) + + auth := smtp.PlainAuth("", SMTPUser, "", SMTPServer) + fmt.Println("authentication: ok") + + if err := smtp.SendMail(SMTPServer+":"+SMTPPort, auth, email.Sender, email.Receivers, email.Generate()); err != nil { + fmt.Printf("error while sending email, err=%v\n", err) + os.Exit(1) + } + + fmt.Println("email send successfully") +}