sendgrid.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package email
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/sendgrid/sendgrid-go"
  6. "github.com/sendgrid/sendgrid-go/helpers/mail"
  7. )
  8. type SendgridClient struct {
  9. APIKey string
  10. PWResetTemplateID 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) SendEmailVerification(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.VerifyEmailTemplateID,
  64. }
  65. request.Body = mail.GetRequestBody(sgMail)
  66. _, err := sendgrid.API(request)
  67. return err
  68. }
  69. func (client *SendgridClient) SendProjectInviteEmail(url, project, projectOwnerEmail, email string) error {
  70. request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
  71. request.Method = "POST"
  72. fmt.Println("GOT HERE", url, project, projectOwnerEmail, email)
  73. sgMail := &mail.SGMailV3{
  74. Personalizations: []*mail.Personalization{
  75. {
  76. To: []*mail.Email{
  77. {
  78. Address: email,
  79. },
  80. },
  81. DynamicTemplateData: map[string]interface{}{
  82. "url": url,
  83. "sender_email": projectOwnerEmail,
  84. "project": project,
  85. },
  86. },
  87. },
  88. From: &mail.Email{
  89. Address: client.SenderEmail,
  90. Name: "Porter",
  91. },
  92. TemplateID: client.ProjectInviteTemplateID,
  93. }
  94. request.Body = mail.GetRequestBody(sgMail)
  95. _, err := sendgrid.API(request)
  96. return err
  97. }