postmark

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2016 License: MIT Imports: 9 Imported by: 15

README

postmark-go

GoDoc Build Status

postmark-go is a Go client library for accessing the Postmark API (http://developer.postmarkapp.com/).

This is an unofficial library that is not affiliated with Postmark. Official libraries are available here.

Installation

go get -u github.com/mattevans/postmark-go

Setup

You'll need to pass an SERVER_API_TOKEN when initializing the client. This token can be found under the 'Credentials' tab of your Postmark server. More info here.

Authentication

auth := &http.Client{
  Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}
client := postmark.NewClient(auth)

Example usage (with Template)

emailReq := &postmark.Email{
  From:       "mail@company.com",
  To:         "jack@sparrow.com",
  TemplateID: 123456,
  TemplateModel: map[string]interface{}{
    "name": "Jack",
    "action_url": "http://click.company.com/welcome",
  },
  Tag:        "onboarding",
  TrackOpens: true,
}

email, response, err := client.Email.Send(emailReq)
if err != nil {
  return err
}

Example usage (with HtmlBody)

emailReq := &postmark.Email{
  From:       "mail@company.com",
  To:         "jack@sparrow.com",
  Subject:    "My Test Email",
  HtmlBody:   "<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
  TextBody:   "Hello dear Postmark user",
  Tag:        "onboarding",
  TrackOpens: true,
}

email, response, err := client.Email.Send(emailReq)
if err != nil {
  return err
}

What's Implemented?

At the moment only a very small number of API endpoints are implemented. Open an issue (or PR) if you required additional endpoints!

Thanks & Acknowledgements 👌

The packages's architecture is adapted from go-github, created by Will Norris. 🍻

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors. A response is considered an error if it has a status code outside the 200 range. API error responses map to ErrorResponse.

Types

type AuthTransport

type AuthTransport struct {
	Transport http.RoundTripper
	Token     string
}

AuthTransport holds a authentication information for Postmark API.

func (*AuthTransport) RoundTrip

func (t *AuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip implements the RoundTripper interface.

type BounceDeliveryStats added in v0.1.2

type BounceDeliveryStats struct {
	InactiveMails int64
	Bounces       []BounceType
}

BounceDeliveryStats represents a collection of bounce stats.

type BounceService added in v0.1.2

type BounceService service

BounceService handles communication with the bounce related methods of the Postmark API (http://developer.postmarkapp.com/developer-api-bounce.html)

func (*BounceService) GetDeliveryStats added in v0.1.2

func (s *BounceService) GetDeliveryStats() (*BounceDeliveryStats, *Response, error)

GetDeliveryStats will build and execute request to send an email via the API.

type BounceType added in v0.1.2

type BounceType struct {
	Type  string
	Name  string
	Count int64
}

BounceType represents the type of bounce with a count.

type Client

type Client struct {
	Token          string
	ConnectionType string
	UserAgent      string
	BackendURL     *url.URL

	// Services used for communicating with the API.
	Email  *EmailService
	Bounce *BounceService
	// contains filtered or unexported fields
}

Client holds a connection to the Postmark API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient creates a new Client with the appropriate connection details and services used for communicating with the API.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*Response, error)

Do sends an API request and returns the API response. The API response is JSON decoded and stored in 'v', or returned as an error if an API (if found).

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlPath string, body interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlPath, which will be resolved to the BackendURL of the Client.

type Email

type Email struct {
	From          string                 `json:",omitempty"`
	To            string                 `json:",omitempty"`
	Cc            string                 `json:",omitempty"`
	Bcc           string                 `json:",omitempty"`
	Subject       string                 `json:",omitempty"`
	Tag           string                 `json:",omitempty"`
	TemplateID    int                    `json:",omitempty"`
	TemplateModel map[string]interface{} `json:",omitempty"`
	HTMLBody      string                 `json:",omitempty"`
	TextBody      string                 `json:",omitempty"`
	ReplyTo       string                 `json:",omitempty"`
	Headers       []EmailHeader          `json:",omitempty"`
	Attachments   []EmailAttachment      `json:",omitempty"`
	TrackOpens    bool                   `json:",omitempty"`
}

Email is the set of parameters that can be used when sending an email.

type EmailAttachment added in v0.1.1

type EmailAttachment struct {
	Name        string
	Content     []byte
	ContentType *string
}

EmailAttachment represents the values for an email attachment.

type EmailHeader

type EmailHeader struct {
	Name  string
	Value string
}

EmailHeader represents the values for an email header.

type EmailResponse

type EmailResponse struct {
	To          string
	SubmittedAt time.Time
	MessageID   string
}

EmailResponse is the set of parameters that is used in response to a send request

type EmailService

type EmailService service

EmailService handles communication with the email related methods of the Postmark API (http://developer.postmarkapp.com/developer-api-email.html)

func (*EmailService) Send

func (s *EmailService) Send(emailRequest *Email) (*EmailResponse, *Response, error)

Send will build and execute request to send an email via the API.

func (*EmailService) SendBatch

func (s *EmailService) SendBatch(emailRequests []*Email) ([]*EmailResponse, *Response, error)

SendBatch will build and execute request to send batch emails via the API.

type ErrorResponse

type ErrorResponse struct {
	*http.Response
	ErrorCode int64
	Message   string
}

An ErrorResponse reports the error caused by an API request

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

type Response

type Response struct {
	*http.Response
	ErrorCode int64
	Message   string
}

Response is a Postmark response. This wraps the standard http.Response returned from the Postmark API.

Directories

Path Synopsis
examples
batch-emails command
send-email command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL