gmail

package module
v0.0.0-...-3ec3141 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2017 License: BSD-3-Clause Imports: 9 Imported by: 3

README

gmail

A simple Go library for sending emails from a Gmail account.

NB: The attachment code was inspired by scorredoira's email and full credit goes to him.

package main

import "github.com/SlyMarbo/gmail"

func main() {
	email := gmail.Compose("Email subject", "Email body")
	email.From = "username@gmail.com"
	email.Password = "password"

	// Defaults to "text/plain; charset=utf-8" if unset.
	email.ContentType = "text/html; charset=utf-8"

	// Normally you'll only need one of these, but I thought I'd show both.
	email.AddRecipient("recipient@example.com")
	email.AddRecipients("another@example.com", "more@example.com")

	err := email.Send()
	if err != nil {
		// handle error.
	}
}

Note:

If you have problems with authentication, be sure to check your password settings. While developing the package, I had forgotten that I have application-specific passwords enabled on my Google account, so my account password wasn't working; I had to sign into my account and create an application-specific password for the package and use that.

Documentation

Overview

Package gmail is a simple Go library for sending emails from a Gmail account.

NB: The attachment code was inspired by scorredoira's email (https://github.com/scorredoira/email) and full credit goes to him.

package main

import "github.com/SlyMarbo/gmail"

func main() {
	email := gmail.Compose("Email subject", "Email body")
	email.From = "username@gmail.com"
	email.Password = "password"

	// Defaults to "text/plain; charset=utf-8" if unset.
	email.ContentType = "text/html; charset=utf-8"

	// Normally you'll only need one of these, but I thought I'd show both.
	email.AddRecipient("recipient@example.com")
	email.AddRecipients("another@example.com", "more@example.com")

	err := email.Send()
	if err != nil {
		// handle error.
	}
}

Note:

If you have problems with authentication, be sure to check your password settings. While developing the package, I had forgotten that I have application-specific passwords enabled on my Google account, so my account password wasn't working; I had to sign into my account and create an application-specific password for the package and use that.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Email

type Email struct {
	Subject, Body string
	From          string
	Name          string
	Password      string
	ContentType   string
	To            []string
	Attachments   map[string][]byte
}

Email represents a single message, which may contain attachments.

func Compose

func Compose(Subject, Body string) *Email

Compose begins a new email, filling the subject and body, and allocating memory for the list of recipients and the attachments.

func (*Email) AddRecipient

func (e *Email) AddRecipient(Recipient string)

AddRecipient adds a single recipient.

func (*Email) AddRecipients

func (e *Email) AddRecipients(Recipients ...string)

AddRecipients adds one or more recipients.

func (*Email) Attach

func (e *Email) Attach(Filename string) error

Attach takes a filename and adds this to the message. Note that since only the filename is stored (and not its path, for privacy reasons), multiple files in different directories but with the same filename and extension cannot be sent.

func (*Email) Bytes

func (e *Email) Bytes() []byte

func (*Email) Send

func (e *Email) Send() error

Send sends the email, returning any error encountered.

Jump to

Keyboard shortcuts

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