@@ -66,40 +66,58 @@ MAIL_FROM=from@example.com`}
66
66
<Highlight className = " go" >
67
67
{ ` package main
68
68
69
- import(
69
+ import (
70
70
"fmt"
71
- "strconv"
72
- "os"
73
- "github.com/go-gomail/gomail"
74
- "github.com/joho/godotenv"
71
+ "gopkg.in/gomail.v2"
72
+ "crypto/tls"
73
+ "os"
74
+ "strconv"
75
+ "github.com/joho/godotenv"
75
76
)
76
77
77
78
func main() {
79
+
80
+ // Load environment variables from .env file
78
81
err := godotenv.Load(".env")
79
82
if err != nil {
80
- fmt.Println(err)
83
+ fmt.Println("Error loading .env file:", err)
84
+ return
81
85
}
82
86
87
+ // Convert MAIL_PORT to int
83
88
mailPort, err := strconv.Atoi(os.Getenv("MAIL_PORT"))
84
89
if err != nil {
85
90
fmt.Println("Error converting MAIL_PORT to int:", err)
86
91
return
87
92
}
93
+
94
+ // Create a new email message
88
95
m := gomail.NewMessage()
89
- m.SetHeader("From", os.Getenv("MAIL_FROM"))
90
- m.SetHeader("To", "email@email.email")
96
+ m.SetHeader("From", os.Getenv("MAIL_FROM"))
97
+ m.SetHeader("To", "email@email.email") // Replace with actual recipient
91
98
m.SetHeader("Subject", "This is a TEST")
92
99
body := "this is really a test"
93
100
m.SetBody("text/plain", body)
94
101
102
+ // Create a new dialer
95
103
d := gomail.NewDialer(os.Getenv("MAIL_HOST"), mailPort, os.Getenv("MAIL_USERNAME"), os.Getenv("MAIL_PASSWORD"))
104
+ d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
96
105
97
- if err := d.DialAndSend(m); err != nil {
98
- fmt.Println("Error sending Test email:", err)
99
- }
100
- } ` }
106
+ // Dial and send the email
107
+ if err := d.DialAndSend(m); err != nil {
108
+ fmt.Println("Error sending email:", err)
109
+ } else {
110
+ fmt.Println("Email sent successfully")
111
+ }
112
+ }
113
+ ` }
101
114
</Highlight >
102
115
</div >
103
116
<div className = " h-2" />
117
+ <Alert variant = ' info' >
118
+ <p >
119
+ با تنظیم <Important >d.TLSConfig</Important >، میتوانید بهصورت امن اقدام به ارسال ایمیلهای تراکنشی کنید.
120
+ </p >
121
+ </Alert >
104
122
105
123
</Layout >
0 commit comments