Skip to content

Instantly share code, notes, and snippets.

@homme
Forked from andelf/smtp_login_auth.go
Last active July 18, 2023 10:03
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save homme/22b457eb054a07e7b2fb to your computer and use it in GitHub Desktop.
Save homme/22b457eb054a07e7b2fb to your computer and use it in GitHub Desktop.
// MIT license (c) andelf 2013
import (
"net/smtp"
"errors"
)
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte(a.username), nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unkown fromServer")
}
}
return nil, nil
}
// usage:
// auth := LoginAuth("loginname", "password")
// err := smtp.SendMail(smtpServer + ":25", auth, fromAddress, toAddresses, []byte(message))
// or
// client, err := smtp.Dial(smtpServer)
// client.Auth(LoginAuth("loginname", "password"))
@Yokii908
Copy link

Thank you so much ! Spent half of the night trying to authenticate to Outlook 365 Smtp. This made the trick. You saved my sleep sir.

@rmacster
Copy link

Beautiful! Works perfectly!

@Frabec
Copy link

Frabec commented Jul 5, 2018

Wonderful ! Thanks !

@bcvery1
Copy link

bcvery1 commented Jul 10, 2018

Amazing, this works perfectly! Thanks!

Copy link

ghost commented Aug 22, 2018

thank you very much!

@alexzh7
Copy link

alexzh7 commented Feb 13, 2019

Thank you!

@jinleileiking
Copy link

I love you..

@clairmont32
Copy link

I was banging my head against this issue for 2 days and this solved it for me

@pauloxm
Copy link

pauloxm commented Mar 10, 2023

Thank you very much. It's work perfectly in my project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment