in

A Simple email sender written in pure Go Support HTML templates and attachments


Simple (but useful) email sender written in pure Go v1.17. Yes, yet another email package here!
😅

Support HTML templates and attachments.

Send HTML email

Method signature:

func (s *Sender) SendHTMLEmail(
    templatePath string,
    to []string,
    cc []string,
    subject string,
    data interface{},
    files []string,
) error

Example:

// Create a new struct for the email data.
type HTMLEmailData struct {
    Name    string
    Website string
}

// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("[email protected]", "secret", "smtp.test.com", 25)

// Send the email with an HTML template.
if err := sender.SendHTMLEmail(
    "my/templates/welcome.html", // path to the HTML template
    []string{
        "[email protected]",      // slice of emails to send
    },
    []string{
        "[email protected]", // slice of emails to send message copy
    },
    "It's a test email!",        // subject of the email
    &HTMLEmailData{
        Name:    "Vic",
        Website: "https://shostak.dev/",
    },
    []string{
        "my/files/image.jpg",    // slice of files to send
    },
); err != nil {
    // Throw error message, if something went wrong.
    return fmt.Errorf("Something went wrong: %v", err)
}

Send plain text email

Method signature:

func (s *Sender) SendPlainEmail(
    to []string,
    cc []string,
    subject string,
    data interface{},
    files []string,
) error

Example:

// Create a new SMTP sender instance with your auth params.
sender := NewEmailSender("[email protected]", "secret", "smtp.test.com", 25)

// Send the email with a plain text.
if err := sender.SendPlainEmail(
    []string{
        "[email protected]",       // slice of emails to send
    },
    []string{
        "[email protected]",  // slice of emails to send message copy
    },
    "It's a test email!",         // subject of the email
    "Here is a plain text body.", // body of the email
    []string{
        "my/files/image.jpg",     // slice of files to send
    },
); err != nil {
    // Throw error message, if something went wrong.
    return fmt.Errorf("Something went wrong: %v", err)
}


⚠
License

Apache-2.0 © Vic Shóstak & True web artisans.

GitHub

https://github.com/koddr/go-email-sender




Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Easy Material White Theme for flutter

Render and position windows as simply as you render and position tooltips in React