git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/postmark/emails.go (about)

     1  package postmark
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  type Email struct {
    11  	// From: REQUIRED The sender email address. Must have a registered and confirmed Sender Signature.
    12  	From string
    13  	// To: REQUIRED Recipient email address. Multiple addresses are comma separated. Max 50.
    14  	To string
    15  	// Cc recipient email address. Multiple addresses are comma separated. Max 50.
    16  	Cc string `json:",omitempty"`
    17  	// Bcc recipient email address. Multiple addresses are comma separated. Max 50.
    18  	Bcc string `json:",omitempty"`
    19  	// Subject: Email subject
    20  	Subject string `json:",omitempty"`
    21  	// Tag: Email tag that allows you to categorize outgoing emails and get detailed statistics.
    22  	Tag string `json:",omitempty"`
    23  	// HtmlBody: HTML email message. REQUIRED, If no TextBody specified
    24  	HtmlBody string `json:",omitempty"`
    25  	// TextBody: Plain text email message. REQUIRED, If no HtmlBody specified
    26  	TextBody string `json:",omitempty"`
    27  	// ReplyTo: Reply To override email address. Defaults to the Reply To set in the sender signature.
    28  	ReplyTo string `json:",omitempty"`
    29  	// Headers: List of custom headers to include.
    30  	Headers []Header `json:",omitempty"`
    31  	// TrackOpens: Activate open tracking for this email.
    32  	TrackOpens bool `json:",omitempty"`
    33  	// Attachments: List of attachments
    34  	Attachments []Attachment `json:",omitempty"`
    35  	// Metadata: Custom metadata key/value pairs.
    36  	Metadata map[string]string `json:",omitempty"`
    37  	// Set message stream ID that's used for sending. If not provided, message will default to the "outbound" transactional stream.
    38  	MessageStream string
    39  }
    40  
    41  type Header struct {
    42  	// Name: header name
    43  	Name string
    44  	// Value: header value
    45  	Value string
    46  }
    47  
    48  type Attachment struct {
    49  	// Name: attachment name
    50  	Name string
    51  	// Content: Base64 encoded attachment data
    52  	Content string
    53  	// ContentType: attachment MIME type
    54  	ContentType string
    55  	// ContentId: populate for inlining images with the images cid
    56  	ContentID string `json:",omitempty"`
    57  }
    58  
    59  type EmailResponse struct {
    60  	// To: Recipient email address
    61  	To string
    62  	// SubmittedAt: Timestamp
    63  	SubmittedAt time.Time
    64  	// MessageID: ID of message
    65  	MessageID string
    66  	// ErrorCode: API Error Codes
    67  	ErrorCode int64
    68  	// Message: Response message
    69  	Message string
    70  }
    71  
    72  func (client *Client) SendEmail(ctx context.Context, serverToken string, email Email) (EmailResponse, error) {
    73  	res := EmailResponse{}
    74  	err := client.request(ctx, requestParams{
    75  		Method:      http.MethodPost,
    76  		URL:         "/email",
    77  		Payload:     email,
    78  		ServerToken: &serverToken,
    79  	}, &res)
    80  
    81  	if res.ErrorCode != 0 {
    82  		return res, fmt.Errorf("%v %s", res.ErrorCode, res.Message)
    83  	}
    84  
    85  	return res, err
    86  }
    87  
    88  // TODO: handle individual errors in []EmailResponse?
    89  func (client *Client) SendEmailsBatch(ctx context.Context, serverToken string, emails []Email) ([]EmailResponse, error) {
    90  	res := []EmailResponse{}
    91  	err := client.request(ctx, requestParams{
    92  		Method:      http.MethodPost,
    93  		URL:         "/email/batch",
    94  		Payload:     emails,
    95  		ServerToken: &serverToken,
    96  	}, &res)
    97  
    98  	return res, err
    99  }