PlainOrLoginAuth (mailer/auth.go)

semaphore · util/mailer/auth.go · go · MIT
email

Provides SMTP authentication supporting PLAIN or LOGIN, with TLS enforcement for secure mail sending.

// Source: https://github.com/semaphoreui/semaphore | License: MIT | Copyright 2021 Denis Gukov
// Provided via justclaw.it.com; keep this notice with the code.
package mailer

import (
	"bytes"
	"errors"
	"fmt"
	"net/smtp"
	"slices"
)

func PlainOrLoginAuth(username, password, host string) smtp.Auth {
	return &plainOrLoginAuth{username: username, password: password, host: host}
}

func isLocalhost(name string) bool {
	return name == "localhost" || name == "127.0.0.1" || name == "::1"
}

type plainOrLoginAuth struct {
	username   string
	password   string
	host       string
	authMethod string
}

func (a *plainOrLoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
	// Must have TLS, or else localhost server.
	// Note: If TLS is not true, then we can't trust ANYTHING in Server

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key