smtpSender

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 27 Imported by: 0

README

smtpSender

Go Report Card

See cmd/sendemail.go file for example

go build -o sendemail sendemail.go
sendemail -h
sendemail -f from@domain.tld -t to@domain.tld -s "Hello subject!" -m "Hello, world!"
sendemail -f from@domain.tld -t to@domain.tld -s "Hello subject!" -html ./message.html -amp ./amp.html -txt ./message.txt

Send email

bldr := &smtpSender.Builder{
	From: "Sender <sender@domain.tld>",
	To: "Me <me+test@mail.tld>",
	Subject: "Test subject",
}
bldr.SetDKIM("domain.tld", "test", myPrivateKey)
bldr.AddHeader("Content-Language: ru", "Message-ID: <Id-123>", "Precedence: bulk")
bldr.AddTextPart("textPlain")
bldr.AddHTMLPart("<h1>textHTML</h1><img src=\"cid:image.gif\"/>", "./image.gif")
bldr.AddAttachment("./file.zip", "./music.mp3")
email := bldr.Email("Id-123", func(result smtpSender.Result){
	fmt.Printf("Result for email id '%s' duration: %f sec result: %v\n", result.ID, result.Duration.Seconds(), result.Err)
})
	
conn := new(smtpSender.Connect)
conn.SetHostName("sender.domain.tld")
conn.SetMapIP("192.168.0.10", "31.32.33.34")
	
email.Send(conn, nil)

or

server = &smtpSender.SMTPserver{
	Host:     "smtp.server.tld",
	Port:     587,
	Username: "sender@domain.tld",
	Password: "password",
}
email.Send(conn, server)

Best way send email from pool

pipe := smtpSender.NewPipe(
	smtpSender.Config{
		Iface:  "31.32.33.34",
		Stream:   5,
	},
	smtpSender.Config{
		Iface:  "socks5://222.222.222.222:7080",
		Stream: 2,
	})
pipe.Start()

for i := 1; i <= 50; i++ {
    bldr := new(smtpSender.Builder)
    bldr.SetFrom("Sender", "sender@domain.tld")
    bldr.SetTo("Me", "me+test@mail.tld")
    bldr.SetSubject("Test subject " + id)
    bldr.AddHTMLPart("<h1>textHTML</h1><img src=\"cid:image.gif\"/>", "./image.gif")
    email := bldr.Email(id, func(result smtpSender.Result) {
       	fmt.Printf("Result for email id '%s' duration: %f sec result: %v\n", result.ID, result.Duration.Seconds(), result.Err)
       	wg.Done()
    })
	err := pipe.Send(email)
	if err != nil {
		fmt.Printf("Send email id '%d' error %+v\n", i, err)
		break
	}
	if i == 35 {
		pipe.Stop()
	}
}

Use template for email

import (
	...
	tmplHTML "html/template"
	tmplText "text/template"
)
    ...
	subj := tmplText.New("Subject")
	subj.Parse("{{.Name}} this template subject text.")
	html := tmplHTML.New("HTML")
	html.Parse(`<h1>This 'HTML' template.</h1><img src="cid:image.gif"><h2>Hello {{.Name}}!</h2>`)
	text := tmplText.New("Text")
	text.Parse("This 'Text' template. Hello {{.Name}}!")
	data := map[string]string{"Name": "Вася"}
	bldr.AddSubjectFunc(func(w io.Writer) error {
		return subj.Execute(w, &data)
	})
	bldr.AddTextFunc(func(w io.Writer) error {
		return text.Execute(w, &data)
	})
	bldr.AddHTMLFunc(func(w io.Writer) error {
		return html.Execute(w, &data)
	}, "./image.gif")
    ...

One more method send email from pool (Depricated)

emailPipe := smtpSender.NewEmailPipe(
	smtpSender.Config{
		Iface:  "31.32.33.34",
		Stream:   5,
	},
	smtpSender.Config{
		Iface:  "socks5://222.222.222.222:7080",
		Stream: 2,
	})

