email.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package notifiers
  2. import (
  3. "os"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/util"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  13. Type: "email",
  14. Name: "Email",
  15. Description: "Sends notifications using Grafana server configured SMTP settings",
  16. Factory: NewEmailNotifier,
  17. OptionsTemplate: `
  18. <h3 class="page-heading">Email addresses</h3>
  19. <div class="gf-form">
  20. <textarea rows="7" class="gf-form-input width-27" required ng-model="ctrl.model.settings.addresses"></textarea>
  21. </div>
  22. <div class="gf-form">
  23. <span>You can enter multiple email addresses using a ";" separator</span>
  24. </div>
  25. `,
  26. })
  27. }
  28. // EmailNotifier is responsible for sending
  29. // alert notifications over email.
  30. type EmailNotifier struct {
  31. NotifierBase
  32. Addresses []string
  33. log log.Logger
  34. }
  35. // NewEmailNotifier is the constructor function
  36. // for the EmailNotifier.
  37. func NewEmailNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  38. addressesString := model.Settings.Get("addresses").MustString()
  39. if addressesString == "" {
  40. return nil, alerting.ValidationError{Reason: "Could not find addresses in settings"}
  41. }
  42. // split addresses with a few different ways
  43. addresses := util.SplitEmails(addressesString)
  44. return &EmailNotifier{
  45. NotifierBase: NewNotifierBase(model),
  46. Addresses: addresses,
  47. log: log.New("alerting.notifier.email"),
  48. }, nil
  49. }
  50. // Notify sends the alert notification.
  51. func (en *EmailNotifier) Notify(evalContext *alerting.EvalContext) error {
  52. en.log.Info("Sending alert notification to", "addresses", en.Addresses)
  53. ruleURL, err := evalContext.GetRuleURL()
  54. if err != nil {
  55. en.log.Error("Failed get rule link", "error", err)
  56. return err
  57. }
  58. error := ""
  59. if evalContext.Error != nil {
  60. error = evalContext.Error.Error()
  61. }
  62. cmd := &models.SendEmailCommandSync{
  63. SendEmailCommand: models.SendEmailCommand{
  64. Subject: evalContext.GetNotificationTitle(),
  65. Data: map[string]interface{}{
  66. "Title": evalContext.GetNotificationTitle(),
  67. "State": evalContext.Rule.State,
  68. "Name": evalContext.Rule.Name,
  69. "StateModel": evalContext.GetStateModel(),
  70. "Message": evalContext.Rule.Message,
  71. "Error": error,
  72. "RuleUrl": ruleURL,
  73. "ImageLink": "",
  74. "EmbeddedImage": "",
  75. "AlertPageUrl": setting.AppUrl + "alerting",
  76. "EvalMatches": evalContext.EvalMatches,
  77. },
  78. To: en.Addresses,
  79. Template: "alert_notification.html",
  80. EmbededFiles: []string{},
  81. },
  82. }
  83. if evalContext.ImagePublicURL != "" {
  84. cmd.Data["ImageLink"] = evalContext.ImagePublicURL
  85. } else {
  86. file, err := os.Stat(evalContext.ImageOnDiskPath)
  87. if err == nil {
  88. cmd.EmbededFiles = []string{evalContext.ImageOnDiskPath}
  89. cmd.Data["EmbeddedImage"] = file.Name()
  90. }
  91. }
  92. err = bus.DispatchCtx(evalContext.Ctx, cmd)
  93. if err != nil {
  94. en.log.Error("Failed to send alert notification email", "error", err)
  95. return err
  96. }
  97. return nil
  98. }