directEmail

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

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

Go to latest
Published: Feb 2, 2018 License: MIT Imports: 21 Imported by: 0

README

Go Report Card

directEmail

Deprecate: later use https://github.com/Supme/smtpSender

Example

package main

import (
	"github.com/supme/directEmail"
	"time"
	"fmt"
)

func main() {
	email := directEmail.New()

	// if use socks5 proxy
	email.Ip = "socks://123.124.125.126:8080"
	// or with auth
	email.Ip = "socks://user:password@123.124.125.126:8080"
    
	// if use specified interface
	email.Ip = "192.168.0.10"

	// if use NAT
	email.MapIp = map[string]string {
		"192.168.0.10": "31.33.34.35",
	}

	// if left blank, then auto resolved (for socks use IP for connecting server)
	email.Host = "resolv.name.myhost.com"

	email.FromEmail = "sender@example.com"
	email.FromName = "Sender name"

	email.ToEmail = "user@example.com"
	email.ToName = "Reciver name"

	// add extra headers if need
	email.Header(fmt.Sprintf("Message-ID: <test_message_%d>", time.Now().Unix()))
	email.Header("Content-Language: ru")

	email.Subject = "Тест отправки email"

	// plain text version
	email.TextPlain(`Текст моего TEXT сообщения`)

	// html version
	email.TextHtml(`
<h2>My email</h2>
<p>Текст моего HTML сообщения</p>
	`)

	// or html version with related files
	email.TextHtmlWithRelated(`
<h2>My email</h2>
<p>Текст моего HTML with related files сообщения</p>
<p>Картинка: <img src="cid:myImage.jpg" width="500px" height="250px" border="1px" alt="My image"/></p>
	`,
		"/path/to/attach/myImage.jpg",
	)

	// attach file if need
	email.Attachment("/path/to/attach/file.jpg")
    
	// Render email message
	email.Render()
	
	// if dkimSelector not blank, then add DKIM signature to message
	email.RenderWithDkim("myDKIMselector", []byte("DKIMprivateKey"))
	
	print("\n", string(email.GetRawMessageString()), "\n\n\n")

	err := email.Send()
	if err != nil {
		print("Send email with error:", err.Error())
	}

	// or send from SMTP server use login and password
	err := email.SendThroughServer("smtp.server.tld", 587, "username", "password")
	if err != nil {
		print("Send email with error:\n", err.Error(), "\n")
	}
}

Documentation

Overview

Package directEmail support direct send email from selected interface include SOCKS5 proxy server

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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 {
	// Ip contains local IP address wich use for send email
	// if blank use default interface
	// if use socks5 proxy "socks://123.124.125.126:8080"
	// and with auth "socks://user:password@123.124.125.126:8080"
	Ip string
	// Host is host name
	// if blank use DNS resolv for field fill
	Host string
	// Port SMTP server port
	Port uint16
	// MapIp use for translate local IP to global if NAT
	// if use Socks server translate IP SOCKS server to real IP
	MapIp map[string]string
	// FromEmail sender email (Required)
	FromEmail string
	// FromName sender name
	FromName string
	// ToEmail recipient email (Required)
	ToEmail string
	// ToName recipient name
	ToName string
	// Subject email subject
	Subject string
	// contains filtered or unexported fields
}

A Email contains options for send email.

func New

func New() Email

New returns a new Email instance for create and send email

func (*Email) Attachment

func (slf *Email) Attachment(files ...string) (err error)

Attachment attach files to email message

func (*Email) GetRawMessageBytes

func (slf *Email) GetRawMessageBytes() []byte

GetRawMessageBytes

func (*Email) GetRawMessageString

func (slf *Email) GetRawMessageString() string

GetRawMessageString

func (*Email) Header

func (slf *Email) Header(headers ...string)

Header add extra headers to email

func (*Email) Render

func (slf *Email) Render() (err error)

Render added text/html, text/plain, attachments part to raw view

func (*Email) RenderWithDkim

func (slf *Email) RenderWithDkim(dkimSelector string, dkimPrivateKey []byte) (err error)

Render added text/html, text/plain, attachments part to raw view If dkim selector not blank add DKIM signature email Generate private key:

openssl genrsa -out /path/to/key/example.com.key 2048

Generate public key:

openssl rsa -in /path/to/key/example.com.key -pubout

Add public key to DNS myselector._domainkey.example.com TXT record

k=rsa; p=MIGfMA0GC...

func (*Email) Send

func (self *Email) Send() error

Send email directly

func (*Email) SendThroughServer

func (self *Email) SendThroughServer(host string, port uint16, username, password string) error

SendThroughServer send email from SMTP server

func (*Email) SetInterfaceByIp

func (slf *Email) SetInterfaceByIp(ip string)

SetInterfaceByIp set IP from which the sending will be made

func (*Email) SetInterfaceByName

func (slf *Email) SetInterfaceByName(name string) error

SetInterfaceByName set interface from which the sending will be made

func (*Email) SetInterfaceDefault

func (slf *Email) SetInterfaceDefault(ip string)

SetInterfaceDefault set default interface for sending

func (*Email) SetInterfaceSocks

func (slf *Email) SetInterfaceSocks(server string, port int)

// SetInterfaceBySocks set SOCKS server through which the sending will be made

func (*Email) SetMapGlobalIpForLocal

func (slf *Email) SetMapGlobalIpForLocal(globalIp, localIp string)

SetMapGlobalIpForLocal set glibal IP for local IP address

func (*Email) SetRawMessageBytes

func (slf *Email) SetRawMessageBytes(data []byte) error

SetRawMessageBytes

func (*Email) SetRawMessageString

func (slf *Email) SetRawMessageString(data string) error

SetRawMessageString

func (*Email) TextHtml

func (slf *Email) TextHtml(content string) (err error)

TextHtml add text/html content to email

func (*Email) TextHtmlWithRelated

func (slf *Email) TextHtmlWithRelated(content string, files ...string) (err error)

TextHtmlWithRelated add text/html content with related file.

Example use file in html

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

func (*Email) TextPlain

func (slf *Email) TextPlain(content string) (err error)

TextPlain add text/plain content to email

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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