email.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package utils
  2. import (
  3. "fmt"
  4. mail "github.com/xhit/go-simple-mail/v2"
  5. "log"
  6. )
  7. var emailClient = func() *mail.SMTPServer {
  8. // 创建邮件客户端
  9. client := mail.NewSMTPClient()
  10. // 设置SMTP服务器和认证信息
  11. client.Host = "smtp.163.com" // 替换为你的SMTP服务器地址
  12. client.Port = 25 // 替换为SMTP服务器端口
  13. client.Username = "17673481176@163.com" // 替换为SMTP服务器的用户名
  14. client.Password = "kk992471" // 替换为SMTP服务器的密码
  15. return client
  16. }()
  17. func EmailPicture(To, subject, data string, pic *mail.File) {
  18. // 创建邮件客户端
  19. //client := mail.NewSMTPClient()
  20. //// 设置SMTP服务器和认证信息
  21. //client.Host = "smtp.163.com" // 替换为你的SMTP服务器地址
  22. //client.Port = 25 // 替换为SMTP服务器端口
  23. //client.Username = "17673481176@163.com" // 替换为SMTP服务器的用户名
  24. //client.Password = "kk992471" // 替换为SMTP服务器的密码
  25. smtpClient, err := emailClient.Connect()
  26. if err != nil {
  27. fmt.Println("client.Connect()", err)
  28. }
  29. // 创建邮件对象
  30. email := mail.NewMSG()
  31. // 设置发件人
  32. email.SetFrom("17673481176@163.com")
  33. // 设置收件人
  34. email.AddTo(To)
  35. // 设置主题
  36. email.SetSubject(subject)
  37. // 设置html文本内容
  38. email.SetBody(mail.TextHTML, data)
  39. // 添加图片附件
  40. if pic != nil {
  41. email.Attach(pic)
  42. }
  43. // 发送邮件
  44. err = email.Send(smtpClient)
  45. if err != nil {
  46. log.Println("116 Failed to send email:", err)
  47. return
  48. }
  49. log.Println("Email sent successfully")
  50. }
  51. //@author: [maplepie](https://github.com/maplepie)
  52. //@function: Email
  53. //@description: Email发送方法
  54. //@param: subject string, body string
  55. //@return: error
  56. func Email(To, subject string, body string) {
  57. smtpClient, err := emailClient.Connect()
  58. if err != nil {
  59. fmt.Println("client.Connect()", err)
  60. }
  61. // 创建邮件对象
  62. email := mail.NewMSG()
  63. // 设置发件人
  64. email.SetFrom("17673481176@163.com")
  65. // 设置收件人
  66. email.AddTo(To)
  67. // 设置主题
  68. email.SetSubject(subject)
  69. // 设置纯文本内容
  70. email.SetBody(mail.TextHTML, body)
  71. // 发送邮件
  72. err = email.Send(smtpClient)
  73. if err != nil {
  74. log.Println("116 Failed to send email:", err)
  75. return
  76. }
  77. log.Println("Email sent successfully")
  78. }
  79. //@author: [SliverHorn](https://github.com/SliverHorn)
  80. //@function: ErrorToEmail
  81. //@description: 给email中间件错误发送邮件到指定邮箱
  82. //@param: subject string, body string
  83. //@return: error
  84. func ErrorToEmail(subject string, body string) error {
  85. //to := strings.Split(global.GlobalConfig.To, ",")
  86. //if to[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除
  87. // to = to[:len(to)-1]
  88. //}
  89. //return send(to, subject, body)
  90. return nil
  91. }
  92. func send(to []string, subject string, body string) error {
  93. //from := global.GlobalConfig.From
  94. //nickname := global.GlobalConfig.Nickname
  95. //secret := global.GlobalConfig.Secret
  96. //host := global.GlobalConfig.Host
  97. //port := global.GlobalConfig.Port
  98. //isSSL := global.GlobalConfig.IsSSL
  99. //
  100. //auth := smtp.PlainAuth("", from, secret, host)
  101. //e := email.NewEmail()
  102. //if nickname != "" {
  103. // e.From = fmt.Sprintf("%s <%s>", nickname, from)
  104. //} else {
  105. // e.From = from
  106. //}
  107. //e.To = to
  108. //e.Subject = subject
  109. //e.HTML = []byte(body)
  110. //var err error
  111. //hostAddr := fmt.Sprintf("%s:%d", host, port)
  112. //if isSSL {
  113. // err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
  114. //} else {
  115. // err = e.Send(hostAddr, auth)
  116. //}
  117. //return err
  118. return nil
  119. }