Categorygithub.com/smtp2go-oss/smtp2go-go
modulepackage
1.0.3
Repository: https://github.com/smtp2go-oss/smtp2go-go.git
Documentation: pkg.go.dev

# 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 <[email protected]>",
		To: []string{
			"Dave <[email protected]>",
		},
		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 <[email protected]>",
		To: []string{
			"Dave <[email protected]>",
		},
		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.

# Functions

Send synchronous send function.
SendAsync asynchronous send function.

# Structs

Email holds the data used to send the email.
EndpointError error during endpoint call.
IncorrectAPIKeyFormatError error for bad api key.
InvalidJSONError error due to bad json.
MissingRequiredFieldError error fro missing field.
RequestError error during request.
SendAsyncResult result struct from async send call.
Smtp2goApiResult response payload from the API.
Smtp2goApiResult_Data struct that holds the response data from the API.
Smtp2goApiResult_FieldFailure if fields failed on the api side this will hold the information.

# Type aliases

MissingAPIKeyError error for missing api key.