-
-
Notifications
You must be signed in to change notification settings - Fork 54
Provider Examples
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.
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.
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
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)
}
}
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.
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)
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 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)
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)
}
}
Socials: go-mail on Mastodon | #go-mail on Discord | #go-mail on Slack