Skip to content

Commit

Permalink
chore: modify comment lang (by copilot space)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program authored Nov 27, 2024
1 parent c85898e commit 5986f49
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions utils/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,44 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error {
return fmt.Errorf("smtp not configured properly")
}

// 创建 gomail 消息对象
// Create gomail message object
message := gomail.NewMessage()

// 根据用户名是否包含"@"来决定发件人地址
// Determine sender address based on whether the username contains "@"
var from string
if strings.Contains(s.Username, "@") {
// 如果用户名包含"@", 则直接使用 From 作为发件人
// If the username contains "@", use From as the sender
from = s.From
} else {
// 否则,将用户名和 From 组合成发件人的邮箱地址
// Otherwise, combine the username and From to form the sender's email address
from = fmt.Sprintf("%s <%s>", s.Username, s.From)
}
message.SetHeader("From", from)
message.SetHeader("To", to)
message.SetHeader("Subject", subject)
message.SetBody("text/html", body)

// 创建 gomail 拨号器
dialer := gomail.NewDialer(s.Host, s.Port, s.Username, s.Password)

// 如果启用TLS协议
// If TLS protocol is enabled
if s.Protocol {
dialer.TLSConfig = &tls.Config{
InsecureSkipVerify: false, // 禁用不安全的证书验证
ServerName: s.Host, // 设置ServerName为SMTP主机
InsecureSkipVerify: false, // Disable insecure certificate verification
ServerName: s.Host, // Set ServerName to the SMTP host
}
} else {
// 启用SSL时,不需要STARTTLS,直接进行加密连接
// When SSL is enabled, no need for STARTTLS, directly establish an encrypted connection
dialer.SSL = true
}

// 针对Outlook的STARTTLS策略适配器
// outlook starttls policy
if strings.Contains(s.Host, "outlook") {
dialer.TLSConfig = &tls.Config{
InsecureSkipVerify: false,
ServerName: s.Host,
}
}

// 拨号并发送邮件
if err := dialer.DialAndSend(message); err != nil {
return fmt.Errorf("sent mail failed: %s", err.Error())
}
Expand Down

0 comments on commit 5986f49

Please sign in to comment.