Gmail API ignoring "FROM" header when sending emails

Viewed 185

I'm trying to send an email using the gmail API in golang as follows:

  var message gmail.Message

  from := `"CUSTOM NAME" abc@gmail.com`
  header := make(map[string]string)
  header["From"] = from
  header["To"] = "to@email.com"
  header["Reply-to"] = from
  header["Subject"] = "subject"
  header["Content-Type"] = "text/html; charset=\"utf-8\""
  msg := ""
  for k, v := range header {
    msg += fmt.Sprintf("%s: %s\r\n", k, v)
  }

  body := "body"
  msg += "\r\n" + body
  message.Raw = base64.StdEncoding.EncodeToString([]byte(msg))
  s.Service.Users.Messages.Send("me", &message).Do()

The email successfully sends but in my inbox, I see the name as "abc" instead of "CUSTOM NAME". Any idea how I fix this?"

1 Answers

You cannot use a random custom name for the FROM header

It needs to be either your primary email or a valid alias.

The Gmail API documentation features a guide about working with aliases, I suggest you to read it.

Related