dousuitang5239
2019-07-26 22:59
浏览 205
已采纳

将换行符添加到base64url字符串

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: 

This is the body of the email

Sincerely,
SenderName"

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 ' ' to put in actual newline. Thanks for any help, I am new with the Gmail API for Go.

图片转代码服务由CSDN问答提供 功能建议

我有一个程序正在发送base64url编码的字符串,但在某些地方我读到了'\'字符 不受base64支持。 我的目的是使用Go中的 Gmail API 发送电子邮件。 正文部分包含以下内容:

 “名称:
 
这是电子邮件的正文
 
此致,
SenderName” 
  <  / pre> 
 
 

当我通过Gmail API发送电子邮件,我需要将它传递给base64url字符串。 我具有以下函数来处理该问题:

  func encodeWeb64String(b [] byte)字符串{
 
s  := base64.URLEncoding.EncodeToString(b)
 
 var i = len-s的1 
 [i] =='='{
 i-
} 
 
返回s  [0:i + 1] 
} 
   
 
 

我已经在发布,并将内容类型设置为 text / html; 字符集= \ “UTF-8 \”。 然后,我使用以下代码创建Gmail消息:

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

电子邮件通过时,看起来像这样:

名称:这是电子邮件的正文 真诚的,SenderName

但是我希望每个' '都放在实际的换行符中。 感谢您的帮助,我是Go API的新用户。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpmopn8542 2019-07-27 22:47
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题