Giter Club home page Giter Club logo

email's Introduction

email sending library

Build Status Coverage Status Go Reference

The library is a wrapper around the stdlib net/smtp simplifying email sending. It supports authentication, SSL/TLS, user-specified SMTP servers, content-type, charset, multiple recipients and more.

Usage example:

client := email.NewSender("localhost", email.ContentType("text/html"), email.Auth("user", "pass"))
err := client.Send("<html>some content, foo bar</html>",
	email.Params{From: "[email protected]", To: []string{"[email protected]"}, Subject: "Hello world!",
		Attachments: []string{"/path/to/file1.txt", "/path/to/file2.txt"},
		InlineImages: []string{"/path/to/image1.png", "/path/to/image2.png"},
	})

options

NewSender accepts a number of options to configure the client:

  • Port: SMTP port (default: 25)
  • TLS: Use TLS SMTP (default: false)
  • STARTTLS: Use STARTTLS (default: false)
  • InsecureSkipVerify: skip certificate verification (default: false)
  • Auth(user, password): Username and password for SMTP authentication (default: empty, no authentication)
  • LoginAuth: Use LOGIN mechanism instead of PLAIN mechanism for SMTP authentication, e.g. this is relevant for Office 365 and Outlook.com
  • ContentType: Content type for the email (default: "text/plain")
  • Charset: Charset for the email (default: "utf-8")
  • TimeOut: Timeout for the SMTP connection (default: 30 seconds)
  • Log: Logger to use (default: no logging)
  • SMTP: Set custom smtp client (default: none)

See go docs for Option functions.

Options should be passed to NewSender after the mandatory first (host) parameter.

sending email

To send email user need to create a sender first and then use Send method. The method accepts two parameters:

  • email content (string)
  • parameters (email.Params)
    type Params struct {
        From            string   // From email field
        To              []string // From email field
        Subject         string   // Email subject
        UnsubscribeLink string   // POST, https://support.google.com/mail/answer/81126 -> "Use one-click unsubscribe"
        InReplyTo       string   // Identifier for email group (category), used for email grouping
        Attachments     []string // Attachments path
        InlineImages    []string // Embedding directly to email body. Autogenerated Content-Id (cid) equals to file name
    }

See go docs for Send function.

technical details

  • Content-Transfer-Encoding set to quoted-printable
  • Custom SMTP client (smtp.Client from stdlib) can be set by user with SMTP option. In this case it will be used instead of making a new smtp client internally.
  • Logger can be set with Log option. It should implement email.Logger interface with a single Logf(format string, args ...interface{}) method. By default, "no logging" internal logger is used. This interface is compatible with the go-pkgz/lgr logger.
  • The library has no external dependencies, except for testing. It uses the stdlib net/smtp package.
  • SSL/TLS supported with TLS option (usually on port 465) as well as with STARTTLS (usually on port 587).

limitations

This library is not intended to be used for sending a lot of massive emails with low latency requirements. The intended use case is sending simple messages, like alerts, notification and so on. For example, sending alerts from a monitoring system, or for authentication-related emails, i.e. "password reset email", "verification email", etc.

email's People

Contributors

ad avatar gurza avatar paskal avatar s0x90 avatar umputun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

email's Issues

Connections pool

I think it would be useful to have reusable connections pool in case sending batch of emails.

Support StartTLS

This is often a source of confusion as people try TSL/SSL against StartTLS port. Should be not hard to do as stdlib supports StartTLS

Add support of attachments

This should be easy to do and probably we can just add another field to the sender.Params, like Attachments []string or Attachments []os.File

Sender timeOut doesn't work for TLS connections

Let's try to use passive loopback ip addresses in TestEmail_SendFailedMakeClient test for TLS Sender.
127.0.0.2 instead of 127.0.0.1
This is a general way to receive timeout network error.

func TestEmail_SendFailedMakeClient(t *testing.T) {
	// ...
	{
		s := NewSender("127.0.0.2", Port(225), TLS(true), TimeOut(time.Millisecond*200))
		err := s.Send("some text", Params{
			From:    "[email protected]",
			To:      []string{"[email protected]"},
			Subject: "subj",
		})
		require.Error(t, err)
	}
}

Run this test:

/usr/local/bin/go test -timeout 30s -run ^TestEmail_SendFailedMakeClient$ github.com/go-pkgz/email

The test will fail with panic message panic: test timed out after 30s. Whereas we expect to receive timeout network error cuz Sender timeout was set to 200ms TimeOut(time.Millisecond*200).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.