diff --git a/mailtrap-email/README.md b/mailtrap-email/README.md new file mode 100644 index 0000000..6bb8efb --- /dev/null +++ b/mailtrap-email/README.md @@ -0,0 +1,50 @@ +# Send Email with Mailtrap + +This example shows how to send transactional emails using [Mailtrap's Email API](https://mailtrap.io) with Encore. + +## Prerequisites + +- A [Mailtrap account](https://mailtrap.io/signup) +- A verified [sending domain](https://mailtrap.io/sending/domains) +- A Mailtrap API token from [API settings](https://mailtrap.io/api-tokens) + +## Setup + +1. Clone and install the example: +```bash + encore app create my-app --example=mailtrap-email + cd my-app +``` + +2. Set your Mailtrap API token as an Encore secret: +```bash + encore secret set --type local,dev,prod MailtrapAPIToken +``` + +3. Run the app locally: +```bash + encore run +``` + +## Send an email + +Call the API endpoint to send an email: + +```bash +curl -X POST http://localhost:4000/email/send \ + -H "Content-Type: application/json" \ + -d '{ + "to": "recipient@example.com", + "subject": "Hello from Encore + Mailtrap", + "body": "This email was sent using Mailtrap Email API and Encore." + }' +``` + +## Using Mailtrap Sandbox for testing + +To test emails safely without delivering to real inboxes, use Mailtrap's Sandbox API by setting the `MAILTRAP_SANDBOX` environment variable to `true` and providing your `MAILTRAP_INBOX_ID`. + +## Learn more + +- [Mailtrap Email API docs](https://mailtrap.io/docs) +- [Encore documentation](https://encore.dev/docs) diff --git a/mailtrap-email/email/email.go b/mailtrap-email/email/email.go new file mode 100644 index 0000000..459f948 --- /dev/null +++ b/mailtrap-email/email/email.go @@ -0,0 +1,73 @@ +package email + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + + "encore.dev/config" +) + +var secrets struct { + MailtrapAPIToken string +} + +type SendParams struct { + To string `json:"to"` + Subject string `json:"subject"` + Body string `json:"body"` +} + +type SendResponse struct { + Success bool `json:"success"` + Message string `json:"message"` +} + +// Send sends a transactional email via Mailtrap Email API. +// +//encore:api public method=POST path=/email/send +func Send(ctx context.Context, p *SendParams) (*SendResponse, error) { + payload := map[string]interface{}{ + "from": map[string]string{ + "email": "hello@yourdomain.com", + "name": "My App", + }, + "to": []map[string]string{ + {"email": p.To}, + }, + "subject": p.Subject, + "text": p.Body, + } + + body, err := json.Marshal(payload) + if err != nil { + return nil, fmt.Errorf("failed to marshal payload: %w", err) + } + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, + "https://send.api.mailtrap.io/api/send", + bytes.NewReader(body)) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Authorization", "Bearer "+secrets.MailtrapAPIToken) + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to send email: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("mailtrap API error: status %d", resp.StatusCode) + } + + return &SendResponse{ + Success: true, + Message: "Email sent successfully", + }, nil +}