email

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2021 License: MIT Imports: 19 Imported by: 5

README

email

A smtp email sender with implict tls (net/smtp uses explict StartTLS, which is vulnerable).

Build Status Coverage Status Go Report Card Documentation

Install

$ go get github.com/lovego/email

Documentation

Index

Examples

Constants

View Source
const (
	// BEncoding represents Base64 encoding scheme as defined by RFC 2045.
	BEncoding = WordEncoder('b')
	// QEncoding represents the Q-encoding scheme as defined by RFC 2047.
	QEncoding = WordEncoder('q')
)
View Source
const (
	MIMEVersion             = "MIME-Version"
	ContentType             = "Content-Type"
	ContentTransferEncoding = "Content-Transfer-Encoding"
	ContentDisposition      = "Content-Disposition"
	ContentDescription      = "Content-Description"

	From = "From"
	To   = "To"
	Cc   = "Cc"
	Bcc  = "Bcc"

	Subject = "Subject"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}
Example
client := testClient()
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
	wg.Add(1)
	go func(i int) {
		sendTestMessage(client, i)
		wg.Done()
	}(i)
}
wg.Wait()
sendTestMessage(client, 4)
Output:

Example (Multipart)
client := testClient()
sendTestMultipartMessage(client)
Output:

func NewClient

func NewClient(urlStr string) (*Client, error)

func (*Client) Send

func (c *Client) Send(ctx context.Context, msg *Message) (err error)

type Conn

type Conn struct {
	*smtp.Client
	// contains filtered or unexported fields
}

func Dial

func Dial(ctx context.Context, addr string, auth smtp.Auth) (*Conn, error)

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, from string, to []string, msg []byte) (err error)
Example
const c = "MIME-Version: 1.0\n" +
	"From: " + testFrom + "\n" +
	"To: " + testTo + "\n" +
	"Subject: test subject\n" +
	"\n" +
	"xxx\r.中文\n" +
	"test body.\n" +
	"test body2.\n"
testConnSend(c)
Output:

<nil>
Example (Message_rfc822)
const c = "MIME-Version: 1.0\n" +
	"From: " + testFrom + "\n" +
	"To: " + testTo + "\n" +
	"Subject: test message/rfc822\n" +
	"Content-Type: message/rfc822\n" +
	"\n" +
	"From: " + testFrom + "\n" +
	"To: " + testTo + "\n" +
	"Subject: test subject-message/rfc822\n" +
	"\n" +
	"xxx\r.中文\n" +
	"test body.\n" +
	"test body2.\n"
testConnSend(c)
Output:

<nil>
type Header struct {
	Name   string
	Values []string
}

type LineWriter

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

LineWriter add "\n" after every 76 bytes.

func NewLineWriter

func NewLineWriter(w io.Writer) *LineWriter

func (*LineWriter) Write

func (lw *LineWriter) Write(b []byte) (total int, err error)

type Message

type Message struct {
	Headers   []Header
	Body      []byte    // body for Content-Type: plain, image, audio, video or application.
	BodyParts []Message // body for Content-Type: multipart or message.
}

Message implements Internet Message as defined in RFC2822 and RFC2045 ~ RFC2049.

Example
m := Message{
	Headers: []Header{
		{Name: From, Values: []string{"测试来源<" + testFrom + ">"}},
		{Name: To, Values: []string{"测试收件人<" + testTo + ">"}},
		{Name: Subject, Values: []string{"测试主题:" + testContent}},
	},
	Body: []byte("测试内容:" + testContent),
}
recipients, err := getRecipients(m.Headers)
if err != nil {
	fmt.Println(err)
	return
}
body, err := m.Bytes()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(string(body))

if err := testConn().Send(context.Background(), testFrom, recipients, body); err != nil {
	fmt.Println(err)
}
Output:

From: =?utf-8?b?5rWL6K+V5p2l5rqQ?= <xiaomei-go@qq.com>
To: =?utf-8?b?5rWL6K+V5pS25Lu25Lq6?= <applejava@qq.com>
Subject: =?utf-8?b?5rWL6K+V5Li76aKY77ya5oiR5Lus5Lit5Zu95Lq65rCR5LuO5q2k56uZ56uL?=
 =?utf-8?b?6LW35p2l5LqG77yBV2UgQ2hpbmVzZSBQZW9wbGUgU3RhbmQgVXAgRnJvbSBO?=
 =?utf-8?b?b3ch?=
MIME-Version: 1.0
Content-Transfer-Encoding: base64

5rWL6K+V5YaF5a6577ya5oiR5Lus5Lit5Zu95Lq65rCR5LuO5q2k56uZ56uL6LW35p2l5LqG77yB
V2UgQ2hpbmVzZSBQZW9wbGUgU3RhbmQgVXAgRnJvbSBOb3ch

func (*Message) Bytes

func (m *Message) Bytes() ([]byte, error)

Bytes is the most important entrance func, it calls all the logic to convert a Message to bytes.

func (*Message) IsMultipart

func (m *Message) IsMultipart() bool

IsMultipart return if this is a multipart message.

type WordDecoder

type WordDecoder struct {
	// CharsetReader, if non-nil, defines a function to generate
	// charset-conversion readers, converting from the provided
	// charset into UTF-8.
	// Charsets are always lower-case. utf-8, iso-8859-1 and us-ascii charsets
	// are handled by default.
	// One of the CharsetReader's result values must be non-nil.
	CharsetReader func(charset string, input io.Reader) (io.Reader, error)
}

A WordDecoder decodes MIME headers containing RFC 2047 encoded-words.

func (*WordDecoder) Decode

func (d *WordDecoder) Decode(word string) (string, error)

Decode decodes an RFC 2047 encoded-word.

func (*WordDecoder) DecodeHeader

func (d *WordDecoder) DecodeHeader(header string) (string, error)

DecodeHeader decodes all encoded-words of the given string. It returns an error if and only if CharsetReader of d returns an error.

type WordEncoder

type WordEncoder byte

A WordEncoder is an RFC 2047 encoded-word encoder.

func (WordEncoder) Encode

func (e WordEncoder) Encode(charset, s string) string

Encode returns the encoded-word form of s. If s is ASCII without special characters, it is returned unchanged. The provided charset is the IANA charset name of s. It is case insensitive.

Jump to

Keyboard shortcuts

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