atmail

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 12 Imported by: 1

README

atmail

aiteung email helper for gmail. Authentication using access token token.json

Example sending email


package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"log"
	"os"

	"github.com/aiteung/atmail"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/gmail/v1"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	b, err := os.ReadFile("client_secret.json")
	if err != nil {
		log.Fatalf("Unable to read client secret file: %v", err)
	}

	// If modifying these scopes, delete your previously saved token.json.
	config, err := google.ConfigFromJSON(b, gmail.GmailSendScope)
	if err != nil {
		log.Fatalf("Unable to parse client secret file to config: %v", err)
	}
	client := atmail.GetClient(config)

	srv, err := gmail.NewService(ctx, option.WithHTTPClient(client))
	if err != nil {
		log.Fatalf("Unable to retrieve Gmail client: %v", err)
	}

	var message gmail.Message

	// Compose the message
	messageStr := []byte(
		"From: youremail@gmail.com\r\n" +
			"To: awangga@gmail.com\r\n" +
			"Subject: My first Gmail API message\r\n\r\n" +
			"Message body goes here!")

	// Place messageStr into message.Raw in base64 encoded format
	message.Raw = base64.URLEncoding.EncodeToString(messageStr)

	// Send the message
	_, err = srv.Users.Messages.Send("me", &message).Do()
	if err != nil {
		log.Printf("Error: %v", err)
	} else {
		fmt.Println("Message sent!")
	}

}

Dev

set env

GOPROXY=proxy.golang.org

git tag v0.0.1
git push origin --tags
go list -m github.com/aiteung/atmail@v0.0.1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateGmailMessage added in v0.0.3

func GenerateGmailMessage(msg EmailMessage) string

func GetGmailService added in v0.0.2

func GetGmailService(secret string, accesstokenfile string, scope ...string) (srv *gmail.Service)

Types

type EmailMessage added in v0.0.3

type EmailMessage struct {
	From        string           `json:"from,omitempty" bson:"from,omitempty"`
	To          string           `json:"to,omitempty" bson:"to,omitempty"`
	Subject     string           `json:"subject,omitempty" bson:"subject,omitempty"`
	Body        string           `json:"body,omitempty" bson:"body,omitempty"`
	Attachments []FileAttachment `json:"attachments,omitempty" bson:"attachments,omitempty"`
}

type FileAttachment added in v0.0.3

type FileAttachment struct {
	MIMEType string `json:"mimetype,omitempty" bson:"filemimetype,omitempty"`
	Name     string `json:"name,omitempty" bson:"filename,omitempty"`
	Base64   string `json:"base64,omitempty" bson:"base64string,omitempty"`
}

Jump to

Keyboard shortcuts

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