smtpSender

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2018 License: MIT Imports: 22 Imported by: 0

README

smtpSender

Go Report Card

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.AddTextPlain("textPlain")
bldr.AddTextHTML("<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.AddTextHTML("<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.AddTextPlain("textPlain")
	bldr.AddTextHTML("<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

This section is empty.

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 (*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) AddHeader

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

AddHeader add extra header to email

func (*Builder) AddReplyTo

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

AddReplyTo add Reply-To header

func (*Builder) AddSubjectFunc

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

AddSubjectFunc add writer function for subject

func (*Builder) AddTextFunc

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

AddTextFunc add writer function for plain text

func (*Builder) AddTextHTML

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

AddTextHTML add text/html content with related file.

Example use related file in html

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

func (*Builder) AddTextPlain

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

AddTextPlain add plain text

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)

SetDKIM sign DKIM parameters

func (*Builder) SetFrom

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

SetFrom email sender

func (*Builder) SetSubject

func (b *Builder) SetSubject(text string)

SetSubject set email subject

func (*Builder) SetTo

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

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 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
	// 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

Jump to

Keyboard shortcuts

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