Giter Site home page Giter Site logo

hedwig's Introduction

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: "[email protected]"}
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: "[email protected]"},
}
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: "[email protected]"})
importants := client.WithDefaultFrom(mail.Address{Address: "[email protected]"})

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)

hedwig's People

Contributors

uhey22e avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.