Rails:使用带有附件的Gmail API发送电子邮件,仅返回编码文件

杰伊斯·阿纳斯鲁洛·贾法里

我正在尝试使用gmail API发送电子邮件。如下所示,我的Ruby代码无需附件即可正常运行:

client = google_client user_id
token = Token.find_by_user_id(user_id)
access_token = token.access_token
gmail = Google::Apis::GmailV1::GmailService.new
gmail.authorization = client
message              = Mail.new
message.date         = Time.now
message.subject      = 'Supertram p'
message.body         = "<p>Hi Alex, how's life?</p>"
message.content_type = 'text/html'
message.from         = self.email
message.to           = '[email protected]'

msg = message.encoded
message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
gmail.send_user_message('me', message_object)

我能够成功发送html格式的电子邮件。我的问题是如何将文件附加到此消息?

client = google_client user_id
token = Token.find_by_user_id(user_id)
access_token = token.access_token
gmail = Google::Apis::GmailV1::GmailService.new
gmail.authorization = client
message              = Mail.new
message.date         = Time.now
message.subject      = 'Supertramp'
message.body         = "<p>Hi Alex, how's life?</p>"
# message.content_type = 'text/html'
message.from         = self.email
message.to           = '[email protected]'
message.add_file("/Users/myname/Downloads/image.png")

msg = message.encoded
message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s)
gmail.send_user_message('me', message_object)

但是这是我收到的消息:

-内容类型:文本/纯文本;charset = UTF-8 Content-Transfer-Encoding:7bit Alex,您好吗?

-内容类型:image / png; filename = image.png Content-Transfer-Encoding:base64 Content-Disposition:附件;文件名= image.png内容ID:<[email protected]> iVBORw0KGgoAAAANSUhEUgAAAfAAAABMCAYAAACS0 + VcAAAX9UlEQVR4Ae2d XWxj1bXH / 6lKK + D1VE1oKEa6CPNwpQh4SFBapYOrIoGUuRnN6IRR25HuC1C / AC8JDMiFGTlP8 [...] == ----

我只收到一个编码字符串,但没有收到附件。我的问题是如何通过带有附件的Gmail API发送电子邮件?

戴姆敦

我认为您在附件方面缺少某些东西

message              = Mail.new
message.date         = Time.now
message.subject      = 'Test Email'
message.body         = "<p>Hi Test, how's life?</p>"
message.content_type = 'text/html'
message.from         = "Test User <[email protected]>"
message.to           = '[email protected]'

service = client.discovered_api('gmail', 'v1')

result = client.execute(
  api_method: service.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(message.to_s)
  },
  parameters:  {
    userId: '[email protected]'
  },
    headers:   { 'Content-Type' => 'application/json' }
)

response = JSON.parse(result.body)

对于带有附件的多部分电子邮件:

message         = Mail.new
message.date    = Time.now
message.subject = 'Supertramp'
message.from    = "testone <[email protected]>"
message.to      = '[email protected]'

message.part content_type: 'multipart/alternative' do |part|
  part.html_part = Mail::Part.new(body: "<p>Hi TEst, how's life?</p>", content_type: 'text/html; charset=UTF-8')
  part.text_part = Mail::Part.new(body: "Hi test, how's life?")
end

open('http://google.com/image.jpg') do |file| 
  message.attachments['image.jpg'] = file.read 
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用gmail rest api -objective-c发送带有附件的电子邮件

使用Java Gmail API发送带有多个(大型)附件的电子邮件

GMAIL API使用C#发送带有附件的电子邮件

如何在Node.js中使用Gmail API发送带有附件的电子邮件?

在reactjs中使用gmail api发送带有纯文本、附件和html的电子邮件

从带有附件的IBM Notes发送电子邮件

在php中使用gmail api发送带有附件的邮件

使用PHP中的GMail API发送电子邮件

如何使用Gmail API快速发送电子邮件

在Java中使用Gmail API发送电子邮件

尝试使用 Gmail API 发送电子邮件

如何使用Gmail REST API发送电子邮件

使用 appscript 在 Gmail 中批量发送带有相应附件的电子邮件

使用仅具有访问令牌的Google API发送电子邮件

从Gmail API发送电子邮件未收到,但显示在已发送文件夹中

如何在Android上使用gmail客户端API发送带有大型附件的电子邮件

使用gmail api发送电子邮件错误:此API不支持解析表单编码的输入

使用回送电子邮件数据源发送带有附件的电子邮件

如何通过Gmail API发送电子邮件?走

通过Gmail API发送电子邮件(JavaScript)

Gmail PHP API发送电子邮件

SendGrid电子邮件API,发送电子邮件附件

Python使用Base64编码的图像作为附件发送电子邮件

在 Python 中使用 zip 文件发送电子邮件

使用批处理文件发送电子邮件

Django rest框架:带有附件的sendgird电子邮件(不带模型)只有filefield可以打开文件并发送按钮来发送电子邮件

无法使用MailGun Node API发送电子邮件

使用Sendblue API和TIdHTTP发送电子邮件

Flutter-使用Google API发送电子邮件