2
0

user_notifier.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package sendgrid
  2. import (
  3. "github.com/porter-dev/porter/internal/notifier"
  4. "github.com/sendgrid/sendgrid-go"
  5. "github.com/sendgrid/sendgrid-go/helpers/mail"
  6. )
  7. type UserNotifier struct {
  8. opts *UserNotifierOpts
  9. }
  10. type UserNotifierOpts struct {
  11. *SharedOpts
  12. PWResetTemplateID string
  13. PWGHTemplateID string
  14. VerifyEmailTemplateID string
  15. ProjectInviteTemplateID string
  16. }
  17. func NewUserNotifier(opts *UserNotifierOpts) notifier.UserNotifier {
  18. return &UserNotifier{opts}
  19. }
  20. func (s *UserNotifier) SendPasswordResetEmail(opts *notifier.SendPasswordResetEmailOpts) error {
  21. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  22. request.Method = "POST"
  23. sgMail := &mail.SGMailV3{
  24. Personalizations: []*mail.Personalization{
  25. {
  26. To: []*mail.Email{
  27. {
  28. Address: opts.Email,
  29. },
  30. },
  31. DynamicTemplateData: map[string]interface{}{
  32. "url": opts.URL,
  33. "email": opts.Email,
  34. },
  35. },
  36. },
  37. From: &mail.Email{
  38. Address: s.opts.SenderEmail,
  39. Name: "Porter",
  40. },
  41. TemplateID: s.opts.PWResetTemplateID,
  42. }
  43. request.Body = mail.GetRequestBody(sgMail)
  44. _, err := sendgrid.API(request)
  45. return err
  46. }
  47. func (s *UserNotifier) SendGithubRelinkEmail(opts *notifier.SendGithubRelinkEmailOpts) error {
  48. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  49. request.Method = "POST"
  50. sgMail := &mail.SGMailV3{
  51. Personalizations: []*mail.Personalization{
  52. {
  53. To: []*mail.Email{
  54. {
  55. Address: opts.Email,
  56. },
  57. },
  58. DynamicTemplateData: map[string]interface{}{
  59. "url": opts.URL,
  60. "email": opts.Email,
  61. },
  62. },
  63. },
  64. From: &mail.Email{
  65. Address: s.opts.SenderEmail,
  66. Name: "Porter",
  67. },
  68. TemplateID: s.opts.PWGHTemplateID,
  69. }
  70. request.Body = mail.GetRequestBody(sgMail)
  71. _, err := sendgrid.API(request)
  72. return err
  73. }
  74. func (s *UserNotifier) SendEmailVerification(opts *notifier.SendEmailVerificationOpts) error {
  75. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  76. request.Method = "POST"
  77. sgMail := &mail.SGMailV3{
  78. Personalizations: []*mail.Personalization{
  79. {
  80. To: []*mail.Email{
  81. {
  82. Address: opts.Email,
  83. },
  84. },
  85. DynamicTemplateData: map[string]interface{}{
  86. "url": opts.URL,
  87. "email": opts.Email,
  88. },
  89. },
  90. },
  91. From: &mail.Email{
  92. Address: s.opts.SenderEmail,
  93. Name: "Porter",
  94. },
  95. TemplateID: s.opts.VerifyEmailTemplateID,
  96. }
  97. request.Body = mail.GetRequestBody(sgMail)
  98. _, err := sendgrid.API(request)
  99. return err
  100. }
  101. func (s *UserNotifier) SendProjectInviteEmail(opts *notifier.SendProjectInviteEmailOpts) error {
  102. request := sendgrid.GetRequest(s.opts.APIKey, "/v3/mail/send", "https://api.sendgrid.com")
  103. request.Method = "POST"
  104. sgMail := &mail.SGMailV3{
  105. Personalizations: []*mail.Personalization{
  106. {
  107. To: []*mail.Email{
  108. {
  109. Address: opts.InviteeEmail,
  110. },
  111. },
  112. DynamicTemplateData: map[string]interface{}{
  113. "url": opts.URL,
  114. "sender_email": opts.ProjectOwnerEmail,
  115. "project": opts.Project,
  116. },
  117. },
  118. },
  119. From: &mail.Email{
  120. Address: s.opts.SenderEmail,
  121. Name: "Porter",
  122. },
  123. TemplateID: s.opts.ProjectInviteTemplateID,
  124. }
  125. request.Body = mail.GetRequestBody(sgMail)
  126. _, err := sendgrid.API(request)
  127. return err
  128. }