title: smtp error: 535 5.7.8 Username and Password not accepted for gmail in go
body: <p>I'm trying to test an app's outgoing emails from localhost using this function:</p>
  
  <pre><code>func SendContactUsForm(subject, email, body string) error {
      var err error
      from := "mysender@gmail.com"
      pass := "somecrazypw"
      to := "mydestination@gmail.com"
      msg := "From: " + from + "\n" +
      "To: " + to + "\n" +
      "Subject: Contact form:" + subject + "\n" + body
      err = smtp.SendMail("smtp.gmail.com:587",
          smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
          from, []string{to}, []byte(msg))
      if err != nil {
          log.Printf("smtp error: %s", err)
          return err
      }
      return nil
  }
  </code></pre>
  
  <p>But I get this error:</p>
  
  <blockquote>
    <p>send_emails.go:171: smtp error: 535 5.7.8 Username and Password not
    accepted. Learn more at
    5.7.8  <a href="https://support.google.com/mail/?p=BadCredentials" rel="noreferrer">https://support.google.com/mail/?p=BadCredentials</a> a7sm5381413wmh.14 - gsmtp contact.go:38: error seding contact us form
    535 5.7.8 Username and Password not accepted.</p>
  </blockquote>
  
  <p>Despite the fact that the credentials of <code>mysender@gmail.com</code> are correct and I have enabled <code>Allow less secure apps</code> on <code>mysender@gmail.com</code>. </p>
  
  <p>So what could be wrong here? And how can I fix it?</p>