mirror of
https://github.com/KevinMidboe/planetposen-mail.git
synced 2025-10-29 17:50:32 +00:00
Mail api for sending order confirmation to planetposen customers
This commit is contained in:
56
client/sendgrid/send_order_confirmation.go
Normal file
56
client/sendgrid/send_order_confirmation.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package sendgrid
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/kevinmidboe/planetposen-mail/client"
|
||||
"github.com/kevinmidboe/planetposen-mail/mail"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SendOrderConfirmation sends an order confirmation.
|
||||
func (c *Client) SendOrderConfirmation(ctx context.Context, record mail.OrderConfirmationEmailData) error {
|
||||
reqBody := sendEmailPayload{
|
||||
Personalizations: []personalization{
|
||||
{
|
||||
To: []email{
|
||||
{
|
||||
Email: record.ToEmail,
|
||||
},
|
||||
},
|
||||
Subject: record.Subject,
|
||||
},
|
||||
},
|
||||
From: email{
|
||||
Email: record.FromEmail,
|
||||
Name: record.FromName,
|
||||
},
|
||||
Content: []content{
|
||||
{
|
||||
Type: "text/html",
|
||||
Value: record.Markup,
|
||||
},
|
||||
},
|
||||
}
|
||||
jsonPayload, err := json.Marshal(reqBody)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshalling sendEmailPayload: %w", err)
|
||||
}
|
||||
reqData := client.HTTPRequestData{
|
||||
Method: http.MethodPost,
|
||||
URL: fmt.Sprintf("%s/v3/mail/send", c.Endpoint),
|
||||
Headers: map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": fmt.Sprintf("Bearer %s", c.APIKey),
|
||||
},
|
||||
PostPayload: jsonPayload,
|
||||
}
|
||||
_, err = c.HTTPClient.RequestBytes(ctx, reqData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error making request to sendgrid to send email: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
45
client/sendgrid/sendgrid.go
Normal file
45
client/sendgrid/sendgrid.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package sendgrid
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kevinmidboe/planetposen-mail/client"
|
||||
"github.com/kevinmidboe/planetposen-mail/config"
|
||||
)
|
||||
|
||||
// Client holds the HTTP client and endpoint information.
|
||||
type Client struct {
|
||||
Endpoint string
|
||||
APIKey string
|
||||
HTTPClient client.HTTPClient
|
||||
}
|
||||
|
||||
// Init sets up a new sendgrid client.
|
||||
func (c *Client) Init(config *config.Config) error {
|
||||
timeout := 5 * time.Second
|
||||
c.Endpoint = config.SendGridAPIEndpoint
|
||||
c.APIKey = config.SendGridAPIKey
|
||||
c.HTTPClient = client.NewHTTPClient(client.Parameters{Timeout: &timeout})
|
||||
return nil
|
||||
}
|
||||
|
||||
type sendEmailPayload struct {
|
||||
Personalizations []personalization `json:"personalizations"`
|
||||
From email `json:"from"`
|
||||
Content []content `json:"content"`
|
||||
}
|
||||
|
||||
type personalization struct {
|
||||
To []email `json:"to"`
|
||||
Subject string `json:"subject"`
|
||||
}
|
||||
|
||||
type email struct {
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type content struct {
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
Reference in New Issue
Block a user