Skip to content

Provider Examples

Winni Neessen edited this page Nov 30, 2024 · 4 revisions

On this page, we provide some simple code examples that work for various different well-known mail providers. They are meant to give you a starting template for the Client part of go-mail for these mail providers.

Provider list

GMail/Google Mail

LOGIN/PLAIN auth

This example shows how to configure the Client to use with Google Mail/Gmail. GMail supports either PLAIN as well as LOGIN auth. Since PLAIN auth is the more standarized authentication method, we will make use of it in here, but basically they are interchangable.

Multi-Factor authentication

If you have multi-factor authentication enabled for your Google account, Google will not allow you to use your default credentials for the SMTP authentication. You will have to create a so called "App password". This app password then has to be used as password for the authentication. You can manage your app passwords on this page: https://support.google.com/accounts/answer/185833?hl=en

Client example code

package main

import (
	"fmt"
	"os"

	"github.com/wneessen/go-mail"
)

func main() {
	username := "[email protected]"
	password := "<app password>"
	client, err := mail.NewClient("smtp.gmail.com", mail.WithTLSPortPolicy(mail.TLSMandatory),
		mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername(username), mail.WithPassword(password))	
	if err != nil {
		fmt.Printf("failed to create mail client: %s\n", err)
		os.Exit(1)
	}

	message := mail.NewMsg()
	// Your message-specific code here
	if err = client.DialAndSend(message); err != nil {
		fmt.Printf("failed to send mail: %s\n", err)
		os.Exit(1)
	}
}

Apple/iCloud Mail

LOGIN/PLAIN auth

This example shows how to configure the Client to use with Google Mail/Gmail. GMail supports either PLAIN as well as LOGIN auth. Since PLAIN auth is the more standarized authentication method, we will make use of it in here, but basically they are interchangable.

App-Password

Apple by default requires a so called "App password". This app password is used as password for the authentication. You can manage your app passwords on this page: https://account.apple.com/account/manage (Click "App-Specific Passwords" in the lower-right corner)

Client example code

package main

import (
	"fmt"
	"os"

	"github.com/wneessen/go-mail"
)

func main() {
	username := "[email protected]"
	password := "<app password>"
	client, err := mail.NewClient("smtp.mail.me.com", mail.WithTLSPortPolicy(mail.TLSMandatory),
		mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername(username), mail.WithPassword(password))	
	if err != nil {
		fmt.Printf("failed to create mail client: %s\n", err)
		os.Exit(1)
	}

	message := mail.NewMsg()
	// Your message-specific code here
	if err = client.DialAndSend(message); err != nil {
		fmt.Printf("failed to send mail: %s\n", err)
		os.Exit(1)
	}
}

ProtonMail

Authentication Token

ProtonMail requires dedicated authentication tokens for each mail address. The token is used as password for the user authentication. You can generate your tokens on this page: https://account.proton.me/u/0/mail/imap-smtp (More information here: https://proton.me/support/smtp-submission)

Client example code

package main

import (
	"fmt"
	"os"

	"github.com/wneessen/go-mail"
)

func main() {
	username := "[email protected]"
	password := "<SMTP token>"
	client, err := mail.NewClient("smtp.protonmail.ch", mail.WithTLSPortPolicy(mail.TLSMandatory),
		mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithUsername(username), mail.WithPassword(password))	
	if err != nil {
		fmt.Printf("failed to create mail client: %s\n", err)
		os.Exit(1)
	}

	message := mail.NewMsg()
	// Your message-specific code here
	if err = client.DialAndSend(message); err != nil {
		fmt.Printf("failed to send mail: %s\n", err)
		os.Exit(1)
	}
}