hedwig

package module
v0.0.0-...-df7706a Latest Latest
Warning

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

Go to latest
Published: May 8, 2022 License: MIT Imports: 4 Imported by: 0

README

hedwig

An email client for Golang, supports multiple email services.

You can use following sending methods.

Installation

This package can be installed with the go get command:

go get github.com/uhey22e/hedwig

Usage

You can send an email via the hedwig.Client interface. SMTP, Amazon SES and other clients implements this interface.

Basic usage - send an email with Gmail.

import (
	"context"
	"net/mail"
	"net/smtp"

	"github.com/uhey22e/hedwig"
	"github.com/uhey22e/hedwig/generalsmtp"
)

from := mail.Address{Address: "from@example.com"}
auth := smtp.PlainAuth("", from.Address, "yourpassword", "smtp.gmail.com")
client, _ := generalsmtp.OpenMailer(context.TODO(), "smtp.gmail.com:587", auth, hedwig.DefaultFrom(from))
to := []mail.Address{
	{Address: "to@example.com"},
}
msg := &hedwig.Mail{
	Subject: "Subject",
}
// hedwig.EMail has io.Writer interface to write the message body.
io.WriteString(msg, "Hello world.")
client.SendMail(context.TODO(), from, to, msg)

Or you can use html/template to render the message body.

msg := &hedwig.Mail{
	Subject:     "Subject",
	ContentType: hedwig.ContentTypeHTML,
}
tmpl, _ := template.New("").Parse(`<p>Hello {{ . }}.</p>`)
tmpl.Execute(msg, "Bob")
client.SendMail(context.TODO(), from, to, msg)

You can duplicate the client to use multiple "from" addresses.

ctx := context.TODO()
addr := "smtp.example.com:25"
client, _ := generalsmtp.OpenMailer(ctx, addr, nil)
news := client.WithDefaultFrom(mail.Address{Address: "news@example.com"})
importants := client.WithDefaultFrom(mail.Address{Address: "importants@example.com"})

Supported services

Amazon SES

You can use github.com/uhey22e/hedwig/amazonses driver. This driver uses aws-sdk-go-v2 package.

import (
	"context"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/uhey22e/hedwig"
	"github.com/uhey22e/hedwig/amazonses"
)

ctx := context.TODO()
cfg, _ := config.LoadDefaultConfig(ctx)
client := amazonses.OpenMailer(ctx, cfg)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownDriver = errors.New("unknown driver")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	From mail.Address
}

type ContentType

type ContentType int

Content-Type of the email body.

const (
	ContentTypePlainText ContentType = iota
	ContentTypeHTML
)

func (ContentType) String

func (c ContentType) String() string

type Driver

type Driver interface {
	SendMail(ctx context.Context, from mail.Address, to []mail.Address, mail *Mail) error
}

type Mail

type Mail struct {
	// Subject of the email.
	Subject string
	// Content type of the message body. Defaults to text/plain.
	ContentType ContentType

	// Buffer for the message body.
	bytes.Buffer
}

Mail is a concrete representation of an email. Mail has embedded bytes.Buffer to read/write the message body.

type Mailer

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

func NewMailer

func NewMailer(d Driver, opts ...MailerOptionFn) *Mailer

NewMailer is intended for use by drivers only. Do not use in application code.

func (*Mailer) SendMail

func (m *Mailer) SendMail(ctx context.Context, to []mail.Address, mail *Mail) error

SendMail sends an email from the default from address.

func (*Mailer) SendMailFrom

func (m *Mailer) SendMailFrom(ctx context.Context, from mail.Address, to []mail.Address, mail *Mail) error

SendMail sends an email from specific address.

func (*Mailer) WithDefaultFrom

func (m *Mailer) WithDefaultFrom(from mail.Address) *Mailer

WithDefaultFrom creates an client with the DefaultFrom option.

type MailerOptionFn

type MailerOptionFn func(m *Mailer)

func DefaultFrom

func DefaultFrom(from mail.Address) MailerOptionFn

DefaultFrom sets an default email address for From property.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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