emails

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	UnknownDate = "unknown date"
	DateFormat  = "Monday, January 2, 2006"
)
View Source
const (
	WelcomeRE              = "Welcome to Ensign!"
	VerifyEmailRE          = "Please verify your email address to login to Ensign"
	InviteRE               = "Join Your Teammate %s on Ensign!"
	PasswordResetRequestRE = "Ensign Password Reset - Action Required"
	PasswordResetSuccessRE = "Ensign Password Reset Confirmation"
	DailyUsersRE           = "Daily PLG Report for %s: %s"
)

Email subject lines

Variables

View Source
var (
	ErrMissingSubject   = errors.New("missing email subject")
	ErrMissingSender    = errors.New("missing email sender")
	ErrMissingRecipient = errors.New("missing email recipient")
	ErrUnparsable       = errors.New("could not parse email address")
)

Functions

func AttachCSV added in v0.7.0

func AttachCSV(message *mail.SGMailV3, data []byte, filename string) (err error)

AttachCSV by encoding the csv data and attaching it to the email as a file.

func AttachData added in v0.7.0

func AttachData(message *mail.SGMailV3, data []byte, filename, mimetype string) error

AttachData onto an email as a file with the specified mimetype

func AttachJSON added in v0.4.0

func AttachJSON(message *mail.SGMailV3, data []byte, filename string) (err error)

AttachJSON by marshaling the specified data into human-readable data and encode and attach it to the email as a file.

func DailyUsersEmail added in v0.5.2

func DailyUsersEmail(data DailyUsersData) (message *mail.SGMailV3, err error)

DailyUsersEmail creates an email to send to admins that reports the PLG status

func InviteEmail added in v0.5.2

func InviteEmail(data InviteData) (message *mail.SGMailV3, err error)

InviteEmail creates an email to invite a user to join an organization

func LoadAttachment added in v0.4.0

func LoadAttachment(message *mail.SGMailV3, attachmentPath string) (err error)

LoadAttachment onto email from a file on disk.

func PasswordResetRequestEmail added in v0.11.0

func PasswordResetRequestEmail(data ResetRequestData) (message *mail.SGMailV3, err error)

PasswordResetRequestEmail creates an email to send to users that have requested a password reset.

func PasswordResetSuccessEmail added in v0.11.0

func PasswordResetSuccessEmail(data EmailData) (message *mail.SGMailV3, err error)

PasswordResetSuccessEmail creates an email to send to users once their password has been reset.

func Render added in v0.4.0

func Render(name string, data interface{}) (text, html string, err error)

Render returns the text and html executed templates for the specified name and data. Ensure that the extension is not supplied to the render method.

func VerifyEmail added in v0.5.0

func VerifyEmail(data VerifyEmailData) (message *mail.SGMailV3, err error)

VerifyEmail creates an email to verify a user's email address

func WelcomeEmail added in v0.4.0

func WelcomeEmail(data WelcomeData) (message *mail.SGMailV3, err error)

WelcomeEmail creates a welcome email for a new user

Types

type Config

type Config struct {
	APIKey       string `split_words:"true" required:"false"`
	FromEmail    string `split_words:"true" default:"ensign@rotational.io"`
	AdminEmail   string `split_words:"true" default:"admins@rotational.io"`
	EnsignListID string `split_words:"true" required:"false"`
	Testing      bool   `split_words:"true" default:"false"`
	Archive      string `split_words:"true"`
}

Configures SendGrid for sending emails and managing marketing contacts.

func (Config) AdminContact added in v0.7.0

func (c Config) AdminContact() (sendgrid.Contact, error)

Parses the AdminEmail and returns a sendgrid contact for ease of mailing.

func (Config) Enabled

func (c Config) Enabled() bool

Returns true if there is a SendGrid API key available

func (Config) FromContact added in v0.7.0

func (c Config) FromContact() (sendgrid.Contact, error)

Parses the FromEmail and returns a sendgrid contact for ease of mailing.

func (Config) MustAdminContact added in v0.7.0

func (c Config) MustAdminContact() sendgrid.Contact

func (Config) MustFromContact added in v0.7.0

func (c Config) MustFromContact() sendgrid.Contact

func (Config) Validate

