smtp.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package workers
  2. import (
  3. "context"
  4. "fmt"
  5. "html"
  6. "io/ioutil"
  7. "os"
  8. "strings"
  9. "time"
  10. "golang-fave/engine/config"
  11. "golang-fave/engine/mysqlpool"
  12. "golang-fave/engine/sqlw"
  13. "golang-fave/engine/utils"
  14. "github.com/vladimirok5959/golang-worker/worker"
  15. )
  16. func SmtpSender(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. ctx,
  69. `SELECT
  70. id,
  71. email,
  72. subject,
  73. message
  74. FROM
  75. fave_notify_mail
  76. WHERE
  77. status = 2
  78. ORDER BY
  79. id ASC
  80. ;`,
  81. )
  82. if err == nil {
  83. defer rows.Close()
  84. values := make([]string, 4)
  85. scan := make([]interface{}, len(values))
  86. for i := range values {
  87. scan[i] = &values[i]
  88. }
  89. for rows.Next() {
  90. err = rows.Scan(scan...)
  91. if err == nil {
  92. if _, err := db.Exec(
  93. ctx,
  94. `UPDATE fave_notify_mail SET status = 3 WHERE id = ?;`,
  95. utils.StrToInt(string(values[0])),
  96. ); err == nil {
  97. go func(db *sqlw.DB, conf *config.Config, id int, subject, msg string, receivers []string) {
  98. if err := smtp_send(
  99. ctx,
  100. (*conf).SMTP.Host,
  101. utils.IntToStr((*conf).SMTP.Port),
  102. (*conf).SMTP.Login,
  103. (*conf).SMTP.Password,
  104. subject,
  105. msg,
  106. receivers,
  107. ); err == nil {
  108. if _, err := db.Exec(
  109. ctx,
  110. `UPDATE fave_notify_mail SET status = 1 WHERE id = ?;`,
  111. id,
  112. ); err != nil {
  113. fmt.Printf("Smtp send error (sql, success): %v\n", err)
  114. }
  115. } else {
  116. if _, err := db.Exec(
  117. ctx,
  118. `UPDATE fave_notify_mail SET error = ?, status = 0 WHERE id = ?;`,
  119. err.Error(),
  120. id,
  121. ); err != nil {
  122. fmt.Printf("Smtp send error (sql, error): %v\n", err)
  123. }
  124. }
  125. }(
  126. db,
  127. conf,
  128. utils.StrToInt(string(values[0])),
  129. html.EscapeString(string(values[2])),
  130. string(values[3]),
  131. []string{html.EscapeString(string(values[1]))},
  132. )
  133. } else {
  134. fmt.Printf("Smtp send error (sql, update): %v\n", err)
  135. }
  136. }
  137. }
  138. }
  139. }
  140. func smtp_send(ctx context.Context, host, port, user, pass, subject, msg string, receivers []string) error {
  141. return utils.SMTPSend(host, port, user, pass, subject, msg, receivers)
  142. }