mailsrv/main.go
2022-10-09 17:05:38 +02:00

59 lines
1.3 KiB
Go

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!<br/><br/>This is an e-mail test, please do not reply.<br/><br/>Thegux Administrator<br/>%s", `<hr/>visit the website: <a href="https://thegux.fr">thegux.fr</a>`)
email := NewEmail(SMTPUser, []string{"receiver@receiver.com"}, "e-mail test", content)
auth := smtp.PlainAuth("", SMTPUser, "<password>", 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")
}