tinymail

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

README

Go Reference Go Report Card

tinymail

tinymail is a small package to easily send simple emails in go.

Download

go get github.com/XotoX1337/tinymail

Features

  • SMTP Authentification
  • Email with text body
  • Email from Template as String or File
  • Attachments

Examples

Text Email
import "github.com/XotoX1337/tinymail"

opts := tinymail.MailerOpts{
    User: "username",
    Password: "password",
    Host: "host",
    Port: 587
}
mailer := tinymail.New(opts)
msg := tinymail.FromString("this is a example")
msg.SetFrom("test@tinymail.test")
msg.SetTo("test.to@tinymail.test")
msg.SetSubject("TestWriteMessage")
err := mailer.SetMessage(msg).Send()
if err != nil {
    fmt.Println(err)
}
# send success
Email from Template
import "github.com/XotoX1337/tinymail"

opts := tinymail.MailerOpts{
    User: "username",
    Password: "password",
    Host: "host",
    Port: 587
}
mailer := tinymail.New(opts)
msg := tinymail.FromTemplateFile(path/to/template/file)
msg.SetFrom("test@tinymail.test")
msg.SetTo("test.to@tinymail.test")
msg.SetSubject("TestWriteMessage")
err := mailer.SetMessage(msg).Send()
if err != nil {
    fmt.Println(err)
}
# send success
Email with Attachments
import "github.com/XotoX1337/tinymail"

opts := tinymail.MailerOpts{
    User: "username",
    Password: "password",
    Host: "host",
    Port: 587
}
mailer := tinymail.New(opts)
msg := tinymail.FromString("attachment example")
msg.SetFrom("test@tinymail.test")
msg.SetTo("test.to@tinymail.test")
msg.SetSubject("TestWriteMessage")
msg.Attach(path/to/file, path/to/second/file, ...)
err := mailer.SetMessage(msg).Send()
if err != nil {
    fmt.Println(err)
}
# send success

Documentation

Overview

Package tinymail provides a simple and easy to use interface to send smtp emails.

Index

Constants

View Source
const DEFAULT_SMTP_PORT int = 587

Variables

This section is empty.

Functions

func FromString added in v0.7.0

func FromString(str string) *message

FromString creates a new message with content from given string.

func FromTemplateFile

func FromTemplateFile(data any, filenames ...string) (*message, error)

FromTemplateFile creates a new message with content from parsed template file.

Returns an error if the template file could not be parsed.

func FromTemplateString

func FromTemplateString(data any, tpl string) (*message, error)

FromTemplateString creates a new message with content from parsed template string.

Returns an error if the template string could not be parsed.

func New

func New(opts MailerOpts) (*mailer, error)

New returns a new Mailer instance

Returns an error, if opts could not be validated

Types

type Mailer

type Mailer interface {
	Send() error
	SetBoundary(boundary string)
	Boundary() string
	Config() *smtpConfig
}

type MailerOpts added in v0.7.0

type MailerOpts struct {
	User     string
	Password string
	Host     string
	Port     int
}

type Message

type Message interface {
	SetFrom(from string)
	From() string
	SetTo(to ...string)
	To() []string
	SetCC(cc ...string)
	CC() []string
	SetBCC(bcc ...string)
	BCC() []string
	SetSubject(s string)
	Subject() string
	Attach(files ...string) error
	Attachments() map[string][]byte
	Body() string
	SetUrgentPriority()
	SetNonUrgentPriority()
	SetNormalPriority()
	Priority() string
}

Jump to

Keyboard shortcuts

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