smtp

package module
v1.0.0-1a Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-smtp

go-smtp is a Go library for sending emails via SMTP. It provides an easy-to-use interface for sending emails with support for custom authentication, TLS, and more.

Installation

To install the library, use go get:

go get github.com/dexterdmonkey/go-smtp

Usage

Here's a simple example of how to use the go-smtp library:

package main

import (
	"fmt"

	"github.com/dexterdmonkey/go-smtp"
)

func main() {
	var mail smtp.Interface

	SMTPUser := "your@email.com"
	SMTPPassword := "yourpassword"
	SMTPHost := "smtp.email.com"
	SMTPPort := 587

	mail, err := smtp.New(
		SMTPUser,
		SMTPPassword,
		SMTPHost,
		SMTPPort,
	)

	if err != nil {
		fmt.Println("error; ", err.Error())
		return
	}

	err = mail.SendMail(smtp.Email{
		To:      []string{"recipient1@email.com"},
		Subject: "subject",
		Body:    "body",
	})

	if err != nil {
		fmt.Println("error; ", err.Error())
		return
	}

	err = mail.SendMail(smtp.Email{
		To:      []string{"recipient2@email.com"},
		Subject: "subject",
		Body:    "body",
	})

	if err != nil {
		fmt.Println("error; ", err.Error())
		return
	}

	fmt.Println("Success")
}

API

Types
Interface

The Interface defines the methods available for the SMTP client:

type Interface interface {
	GetSenderAddress() string
	GetPassword() string
	GetHost() string
	GetPort() int
	ParseBody(body string, parameters map[string]interface{}) string
	SendMail(email Email) error
}
Email

The Email struct represents an email to be sent:

type Email struct {
	To      []string
	Cc      []string
	Bcc     []string
	Subject string
	Body    string
}
Functions
New

Creates a new SMTP client:

func New(senderAddress, password, host string, port int) (*SMTP, error)
GetSenderAddress

Returns the sender address:

func (c *SMTP) GetSenderAddress() string
GetPassword

Returns the password:

func (c *SMTP) GetPassword() string
GetHost

Returns the host:

func (c *SMTP) GetHost() string
GetPort

Returns the port:

func (c *SMTP) GetPort() int
GetClient

Returns an authenticated SMTP client:

func (c *SMTP) GetClient() (*smtp.Client, error)
SendMail

Sends an email:

func (c *SMTP) SendMail(email Email) error
ParseBody

Parses the body of the email with the provided parameters:

func (c *SMTP) ParseBody(body string, parameters map[string]interface{}) string

License

This library is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Email

type Email struct {
	To      []string
	Cc      []string
	Bcc     []string
	Subject string
	Body    string
}

Email struct represents the email structure with recipients, subject, and body.

type Interface

type Interface interface {
	GetSenderAddress() string
	GetPassword() string
	GetHost() string
	GetPort() int
	ParseBody(body string, parameters map[string]interface{}) string
	SendMail(email Email) error
}

Interface defines the methods that any SMTP client must implement.

type SMTP

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

SMTP struct represents the SMTP client with necessary credentials and configurations.

func New

func New(senderAddress, password, host string, port int) (*SMTP, error)

New initializes and returns a new SMTP client.

func (*SMTP) GetClient

func (c *SMTP) GetClient() (*smtp.Client, error)

GetClient initializes and returns an SMTP client.

func (*SMTP) GetHost

func (c *SMTP) GetHost() string

GetHost returns the host for the SMTP client.

func (*SMTP) GetPassword

func (c *SMTP) GetPassword() string

GetPassword returns the password for the SMTP client.

func (*SMTP) GetPort

func (c *SMTP) GetPort() int

GetPort returns the port for the SMTP client as an integer.

func (*SMTP) GetSenderAddress

func (c *SMTP) GetSenderAddress() string

GetSenderAddress returns the sender's email address.

func (*SMTP) ParseBody

func (c *SMTP) ParseBody(body string, parameters map[string]interface{}) string

ParseBody replaces placeholders in the email body with actual values from the parameters map.

func (*SMTP) SendMail

func (c *SMTP) SendMail(email Email) error

SendMail sends an email with the specified content and recipients.

Jump to

Keyboard shortcuts

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