gmail

package module
v0.0.0-...-6a545d8 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2016 License: MIT Imports: 21 Imported by: 0

README

Gmail sender

GoDoc Build Status Coverage Status

Library to send messages using Google GMail.

You need to register the app on the Google server and get the configuration file that will be used for authorization. When you first initialize the application in the console will display the URL you need to go and get the authorization code. This code must be entered in response to the application and execution will continue. This function must be performed once the authorization keys stored in files.

Message send example
package main

import (
	"io/ioutil"
	"log"

	"github.com/mdigger/gmail"
)

func main() {
	// initialize the library
	if err := gmail.Init("config.json", "token.json"); err != nil {
		log.Fatal(err)
	}
	// create a new message
	msg, err := gmail.NewMessage(
		"Subject", // subject
		"me",      // from
		[]string{"Test User <test@example.com>"}, // to
		nil, // cc
		[]byte(`<html><p>body text</p></html>`), // message body
	)
	if err != nil {
		log.Fatal(err)
	}
	// attach file
	if err = msg.AddFile("README.md"); err != nil {
		log.Fatal(err)
	}
	// send a message
	if err := msg.Send(); err != nil {
		log.Fatal(err)
	}
}

### Registration procedure

  • Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
  • On the Add credentials to your project page, click the Cancel button.
  • At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  • Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  • Select the application type Other, enter the name "Gmail API", and click the Create button.
  • Click OK to dismiss the resulting dialog.
  • Click the Download JSON button to the right of the client ID.
  • Move this file to your working directory and rename it client_secret.json.

Documentation

Overview

Package gmail allows you to send email messages using Google GMail.

You need to register the app on the Google server and get the configuration file that will be used for authorization. When you first initialize the application in the console will display the URL you need to go and get the authorization code. This code must be entered in response to the application and execution will continue. This function must be performed once the authorization keys stored in files.

Registration procedure

1. Use wizard <https://console.developers.google.com/start/api?id=gmail> to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.

2. On the Add credentials to your project page, click the Cancel button.

3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.

4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.

5. Select the application type Other, enter the name "Gmail API", and click the Create button.

6. Click OK to dismiss the resulting dialog.

7. Click the Download JSON button to the right of the client ID.

8. Move this file to your working directory and rename it client_secret.json.

Example
package main

import (
	"log"

	"github.com/mdigger/gmail"
)

func main() {
	// initialize the library
	if err := gmail.Init("config.json", "token.json"); err != nil {
		log.Fatal(err)
	}
	// create a new message
	msg, err := gmail.NewMessage(
		"Subject",                                // subject
		"me",                                     // from
		[]string{"Test User <test@example.com>"}, // to
		nil,                                      // cc
		[]byte(`<html><p>body text</p></html>`),  // message body
	)
	if err != nil {
		log.Fatal(err)
	}
	// attach file
	if err = msg.AddFile("README.md"); err != nil {
		log.Fatal(err)
	}
	// send a message
	if err := msg.Send(); err != nil {
		log.Fatal(err)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// A function to request authorization code. As the parameter gets URL
	// to get the authorization code. In response, shall return the
	// obtained authorization code or an error.
	Prompt func(authURL string) (code string, err error)
	// The message text that will be displayed to request the authorization
	// code. Used by embedded prompt function.
	RequestMessage = "Go to the following link in your browser then type" +
		" the authorization code:"
)
View Source
var (
	// is returned if you try to send a blank message with no attached files and
	// no text messages
	ErrNoBody = errors.New("contents are undefined")
	// error not initialized the GMail
	ErrServiceNotInitialized = errors.New("gmail service not initialized")
)

Predefined error returned when trying to send a message.

Functions

func Init

func Init(config, token string) error

Init initialise GMail service. The initialization process reads a configuration file that must be created and received through the console of Google Application. Read more about the procedure of obtaining the key file can be read on Google: (https://developers.google.com/gmail/api/quickstart/go).

When you first start the console will display a string with the URL you need to go and get the key to authorize the application. This key must then be entered immediately into the app.

The resulting authorization token will be saved to the parameter file for future use.

Types

type Message

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

The Message describes an email message.

func NewMessage

func NewMessage(subject, from string, to, cc []string, body []byte) (*Message, error)

NewMessage creates a new email message to send.

The message is always sent on behalf of an authorized user, so that the from field can be empty or contain the string "me". If you set this field to a different address, it will appear in the Reply-To and when you reply to this message will use the sender's address.

You can specify email address in the following formats (supported by parsing the name and email address):

test@example.com
<test@example.com>
TestUser <test@example.com>

Parsing checks the validity of the format of the email address. You must specify at least one address to send (to or cc), or when trying to send messages will return an error.

You can use text or HTML message format is determined automatically. To guarantee that the format will be specified as HTML, consider wrapping the text with <html> tag. When adding the HTML content of the message, text version, to support legacy mail program will be added automatically. When you try to add as text message binary data will return an error. You can set nil as a parameter to empty message body.

func (*Message) AddFile

func (m *Message) AddFile(filename string) error

AddFile reads the contents of specified in the parameter file and attaches it as an attachment to the message.

func (*Message) Attach

func (m *Message) Attach(name string, data []byte) error

Attach attaches to the message an attachment as a file. Passing an empty content deletes the file with the same name if it was previously added.

func (*Message) Has

func (m *Message) Has(name string) bool

Has returns true if a file with that name was in the message as an attachment.

func (*Message) Send

func (m *Message) Send() error

Send sends the message through GMail.

Before sending, you must initialize the service by calling the Init function.

func (*Message) SetBody

func (m *Message) SetBody(data []byte) error

SetBody sets the contents of the text of the letter.

You can use text or HTML message format (is determined automatically). To guarantee that the format will be specified as HTML, consider wrapping the text with <html> tag. When adding the HTML content, text version, to support legacy mail program will be added automatically. When you try to add as message binary data will return an error. You can pass as a parameter the nil, then the message will be without a text submission.

Jump to

Keyboard shortcuts

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