func (c Config) Validate() (err error)

From and admin emails are required if the SendGrid API is enabled.

type DailyUsersData added in v0.5.2

type DailyUsersData struct {
	EmailData
	Date                time.Time         `json:"date"`
	InactiveDate        time.Time         `json:"inactive_date"`
	Domain              string            `json:"domain"`
	EnsignDashboardLink string            `json:"dashboard_url"`
	NewUsers            int               `json:"new_users"`
	DailyUsers          int               `json:"daily_users"`
	ActiveUsers         int               `json:"active_users"`
	InactiveUsers       int               `json:"inactive_users"`
	APIKeys             int               `json:"api_keys"`
	ActiveKeys          int               `json:"active_keys"`
	InactiveKeys        int               `json:"inactive_keys"`
	RevokedKeys         int               `json:"revoked_keys"`
	Organizations       int               `json:"organizations"`
	NewOrganizations    int               `json:"new_organizations"`
	Projects            int               `json:"projects"`
	NewProjects         int               `json:"new_projects"`
	NewAccounts         []*NewAccountData `json:"-"`
}

DailyUsersData is used to complete the daily users email template

func (DailyUsersData) FormattedDate added in v0.5.2

func (d DailyUsersData) FormattedDate() string

func (DailyUsersData) FormattedInactiveDate added in v0.5.2

func (d DailyUsersData) FormattedInactiveDate() string

func (DailyUsersData) NewAccountsCSV added in v0.7.0

func (d DailyUsersData) NewAccountsCSV() (_ []byte, err error)
Example
package main

import (
	"fmt"

	"github.com/rotationalio/ensign/pkg/utils/emails"
)

func main() {
	data := emails.DailyUsersData{
		NewAccounts: []*emails.NewAccountData{
			{
				Name:          "Wiley E. Coyote",
				Email:         "wiley@acme.co",
				EmailVerified: true,
				Role:          "owner",
				LastLogin:     "2023-07-08T19:21:39Z",
				Created:       "2023-07-08T12:02:52Z",
				Organization:  "Acme, Inc.",
				Domain:        "acme.co",
				Projects:      3,
				APIKeys:       7,
				Invitations:   3,
				Users:         2,
			},
			{
				Name:          "Rod P. Runner",
				Email:         "rod@acme.co",
				EmailVerified: false,
				Role:          "member",
				LastLogin:     "2023-07-08T13:12:42Z",
				Created:       "2023-07-08T12:02:52Z",
				Organization:  "Acme, Inc.",
				Domain:        "acme.co",
				Projects:      3,
				APIKeys:       7,
				Invitations:   3,
				Users:         2,
			},
			{
				Name:          "Julie Smith Lee",
				Email:         "jlee@foundations.io",
				EmailVerified: true,
				Role:          "owner",
				LastLogin:     "2023-07-08T08:22:27Z",
				Created:       "2023-07-08T08:21:01Z",
				Organization:  "Foundations",
				Domain:        "foundations.io",
				Projects:      1,
				APIKeys:       1,
				Invitations:   8,
				Users:         1,
			},
		},
	}

	csv, _ := data.NewAccountsCSV()
	fmt.Println(string(csv))
}
Output:

name,email,email_verified,role,last_login,created,organization,domain,projects,apikeys,users,invitations
Wiley E. Coyote,wiley@acme.co,true,owner,2023-07-08T19:21:39Z,2023-07-08T12:02:52Z,"Acme, Inc.",acme.co,3,7,2,3
Rod P. Runner,rod@acme.co,false,member,2023-07-08T13:12:42Z,2023-07-08T12:02:52Z,"Acme, Inc.",acme.co,3,7,2,3
Julie Smith Lee,jlee@foundations.io,true,owner,2023-07-08T08:22:27Z,2023-07-08T08:21:01Z,Foundations,foundations.io,1,1,1,8

func (DailyUsersData) TabTable added in v0.5.2

func (d DailyUsersData) TabTable() string
Example
package main

import (
	"fmt"

	"github.com/rotationalio/ensign/pkg/utils/emails"
)

