gomail

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 6 Imported by: 0

README

Gomail

Gomail is a Golang module that provides an abstraction for sending template emails to users in Golang applications. It allows users to provide their SMTP credentials and a folder for the email templates, and then sends the email using the specified template.

Installation

To install Gomail, use the following command:

    go get github.com/rhaqim/gomail

Usage

To use Gomail, you need to import the module and create a new instance of the Gomail struct. You can then use the SendEmail method to send an email.

    package main

    import (
        "log"
        "github.com/rhaqim/gomail"
    )

    func main() {
        auth := gomail.EmailAuthConfig{
            Host:     "smtp.gmail.com",
            Port:     587,
            Username: "user",
            Password: "password",
            From:     "me@gmail.com",
        }

        templateDir := "templates"

        g := gomail.NewGomail(auth, templatesDir)

        App(g)

    }

    func App(mail gomail.Gomail) {

        email := &gomail.Email{
            Recipients:       []string{"recipient1e@gmail.com", "recipiente2@gmail.com"},
            Subject:          "Hello",
            Body:             "Hello, this is a test email",
            TemplateFileName: "hello.html",
            Data:             map[string]interface{}{"Title": "Hello", "Body": "Hello, this is a test email"},
        }

        err := mail.SendEmail(email)
        if err != nil {
            log.Fatal(err)
        }
    }

The template file should be in the specified template directory and should be an HTML file. The template file should contain the email body and any other information that you want to include in the email.

    <!DOCTYPE html>
    <html>
        <head>
            <title>My Go Template</title>
        </head>
        <body>
            <h1>{{.Title }}</h1>
            <p>{{ .Body }}</p>
        </body>
    </html>

Contributing

To contribute to Gomail, fork the repository and create a new branch. Once you have made your changes, submit a pull request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Email

type Email struct {
	Recipients       []string               // Recipients of the email
	Subject          string                 // Subject of the email
	Body             string                 // Body of the email
	TemplateFileName string                 // Name of the template file
	Data             map[string]interface{} // Data to be parsed with the template
}

Email is a struct that contains the email data.

If data is provided, the template will be parsed with the data. Otherwise, the template will be parsed with the struct below. Ensure that the data keys match the template placeholders.

struct {
	Title string // Title of the email
	Body  string // Body of the email
}

type EmailAuthConfig

type EmailAuthConfig struct {
	Host     string // Host of the email provider
	Port     int    // Port of the email provider
	Username string // Username of the email provider
	Password string // Password of the email provider
	From     string // From address as the sender of the email
}

EmailAuthConfig is a struct that contains the email authentication data. You can get the host, port, username and password from the email provider.

Example:

EmailAuthConfig{
	Host:     "smtp.gmail.com",
	Port:     587,
	Username: "user",
	Password: "password",
	From:     "me@gmail.com",
}

type GoemailConfig

type GoemailConfig struct {
	Config      EmailAuthConfig
	TemplateDir string
	Log         bool
	// contains filtered or unexported fields
}

func (*GoemailConfig) SendEmail

func (g *GoemailConfig) SendEmail(mail *Email) error

type Gomail

type Gomail interface {

	// SendEmail sends an email to the specified recipients with the given subject, template, and data.
	SendEmail(mail *Email) error
	// contains filtered or unexported methods
}

Goemail is an interface for sending emails and parsing templates.

func NewGomail

func NewGomail(auth EmailAuthConfig, templateDir string) Gomail

NewGomail creates a new Gomail instance with the given email authentication configuration and template directory.

type ValidateKind

type ValidateKind string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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