Search code examples
gosslsmtpgmail

Google SMTP server fails


I get the following error when attempting to use Google's SMTP server.

535 5.7.8 Username and Password not accepted. Learn more at 5.7.8 https://support.google.com/mail/?p=BadCredentials fa15sm2375541pjb.40 - gsmtp

This is my code:

// Sender data.
    from := req.FormValue("email")
    //password := "xxxx" //<- log in password fails
    password := "xxxx" // <- app password fails

    // Receiver email address.
    to := []string{
        "[email protected]",
    }

    // smtp server configuration.
    smtpHost := "smtp.gmail.com"
    smtpPort := "587"

    msg := req.FormValue("name") + "\n" + req.FormValue("message")

    message := []byte(msg)

    auth := smtp.PlainAuth("", from, password, smtpHost)

    err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
    if err != nil {
        tmp.Message = "Message not sent: " + err.Error()
        htmlTags["contact"] = tmp
        err = tmpl.ExecuteTemplate(w, "send_failure", htmlTags["contact"])
    } else {
        tmp.Message = "Message sent"
        htmlTags["contact"] = tmp
        err = tmpl.ExecuteTemplate(w, "send_success", htmlTags["contact"])
    }
} else {
    tmp.Message = "You message has not been sent. Cookies are required to send messages."
    htmlTags["contact"] = tmp
    err = tmpl.ExecuteTemplate(w, "send_failure", htmlTags["contact"])
}

The account has 2FA enabled and app password is used.

Allow less secure apps: ON

The sending code also lives on a server with a self signing cert giving the following error:

Certificate - missing
This site is missing a valid, trusted certificate (net::ERR_CERT_AUTHORITY_INVALID).

Solution

  • Provided your username and password is correct, these are the steps to get Gmail working from any client.

    # Visit https://accounts.google.com
    #
    # 1. Click on [Security] menu
    # 2. Scroll to section [Less secure app access] and set it to ON
    # 3. Scroll to section [Signing in to Google] and set [Use your phone to sign in] to OFF 
    # 4. Scroll to section [Signing in to Google] and set [2-step Verification] to OFF
    

    Now a simple text/plain email example to send:

    To: "Jane Doe" <[email protected]>
    From: "John Doe" <[email protected]>
    Subject: A text/plain email
    MIME-Version: 1.0 (Created with SublimeText 3)
    Content-Type: text/plain; charset="utf-8"
    Content-Transfer-Encoding: 7bit
    
    Good morning.
    
    This is a message in text/plain format.
    It contains text/plain.
    It contains no base64 inline images or attachments.
    Each line is no more than 76 characters in length.
    
    Please reach out if you have any questions.
    
    
    Thank you,
    
    John Doe
    SENIOR DEVELOPER
    XYZ Solutions Inc.
    P | 999.555.1234
    

    Then, you can use CURL to send it:

    # GMAIL TEST - text-plain.eml
    curl --verbose -ssl smtps://smtp.gmail.com:465 --login-options AUTH=PLAIN --user [email protected]:passwword123 --mail-from [email protected] --mail-rcpt [email protected] --mail-rcpt-allowfails --upload-file text-plain.eml