smtp.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "html"
  6. "io/ioutil"
  7. "os"
  8. "strings"
  9. "time"
  10. "golang-fave/engine/mysqlpool"
  11. "golang-fave/engine/sqlw"
  12. "golang-fave/engine/wrapper/config"
  13. "golang-fave/utils"
  14. "github.com/vladimirok5959/golang-worker/worker"
  15. )
  16. func smtp_sender(www_dir string, mp *mysqlpool.MySqlPool) *worker.Worker {
  17. return worker.New(func(ctx context.Context, w *worker.Worker, o *[]worker.Iface) {
  18. if www_dir, ok := (*o)[0].(string); ok {
  19. if mp, ok := (*o)[1].(*mysqlpool.MySqlPool); ok {
  20. smtp_loop(ctx, www_dir, mp)
  21. }
  22. }
  23. select {
  24. case <-ctx.Done():
  25. case <-time.After(5 * time.Second):
  26. return
  27. }
  28. }, &[]worker.Iface{
  29. www_dir,
  30. mp,
  31. })
  32. }
  33. func smtp_loop(ctx context.Context, www_dir string, mp *mysqlpool.MySqlPool) {
  34. dirs, err := ioutil.ReadDir(www_dir)
  35. if err == nil {
  36. for _, dir := range dirs {
  37. select {
  38. case <-ctx.Done():
  39. return
  40. default:
  41. if mp != nil {
  42. target_dir := strings.Join([]string{www_dir, dir.Name()}, string(os.PathSeparator))
  43. if utils.IsDirExists(target_dir) {
  44. smtp_process(ctx, target_dir, dir.Name(), mp)
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. func smtp_process(ctx context.Context, dir, host string, mp *mysqlpool.MySqlPool) {
  52. db := mp.Get(host)
  53. if db != nil {
  54. conf := config.ConfigNew()
  55. if err := conf.ConfigRead(strings.Join([]string{dir, "config", "config.json"}, string(os.PathSeparator))); err == nil {
  56. if !((*conf).SMTP.Host == "" || (*conf).SMTP.Login == "" && (*conf).SMTP.Password == "") {
  57. if err := db.Ping(ctx); err == nil {
  58. smtp_prepare(ctx, db, conf)
  59. }
  60. }
  61. } else {
  62. fmt.Printf("Smtp error (config): %v\n", err)
  63. }
  64. }
  65. }
  66. func smtp_prepare(ctx context.Context, db *sqlw.DB, conf *config.Config) {
  67. rows, err := db.Query(
  68. `SELECT
  69. id,
  70. email,
  71. subject,
  72. message
  73. FROM
  74. notify_mail
  75. WHERE
  76. status = 2
  77. ORDER BY
  78. id ASC
  79. ;`,
  80. )
  81. if err == nil {
  82. defer rows.Close()
  83. values := make([]string, 4)
  84. scan := make([]interface{}, len(values))
  85. for i := range values {
  86. scan[i] = &values[i]
  87. }
  88. for rows.Next() {
  89. err = rows.Scan(scan...)
  90. if err == nil {
  91. if _, err := db.Exec(
  92. `UPDATE notify_mail SET status = 3 WHERE id = ?;`,
  93. utils.StrToInt(string(values[0])),
  94. ); err == nil {
  95. go func(db *sqlw.DB, conf *config.Config, id int, subject, msg string, receivers []string) {
  96. if err := smtp_send(
  97. ctx,
  98. (*conf).SMTP.Host,
  99. utils.IntToStr((*conf).SMTP.Port),
  100. (*conf).SMTP.Login,
  101. (*conf).SMTP.Password,
  102. subject,
  103. msg,
  104. receivers,
  105. ); err == nil {
  106. if _, err := db.Exec(
  107. `UPDATE notify_mail SET status = 1 WHERE id = ?;`,
  108. id,
  109. ); err != nil {
  110. fmt.Printf("Smtp send error (sql, success): %v\n", err)
  111. }
  112. } else {
  113. if _, err := db.Exec(
  114. `UPDATE notify_mail SET error = ?, status = 0 WHERE id = ?;`,
  115. err.Error(),
  116. id,
  117. ); err != nil {
  118. fmt.Printf("Smtp send error (sql, error): %v\n", err)
  119. }
  120. }
  121. }(
  122. db,
  123. conf,
  124. utils.StrToInt(string(values[0])),
  125. html.EscapeString(string(values[2])),
  126. string(values[3]),
  127. []string{html.EscapeString(string(values[1]))},
  128. )
  129. } else {
  130. fmt.Printf("Smtp send error (sql, update): %v\n", err)
  131. }
  132. }
  133. }
  134. }
  135. }
  136. func smtp_send(ctx context.Context, host, port, user, pass, subject, msg string, receivers []string) error {
  137. return utils.SMTPSend(host, port, user, pass, subject, msg, receivers)
  138. }