mail/smtp.go (view raw)
1package mail
2
3import (
4 "crypto/tls"
5 "os"
6 "strconv"
7 "time"
8
9 mail "github.com/xhit/go-simple-mail/v2"
10)
11
12func mailClient() (*mail.SMTPClient, error) {
13 var (
14 EMAIL_USER_SECRET = os.Getenv("EMAIL_USER_SECRET")
15 EMAIL_PASSWORD = os.Getenv("EMAIL_PASSWORD")
16 SMTP_HOST = os.Getenv("SMTP_HOST")
17 SMTP_PORT = os.Getenv("SMTP_PORT")
18 )
19 server := mail.NewSMTPClient()
20
21 // SMTP Server
22 server.Host = SMTP_HOST
23 server.Port, _ = strconv.Atoi(SMTP_PORT)
24 server.Username = EMAIL_USER_SECRET
25 server.Password = EMAIL_PASSWORD
26 server.Encryption = mail.EncryptionSTARTTLS
27
28 // Variable to keep alive connection
29 server.KeepAlive = false
30
31 // Timeout for connect to SMTP Server
32 server.ConnectTimeout = 10 * time.Second
33
34 // Timeout for send the data and wait respond
35 server.SendTimeout = 10 * time.Second
36
37 // Set TLSConfig to provide custom TLS configuration. For example,
38 // to skip TLS verification (useful for testing):
39 server.TLSConfig = &tls.Config{InsecureSkipVerify: true}
40
41 // SMTP client
42 smtpClient, err := server.Connect()
43 if err != nil {
44 return nil, err
45 }
46
47 return smtpClient, nil
48}