Categorygithub.com/Supme/smtpSender
modulepackage
0.1.2
Repository: https://github.com/supme/smtpsender.git
Documentation: pkg.go.dev

# README

smtpSender

Go Report Card

See cmd/sendemail.go file for example

go build -o sendemail sendemail.go
sendemail -h
sendemail -f [email protected] -t [email protected] -s "Hello subject!" -m "Hello, world!"
sendemail -f [email protected] -t [email protected] -s "Hello subject!" -html ./message.html -amp ./amp.html -txt ./message.txt

Send email

bldr := &smtpSender.Builder{
	From: "Sender <[email protected]>",
	To: "Me <[email protected]>",
	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: "[email protected]",
	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", "[email protected]")
    bldr.SetTo("Me", "[email protected]")
    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", "[email protected]")
	bldr.SetTo("Me", "[email protected]")
	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())

# Packages

Example use: sendemail -V -f [email protected] -t [email protected] -s "Hello subject!" -m "Hello, world!" sendemail -f [email protected] -t [email protected] -s "Hello subject!" -html ./message.html -amp ./amp.html -txt ./message.txt.

# Functions

NewBuilder return new Builder.
NewDelimitWriter get writer, delimit bytes and count through which to add a delimit bytes.
NewEmailPipe return new chanel for stream send Deprecated: use NewPipe.
NewPipe return new stream sender pipe.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

Builder helper for create email.
Config profile for sender pool.
Connect to smtp server from configured interface.
DelimitWriter Writer with delimiter bytes.
Email struct.
Pipe email pipe for send email.
Result struct for return send emailField result.
SMTPserver use for send email from server.