sendmail

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

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

Go to latest
Published: Apr 6, 2018 License: GPL-3.0 Imports: 12 Imported by: 0

README

Go sendmail Build Status

This package implements classic, well known from PHP, method of sending emails. It's stupid simple and it works not only with Sendmail, but also with other MTAs, like Postfix or sSMTP, which provide compatibility interface.

  • it separates email headers from email body,
  • encodes UTF-8 headers like Subject, From, To
  • makes it easy to use text/template
  • doesn't require any SMTP configuration,
  • just uses /usr/sbin/sendmail command which is present on most of the systems,
    • if not, just update sendmail.Binary
  • outputs emails to stdout when environment variable DEBUG is set.

Installation

go get -u github.com/meehow/sendmail

Usage

package main

import (
	"io"
	"log"
	"net/mail"

	"github.com/meehow/sendmail"
)

func main() {
	sm := sendmail.Mail{
		Subject: "Cześć",
		From:    &mail.Address{"Michał", "me@example.com"},
		To: []*mail.Address{
			{"Ktoś", "info@example.com"},
			{"Ktoś2", "info2@example.com"},
		},
	}
	io.WriteString(&sm.Text, ":)\r\n")
	if err := sm.Send(); err != nil {
		log.Println(err)
	}
}

Instead of io.WriteString, you can execute a template:

tpl := template.Must(template.New("email").Parse(`Hello {{.Name}}!`))
tpl.ExecuteTemplate(&sm.Text, "email", &struct{ Name string }{"Dominik"})

ToDo

  • HTML emails

Documentation

Overview

Package sendmail implements then classic method of sending emails, well known from PHP.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Binary points to the sendmail binary.
	Binary = "/usr/sbin/sendmail"
)

Functions

func Validate

func Validate(email string) error

Validate checks if email host has assigned IP address

Types

type Mail

type Mail struct {
	Subject string
	From    *mail.Address
	To      []*mail.Address
	Header  http.Header
	Text    bytes.Buffer
	HTML    bytes.Buffer
}

Mail defines basic mail structure and headers

func (*Mail) Send

func (m *Mail) Send() error

Send sends an email, or prints it on stderr, when environment variable `DEBUG` is set.

func (*Mail) WriteTo

func (m *Mail) WriteTo(wr io.Writer) error

WriteTo writes headers and content of the email to io.Writer

Jump to

Keyboard shortcuts

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