sendgrid.go 3.3 KB

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