0

I have a program that is sending a base64url encoded string, but I read in places that the '\' character isn't supported by base64. My purpose is to send emails with the Gmail API in Go. The body part consists of the following:

"Name: \n\nThis is the body of the email\n\nSincerely,\nSenderName"

When I send emails through the Gmail API, I need to pass it a base64url string. I have the following function to handle that:

func encodeWeb64String(b []byte) string {

    s := base64.URLEncoding.EncodeToString(b)

    var i = len(s) - 1
    for s[i] == '=' {
        i--
    }

    return s[0 : i+1]
}

I have already added the header information to msg following this post, and have set the content type to text/html; charset=\"utf-8\". I then create the Gmail message using this:

gmsg := gmail.Message{
    Raw: encodeWeb64String([]byte(msg)),
}

When the email comes through, it looks like this:

Name: This is the body of the email Sincerely, SenderName

But I want each '\n' to put in actual newline. Thanks for any help, I am new with the Gmail API for Go.

Gabe
  • 5,643
  • 3
  • 26
  • 54
  • 1
    The base64 encoder handles all byte values including newline. Use "Show Original' in Gmail to view the raw message bytes received. It's possible that newlines were sent as you expect, but the message is rendered as HTML. – Charlie Tumahai Jul 27 '19 at 01:04
  • Unrelated to your problem, but I'm pretty sure you want to use `base64.RawURLEncoding` if you don't want padding (your function uses the padded version and then appears to manually strip out the padding). – Dave C Jul 27 '19 at 15:24
  • @CeriseLimón where is the option to 'Show Original' in Gmail, and how do I do it? – Gabe Jul 27 '19 at 21:53
  • @DaveC can you include a code example of how to use `base64.RawURLEncoding` – Gabe Jul 27 '19 at 22:14
  • "Show Original" is in the more menu (three vertical dots) in the message view. Show the code the sets the message headers. I suspect that the application is not setting or incorrectly setting the content type. – Charlie Tumahai Jul 27 '19 at 22:48

1 Answers1

0

I finally fixed it. I had to change the content type from text/html to text/plain, and now the newlines show properly on the email client

Gabe
  • 5,643
  • 3
  • 26
  • 54