func main() {
	dailyUsersData := emails.DailyUsersData{
		Domain:           "ensign.local",
		NewUsers:         2,
		DailyUsers:       8,
		ActiveUsers:      102,
		InactiveUsers:    3,
		APIKeys:          58,
		ActiveKeys:       52,
		InactiveKeys:     6,
		RevokedKeys:      12,
		Organizations:    87,
		NewOrganizations: 1,
		Projects:         87,
		NewProjects:      1,
	}
	fmt.Println(dailyUsersData.TabTable())
}
Output:

New Users:          2    Daily Users:        8
Active Users:       102  Inactive Users:     3
API Keys:           58   Revoked API Keys:   12
Active API Keys:    52   Inactive API Keys:  6
New Organizations:  1    Organizations:      87
New Projects:       1    Projects:           87

type EmailData added in v0.4.0

type EmailData struct {
	Subject   string           `json:"-"`
	Sender    sendgrid.Contact `json:"-"`
	Recipient sendgrid.Contact `json:"-"`
}

EmailData includes data fields that are common to all the email builders such as the subject and sender/recipient information.

func (EmailData) Build added in v0.4.0

func (e EmailData) Build(text, html string) (msg *mail.SGMailV3, err error)

Build creates a new email from pre-rendered templates.

func (EmailData) Validate added in v0.4.0

func (e EmailData) Validate() error

Validate that all required data is present to assemble a sendable email.

type EmailManager

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

EmailManager allows a server to send rich emails using the SendGrid service.

func New

func New(conf Config) (m *EmailManager, err error)

New email manager with the specified configuration.

func (*EmailManager) AddContact added in v0.4.0

func (m *EmailManager) AddContact(contact *sg.Contact, listIDs ...string) (err error)

AddContact adds a contact to SendGrid, adding them to the Ensign marketing list if it is configured. This is an upsert operation so existing contacts will be updated. The caller can optionally specify additional lists that the contact should be added to. If no lists are configured or specified, then the contact is added or updated in SendGrid but is not added to any marketing lists.

func (*EmailManager) Send

func (m *EmailManager) Send(message *sgmail.SGMailV3) (err error)

type InviteData added in v0.5.2

type InviteData struct {
	EmailData
	Email       string
	InviterName string
	OrgName     string
	Role        string
	InviteURL   string
}

InviteData is used to complete the invite email template

type NewAccountData added in v0.7.0

type NewAccountData struct {
	Name          string `json:"name"`           // name of the user
	Email         string `json:"email"`          // email address of the user
	EmailVerified bool   `json:"email_verified"` // if the user has verified their email address
	Role          string `json:"role"`           // role of the user in the organization
	LastLogin     string `json:"last_login"`     // timestamp the user logged in
	Created       string `json:"created"`        // timestamp the user was added to the org
	Organization  string `json:"organization"`   // name of the organization (workspace)
	Domain        string `json:"domain"`         // domain of the organization
	Projects      int    `json:"projects"`       // number of projects in the organization
	APIKeys       int    `json:"apikeys"`        // number of api keys in the organization
	Users         int    `json:"users"`          // number of users in the organization
	Invitations   int    `json:"invitations"`    // number of user invitations in the organization
}

NewAccountData describes user accounts that were created in the last 24 hours. An account is an instance of a user assigned to an organization. This includes the cases where a user registers and creates an organization, where a user is invited to an existing organization and creates an account, and where an existing user is invited to a second organization. The organization data is from the perspective of the entire organization not just the users' apikeys, projects, invitations, etc.

type ResetRequestData added in v0.11.0

type ResetRequestData struct {
	EmailData
	ResetURL string
}

ResetRequestData is used to complete the password reset request email template

type SendGridClient

type SendGridClient interface {
	Send(email *sgmail.SGMailV3) (*rest.Response, error)
}

SendGridClient is an interface that can be implemented by live email clients to send real emails or by mock clients for testing.

type VerifyEmailData added in v0.5.0

type VerifyEmailData struct {
	EmailData
	FullName  string
	VerifyURL string
}

VerifyEmailData is used to complete the verify email template

type WelcomeData added in v0.4.0

type WelcomeData struct {
	EmailData
	FirstName    string
	LastName     string
	Email        string
	Organization string
	Domain       string
}

WelcomeData is used to complete the welcome email template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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