sendgrid.go 3.1 KB

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