atmail

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 13 Imported by: 1

README

atmail

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

Example sending email

package main

import (
    "encoding/base64"
    "fmt"
    "log"
    "net/http"
    "os"

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

func filereadmime(fname string) (fileData, fileMIMEType string) {
    fileBytes, err := os.ReadFile(fname)
    if err != nil {
        log.Fatalf("Error: %v", err)
    }
    fileMIMEType = http.DetectContentType(fileBytes)
    fileData = base64.StdEncoding.EncodeToString(fileBytes)
    return fileData, fileMIMEType
}

func main() {
    msg := &atmail.EmailMessage{
        From:    "Penerbit Buku Pedia<penerbit@bukupedia.co.id>",
        To:      "awangga@gmail.com",
        Subject: "subjek email",
        Body:    "ini bodi isinya ya <b>luar biasa</b>",
    }

    filename := "README.md"
    fileData, fileMIMEType := filereadmime(filename) //"text/plain; charset=utf-8"
    attachment := &atmail.FileAttachment{
        Name:     filename,
        MIMEType: fileMIMEType,
        Base64:   fileData,
    }
    msg.Attachments = append(msg.Attachments, *attachment)

    srv := atmail.GetGmailService("client_secret.json", "token.json", gmail.GmailSendScope)
    var message gmail.Message
    message.Raw = atmail.Base64Message(*msg)
    // Send the message
    resp, err := srv.Users.Messages.Send("me", &message).Do()
    if err != nil {
        log.Printf("Error: %v", err)
    } else {
        fmt.Println(resp.LabelIds)
    }
}

Dev

set env

GOPROXY=proxy.golang.org

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64Message added in v0.0.4

func Base64Message(msg EmailMessage) string

func GetGmailService added in v0.0.2

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

func SetMIMEandNameifEmpty added in v0.0.8

func SetMIMEandNameifEmpty(msg *EmailMessage)

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:"mimetype,omitempty"`
	Name     string `json:"name,omitempty" bson:"name,omitempty"`
	Base64   string `json:"base64,omitempty" bson:"base64,omitempty"`
}

type Queue added in v0.0.9

type Queue struct {
	ID       primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	Session  string             `json:"session,omitempty" bson:"session,omitempty"`
	Function string             `json:"function,omitempty" bson:"function,omitempty"`
	Scope    []string           `json:"scope,omitempty" bson:"scope,omitempty"`
	Base64   string             `json:"base64,omitempty" bson:"base64,omitempty"`
}

type Success added in v0.0.9

type Success struct {
	ID       primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
	LabelIds []string           `json:"labels,omitempty" bson:"labels,omitempty"`
	Queue    Queue              `json:"queue,omitempty" bson:"queue,omitempty"`
}

Jump to

Keyboard shortcuts

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