smtp – petersanchez.com/x/carrier/smtp Index | Examples | Files

package smtp

import "petersanchez.com/x/carrier/smtp"

Package smtp is a delivery service for the carrier email message package.

Example

Code:play 

package main

import (
	"log"
	"os"

	"petersanchez.com/x/carrier"
	"petersanchez.com/x/carrier/smtp"
)

func main() {
	// import (
	//     "petersanchez.com/x/carrier"
	//     "petersanchez.com/x/carrier/smtp"
	// )
	conf := &smtp.MailConfig{
		SMTPPort:    587,
		SMTPHost:    "smtp.yourdomain.com",
		SMTPEncType: "starttls",
		SMTPAuth:    "plain",
		SMTPUser:    "user@yourdomain.com",
		SMTPPass:    "supersecurepassword",
	}

	// You should handle errors properly in all instances below
	svc, err := smtp.NewSMTPService(conf)
	if err != nil {
		// handle errors (here and below)
		log.Fatal(err)
	}

	msg := carrier.NewMessage()
	msg.SetFrom("me@mydomain.com").
		SetTo("recipient@theirdomain.com").
		SetCc("copy@somedomain.com")
	msg.SetSubject("Sending email from Go!")

	file, err := os.Open("funny.jpg")
	msg.AddAttachment("funny.jpg", file, "")
	file.Close()

	err = msg.SetBody("This is the text email body.")
	err = msg.SetBodyHTML("This is the HTML email body.")

	// Send email
	err = svc.Send(msg)
	log.Println("Successfully sent email via SMTP.")
}

Index

Examples

Variables

var DefaultMailConfig = MailConfig{
	SMTPPort:    25,
	SMTPHost:    "localhost",
	SMTPEncType: "insecure",
	SMTPAuth:    "none",
}

DefaultMailConfig is the default config to use.

Types

type MailConfig

type MailConfig struct {
	// SMTP Port
	SMTPPort int
	// SMTP Hostname
	SMTPHost string
	// SMTP Connection encryption type
	// Valid options are: insecure, starttls, tls
	// Defaults to insecure
	SMTPEncType string
	// SMTP Auth type
	// Valid options are: none, plain
	// Defaults to none
	SMTPAuth string
	// SMTP User account
	// Required if SMTPAuth is plain
	SMTPUser string
	// SMTP Password
	// Required if SMTPAuth is plain
	SMTPPass string
}

MailConfig Configuration for SMTP connection and sending

type Service

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

Service Direct delivery via SMTP connection

func NewSMTPService

func NewSMTPService(mc *MailConfig) (*Service, error)

NewSMTPService Returns an smtp service instance

func (*Service) Send

func (s *Service) Send(msg *carrier.Message) error

Send Sends an email from the given *carrier.Message object

Source Files

smtp.go

Version
v0.0.0-20250114214955-7f5d9b835a85 (latest)
Published
Jan 14, 2025
Platform
linux/amd64
Imports
4 packages
Last checked
1 month ago

Tools for package owners.