ruby-on-rails - 发送电子邮件时 Gmail API 不包含附件
问题描述
我想通过 Gmail Api 发送邮件。这是我的代码:
@client = Google::APIClient.new
@client.authorization.access_token = access_token
@service = @client.discovered_api('gmail')
mail = Mail.new
mail.subject = subject
mail.from= from_email
mail.to= to_email
mail.part content_type: 'multipart/alternative' do |part|
part.html_part = Mail::Part.new(body: message_html, content_type: 'text/html; charset=UTF-8')
part.text_part = Mail::Part.new(body: message_text)
end
open('https://example.com/image.png') do |file|
mail.attachments['image.png'] = { mime_type: 'image/png', content: file.read }
end
results = @client.execute(
api_method: @service.users.messages.to_h['gmail.users.messages.send'],
parameters: { userId: 'me' },
body_object: {
raw: Base64.urlsafe_encode64(mail.to_s)
},
)
当我使用上述代码发送邮件时,我只接收带有 message_htmt 的内容,没有附件。
请指导我发送带有附件的邮件。
解决方案
使用下面的代码 -
open('https://example.com/image.png') do |file|
mail.attachments['image.jpg'] = file.read
end
有关更多信息,请查看此问题。
推荐阅读
- java - 当我尝试使用改造 2 上传图像时,我的 cursor.getString() 返回 null
- python - GPU 上具有 BIC 或 AIC 的高斯混合模型
- python - 无法调用类 bark()
- angular7 - Angular7 i18n 消息...错过了翻译
- typescript - VS Code 在 Vue + TypeScript 项目中找不到模块,WebStorm 没有问题
- laravel - Laravel Redisearch 包
- allure - HTML版本的诱惑报告
- python - MultiCheckCombobox pyqt
- r - R - 具有相同概率的类的卡方独立性检验
- c# - 使用 jquery 为非常大的数据加载嵌套的 gridview