Skip to content

Instantly share code, notes, and snippets.

@homme
Forked from andelf/smtp_login_auth.go
Last active July 1, 2025 21:15
Show Gist options
  • Select an option

  • Save homme/22b457eb054a07e7b2fb to your computer and use it in GitHub Desktop.

Select an option

Save homme/22b457eb054a07e7b2fb to your computer and use it in GitHub Desktop.

Revisions

  1. Homme Zwaagstra revised this gist Feb 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion smtp_login_auth.go
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ func LoginAuth(username, password string) smtp.Auth {
    }

    func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
    return "LOGIN", []byte{}, nil
    return "LOGIN", []byte(a.username), nil
    }

    func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
  2. @andelf andelf created this gist Mar 8, 2013.
    41 changes: 41 additions & 0 deletions smtp_login_auth.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    // 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{}, 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"))