smtp2go

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: MIT Imports: 7 Imported by: 5

README

Go Reference Build Status license

SMTP2GO API

Go wrapper around the SMTP2GO /email/send API endpoint.

Installation

go get github.com/smtp2go-oss/smtp2go-go

Add the import in your source file

import "github.com/smtp2go-oss/smtp2go-go"

Usage

Sign up for a free account here and once logged in navigate to the Settings -> API Keys page, create a new API key and make sure the /email/send endpoint is enabled:

Once you have an API key you need to export it into the environment where your Go application is going to be executed, this can be done on the terminal like so:

`$ export SMTP2GO_API_KEY="<your_API_key>"`

Or alternatively you can set it in code via

import "os"
os.Setenv("SMTP2GO_API_KEY", "<your_API_key>")

Then sending mail is as simple as:

package main

import (
	"fmt"
	"github.com/smtp2go-oss/smtp2go-go"
)

func main() {

	email := smtp2go.Email{
		From: "Matt <matt@example.com>",
		To: []string{
			"Dave <dave@example.com>",
		},
		Subject:  "Trying out SMTP2GO",
		TextBody: "Test Message",
		HtmlBody: "<h1>Test Message</h1>",
	}
	res, err := smtp2go.Send(&email)
	if err != nil {
		fmt.Printf("An Error Occurred: %s", err)
	}
	fmt.Printf("Sent Successfully: %s", res)
}

You can also send Asynchronously:

package main

import (
	"fmt"
	"github.com/smtp2go-oss/smtp2go-go"
)

func main() {

	email := smtp2go.Email{
		From: "Matt <matt@example.com>",
		To: []string{
			"Dave <dave@example.com>",
		},
		Subject:  "Trying out SMTP2GO",
		TextBody: "Test Message",
		HtmlBody: "<h1>Test Message</h1>",
	}

	var c chan *smtp2go.SendAsyncResult = smtp2go.SendAsync(&email)
	res := <-c
	if res.Error != nil {
		fmt.Printf("An Error Occurred: %s", res.Error)
	}
	fmt.Printf("Sent Successfully: %s", res.Result)
}

Development

Clone repo. Run tests with go test.

Contributing

Bug reports and pull requests are welcome on GitHub here

License

The package is available as open source under the terms of the MIT License.

Documentation

Index

Constants

View Source
const APIHeader string = "X-Smtp2go-Api"
View Source
const APIKeyEnv string = "SMTP2GO_API_KEY"
View Source
const APIKeyHeader string = "X-Smtp2go-Api-Key"
View Source
const APIRootEnv string = "SMTP2GO_API_ROOT"
View Source
const APIVersionHeader string = "X-Smtp2go-Api-Version"

Variables

View Source
var APIKeyRegexp *regexp.Regexp = regexp.MustCompile("^api-[a-zA-Z0-9]{32}$")

Functions

func SendAsync

func SendAsync(e *Email) chan *SendAsyncResult

SendAsync asynchronous send function

Types

type Email

type Email struct {
	From          string               `json:"sender"`
	To            []string             `json:"to"`
	Cc            []string             `json:"cc"`
	Bcc           []string             `json:"bcc"`
	Subject       string               `json:"subject"`
	TextBody      string               `json:"text_body"`
	HtmlBody      string               `json:"html_body"`
	TemplateID    string               `json:"template_id"`
	TemplateData  interface{}          `json:"template_data"`
	CustomHeaders []*EmailCustomHeader `json:"custom_headers"`
	Attachments   []*EmailBinaryData   `json:"attachments"`
	Inlines       []*EmailBinaryData   `json:"inlines"`
}

Email holds the data used to send the email

type EmailBinaryData added in v1.0.4

type EmailBinaryData struct {
	Filename string `json:"filename"`
	Fileblob string `json:"fileblob"`
	URL      string `json:"url"`
	MimeType string `json:"mimetype"`
}

type EmailCustomHeader added in v1.0.4

type EmailCustomHeader struct {
	Header string `json:"header"`
	Value  string `json:"value"`
}

type EndpointError

type EndpointError struct {
	// contains filtered or unexported fields
}

EndpointError error during endpoint call

func (EndpointError) Error

func (f EndpointError) Error() string

Error implementation of Error on EndpointError

type IncorrectAPIKeyFormatError

type IncorrectAPIKeyFormatError struct {
	// contains filtered or unexported fields
}

IncorrectAPIKeyFormatError error for bad api key

func (IncorrectAPIKeyFormatError) Error

Error implementation of Error on IncorrectAPIKeyFormatError

type InvalidJSONError

type InvalidJSONError struct {
	// contains filtered or unexported fields
}

InvalidJSONError error due to bad json

func (InvalidJSONError) Error

func (f InvalidJSONError) Error() string

Error implementation of Error on InvalidJSONError

type MissingAPIKeyError

type MissingAPIKeyError string

MissingAPIKeyError error for missing api key

func (MissingAPIKeyError) Error

func (f MissingAPIKeyError) Error() string

Error implementation of Error on MissingAPIKeyError

type MissingRequiredFieldError

type MissingRequiredFieldError struct {
	// contains filtered or unexported fields
}

MissingRequiredFieldError error fro missing field

func (MissingRequiredFieldError) Error

Error implementation of Error on MissingRequiredFieldError

type RequestError

type RequestError struct {
	// contains filtered or unexported fields
}

RequestError error during request

func (RequestError) Error

func (f RequestError) Error() string

Error implementation of Error on RequestError

type SendAsyncResult

type SendAsyncResult struct {
	Error  error
	Result *Smtp2goApiResult
}

SendAsyncResult result struct from async send call

type Smtp2goApiResult

type Smtp2goApiResult struct {
	RequestId string                `json:"request_id"`
	Data      Smtp2goApiResult_Data `json:"data"`
}

Smtp2goApiResult response payload from the API

func Send

func Send(e *Email) (*Smtp2goApiResult, error)

Send synchronous send function

type Smtp2goApiResult_Data

type Smtp2goApiResult_Data struct {
	Error                 string                        `json:"error"`
	ErrorCode             string                        `json:"error_code"`
	FieldValidationErrors Smtp2goApiResult_FieldFailure `json:"field_validation_errors"`
}

Smtp2goApiResult_Data struct that holds the response data from the API

type Smtp2goApiResult_FieldFailure

type Smtp2goApiResult_FieldFailure struct {
	FieldName string `json:"fieldname"`
	Message   string `json:"message"`
}

Smtp2goApiResult_FieldFailure if fields failed on the api side this will hold the information

Jump to

Keyboard shortcuts

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