start := time.Now()
wg := &sync.WaitGroup{}
for i := 1; i <= 15; i++ {
	id := "Id-" + strconv.Itoa(i)
	bldr := new(smtpSender.Builder)
	bldr.SetFrom("Sender", "sender@domain.tld")
	bldr.SetTo("Me", "me+test@mail.tld")
	bldr.SetSubject("Test subject " + id)
	bldr.SetDKIM("domain.tld", "test", myPrivateKey)
	bldr.AddHeader("Content-Language: ru", "Message-ID: <Id-123>", "Precedence: bulk")
	bldr.AddTextPart("textPlain")
	bldr.AddHTMLPart("<h1>textHTML</h1><img src=\"cid:image.gif\"/>", "./image.gif")
	bldr.AddAttachment("./file.zip", "./music.mp3")
	wg.Add(1)
	email := bldr.Email(id, func(result smtpSender.Result) {
		fmt.Printf("Result for email id '%s' duration: %f sec result: %v\n", result.ID, result.Duration.Seconds(), result.Err)
		wg.Done()
	})
	emailPipe <- email
}
wg.Wait()

fmt.Printf("Stream send duration: %s\r\n", time.Now().Sub(start).String())

Documentation

Index

Constants

View Source
const (
	DKIMSignMethodDoubleWrite = iota
	DKIMSignMethodBufferWrite
)
View Source
const Version = "0.0.8"

Variables

View Source
var ErrPipeStopped = errors.New("email streaming pipe stopped")

Functions

func NewEmailPipe

func NewEmailPipe(conf ...Config) chan<- Email

NewEmailPipe return new chanel for stream send Deprecated: use NewPipe

Types

type Builder

type Builder struct {
	From    string
	To      string
	Subject string
	// contains filtered or unexported fields
}

Builder helper for create email

func NewBuilder added in v0.0.6

func NewBuilder() *Builder

NewBuilder return new Builder

func (*Builder) AddAMPFunc added in v0.0.6

func (b *Builder) AddAMPFunc(f func(io.Writer) error) *Builder

AddAMPFunc add writer function for AMP HTML

func (*Builder) AddAMPPart added in v0.0.6

func (b *Builder) AddAMPPart(amp []byte) *Builder

AddAMPPart add text/x-amp-html content with related file.

func (*Builder) AddAttachment

func (b *Builder) AddAttachment(file ...string) error

AddAttachment add attachment files to email

func (*Builder) AddHTMLFunc

func (b *Builder) AddHTMLFunc(f func(io.Writer) error, file ...string) error

AddHTMLFunc add writer function for HTML

func (*Builder) AddHTMLPart added in v0.0.6

func (b *Builder) AddHTMLPart(html []byte, file ...string) (err error)

AddHTMLPart add text/html content with related file.

Example use related file in html

AddHTMLPart(
	`... <img src="cid:myImage.jpg" width="500px" height="250px" border="1px" alt="My image"/> ...`,
	"/path/to/attach/myImage.jpg",
)

func (*Builder) AddHeader

func (b *Builder) AddHeader(headers ...string) *Builder

AddHeader add extra header to email

func (*Builder) AddMIMEHeader added in v0.0.6

func (b *Builder) AddMIMEHeader(mimeHeader textproto.MIMEHeader) *Builder

AddMIMEHeader add extra mime header to email

func (*Builder) AddReplyTo

func (b *Builder) AddReplyTo(name, email string) *Builder

AddReplyTo add Reply-To header

func (*Builder) AddSubjectFunc

func (b *Builder) AddSubjectFunc(f func(io.Writer) error) *Builder

AddSubjectFunc add writer function for subject

func (*Builder) AddTextFunc

func (b *Builder) AddTextFunc(f func(io.Writer) error) *Builder

AddTextFunc add writer function for plain text

func (*Builder) AddTextHTML

func (b *Builder) AddTextHTML(html []byte, file ...string) (err error)

AddTextHTML Deprecated: use AddHTMLPart

func (*Builder) AddTextPart added in v0.0.6

func (b *Builder) AddTextPart(text []byte) *Builder

AddTextPart add plain text

func (*Builder) AddTextPlain

func (b *Builder) AddTextPlain(text []byte)

AddTextPlain add plain text Deprecated: use AddTextPart

func (*Builder) Email

func (b *Builder) Email(id string, resultFunc func(Result)) *Email

Email return Email struct with render function

func (*Builder) SetDKIM

func (b *Builder) SetDKIM(domain, selector string, privateKey []byte) *Builder

SetDKIM sign DKIM parameters

func (*Builder) SetDKIMSignMethod added in v0.0.12

func (b *Builder) SetDKIMSignMethod(signMethod int) *Builder

func (*Builder) SetFrom

func (b *Builder) SetFrom(name, email string) *Builder

SetFrom email sender

func (*Builder) SetSubject

func (b *Builder) SetSubject(subject string) *Builder

SetSubject set email subject

func (*Builder) SetTo

func (b *Builder) SetTo(name, email string) *Builder

SetTo email recipient

type Config

type Config struct {
	Hostname   string
	Iface      string
	Port       int
	Stream     int
	MapIP      map[string]string
	SMTPserver *SMTPserver
}

Config profile for sender pool

type Connect

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

Connect to smtp server from configured interface

func (*Connect) SetHostName

func (c *Connect) SetHostName(name string)

SetHostName set server hostname for HELO. If left blanc then use resolv name.

func (*Connect) SetIface

func (c *Connect) SetIface(iface string)

SetIface use this Iface for send. Default use default interface. Example:

IP "1.2.3.4"
Socks5 proxy "socks5://user:password@1.2.3.4" or "socks5://1.2.3.4"

func (*Connect) SetMapIP

func (c *Connect) SetMapIP(localIP, globalIP string)

SetMapIP if use NAT set global IP address

func (*Connect) SetSMTPport

func (c *Connect) SetSMTPport(port int)

SetSMTPport set SMTP server port. Default 25 use for translate local Iface to global if NAT if use Socks server translate Iface SOCKS server to real Iface

type DelimitWriter added in v0.0.6

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

DelimitWriter Writer with delimiter bytes

func NewDelimitWriter added in v0.0.6

func NewDelimitWriter(writer io.Writer, delimiter []byte, cnt int) *DelimitWriter

NewDelimitWriter get writer, delimit bytes and count through which to add a delimit bytes. Return DelimitWriter

func (*DelimitWriter) Write added in v0.0.6

func (w *DelimitWriter) Write(p []byte) (int, error)

Write write delimiter function

type Email

type Email struct {
	// ID is id for return result
	ID string
	// From emailField has format
	// example
	//  "Name <emailField@domain.tld>"
	//  "<emailField@domain.tld>"
	//  "emailField@domain.tld"
	From string

	// To emailField has format as From
	To string

	// ResultFunc exec after send emil
	ResultFunc func(Result)
	// WriteCloser email body data writer function
	WriteCloser func(io.WriteCloser) error
	// DontUseTLS STARTTLS off
	DontUseTLS bool
	// contains filtered or unexported fields
}

Email struct

func (*Email) Send

func (e *Email) Send(connect *Connect, server *SMTPserver)

Send sending this email

type Pipe

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

Pipe email pipe for send email

func NewPipe

func NewPipe(conf ...Config) *Pipe

NewPipe return new stream sender pipe

func (*Pipe) Send

func (pipe *Pipe) Send(email Email) (err error)

Send add email to stream

func (*Pipe) Start

func (pipe *Pipe) Start()

Start stream sender

func (*Pipe) Stop

func (pipe *Pipe) Stop()

Stop stream sender

type Result

type Result struct {
	ID       string
	Duration time.Duration
	Err      error
}

Result struct for return send emailField result

type SMTPserver

type SMTPserver struct {
	Host     string
	Port     int
	Username string
	Password string
}

SMTPserver use for send email from server

Directories

Path Synopsis
Example use: sendemail -V -f from@domain.tld -t to@domain.tld -s "Hello subject!" -m "Hello, world!" sendemail -f from@domain.tld -t to@domain.tld -s "Hello subject!" -html ./message.html -amp ./amp.html -txt ./message.txt
Example use: sendemail -V -f from@domain.tld -t to@domain.tld -s "Hello subject!" -m "Hello, world!" sendemail -f from@domain.tld -t to@domain.tld -s "Hello subject!" -html ./message.html -amp ./amp.html -txt ./message.txt

Jump to

Keyboard shortcuts

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