Ruby 无法使用Gmail API发送Gmail消息
使用发送电子邮件,但我对需要传递到Ruby 无法使用Gmail API发送Gmail消息,ruby,gmail,gmail-api,google-api-ruby-client,Ruby,Gmail,Gmail Api,Google Api Ruby Client,使用发送电子邮件,但我对需要传递到Google::api::GmailV1::Message的电子邮件格式有问题,我将以以下格式传递原始参数 email_raw = "From: <#{@google_account}> To: <#{send_to}> Subject: This is the email subject The email body text goes here" # raw is: The entire email messa
Google::api::GmailV1::Message
的电子邮件格式有问题,我将以以下格式传递原始参数
email_raw = "From: <#{@google_account}>
To: <#{send_to}>
Subject: This is the email subject
The email body text goes here"
# raw is: The entire email message in an RFC 2822 formatted and base64url encoded string.
message_to_send = Google::Apis::GmailV1::Message.new(raw: Base64.encode64(email_raw))
response = @service.send_user_message("me", message_to_send)
email\u raw=“发件人:
致:
主题:这是电子邮件主题
电子邮件正文文本显示在此处“
#raw is:RFC 2822格式和base64url编码字符串中的整个电子邮件消息。
message_to_send=Google::api::GmailV1::message.new(原始:Base64.encode64(电子邮件_原始))
response=@service.send\u user\u message(“我”,message\u to\u send)
即使我在未使用base64编码的情况下通过电子邮件\u raw
,此操作也会失败。我提供了有效的电子邮件,但由于错误而失败
Google::API::ClientError(无效参数:需要收件人地址)
我已经检查过了,我也发现了,但是它使用了我在Gmail API Ruby客户端库中找不到的Mail
类。目前,email\u raw
包含\n
字符,但我在没有它的情况下对它进行了测试,它无法工作此外,我还想在邮件中发送附件。请注意,Gmail需要base64url编码,而不是base64编码 见: 原始字符串(字节格式) RFC 2822格式和base64url编码字符串中的整个电子邮件。当提供format=RAW参数时,在messages.get和drafts.get响应中返回 base64编码的字符串 我建议您首先使用测试-您可以使用在线base64url编码器对消息进行编码 然后,在使用Ruby时,可以使用以下方法:
Base64.urlsafe\u encode64(消息)
更新
问题似乎是您的原始消息体
消息正文应具有以下索引结构:
To: masroorh7@gmail.com Content-Type: multipart/alternative; boundary="000000000000f1f8eb05b18e8970" --000000000000f1f8eb05b18e8970 Content-Type: text/plain; charset="UTF-8" This is a test email --000000000000f1f8eb05b18e8970 Content-Type: text/html; charset="UTF-8" <div dir="ltr">This is a test email</div> --000000000000f1f8eb05b18e8970--
因此,您的消息正文应该是:
Google::Apis::GmailV1::Message.new(raw:encodedMessage)
请注意,Gmail需要base64url编码,而不是base64编码
见:
原始字符串(字节格式)
RFC 2822格式和base64url编码字符串中的整个电子邮件。当提供format=RAW参数时,在messages.get和drafts.get响应中返回
base64编码的字符串
我建议您首先使用测试-您可以使用在线base64url编码器对消息进行编码
然后,在使用Ruby时,可以使用以下方法:
Base64.urlsafe\u encode64(消息)
更新
问题似乎是您的原始消息体
消息正文应具有以下索引结构:
To: masroorh7@gmail.com Content-Type: multipart/alternative; boundary="000000000000f1f8eb05b18e8970" --000000000000f1f8eb05b18e8970 Content-Type: text/plain; charset="UTF-8" This is a test email --000000000000f1f8eb05b18e8970 Content-Type: text/html; charset="UTF-8" <div dir="ltr">This is a test email</div> --000000000000f1f8eb05b18e8970--
因此,您的消息正文应该是:
Google::Apis::GmailV1::Message.new(raw:encodedMessage)
我们可以轻松地将形成标准化和格式化的电子邮件的工作转移到这个位置。只需将gem包含在您的项目中并执行此操作
mail = Mail.new
mail.subject = "This is the subject"
mail.to = "someperson@gmail.com"
# to add your html and plain text content, do this
mail.part content_type: 'multipart/alternative' do |part|
part.html_part = Mail::Part.new(body: email_body, content_type: 'text/html')
part.text_part = Mail::Part.new(body: email_body)
end
# to add an attachment, do this
mail.add_file(params["file"].tempfile.path)
# when you do mail.to_s it forms a raw email text string which you can supply to the raw argument of Message object
message_to_send = Google::Apis::GmailV1::Message.new(raw: mail.to_s)
# @service is an instance of Google::Apis::GmailV1::GmailService
response = @service.send_user_message("me", message_to_send)
我们可以轻松地将形成标准化和格式化的电子邮件的工作转移到这个位置。只需将gem包含在您的项目中并执行此操作
mail = Mail.new
mail.subject = "This is the subject"
mail.to = "someperson@gmail.com"
# to add your html and plain text content, do this
mail.part content_type: 'multipart/alternative' do |part|
part.html_part = Mail::Part.new(body: email_body, content_type: 'text/html')
part.text_part = Mail::Part.new(body: email_body)
end
# to add an attachment, do this
mail.add_file(params["file"].tempfile.path)
# when you do mail.to_s it forms a raw email text string which you can supply to the raw argument of Message object
message_to_send = Google::Apis::GmailV1::Message.new(raw: mail.to_s)
# @service is an instance of Google::Apis::GmailV1::GmailService
response = @service.send_user_message("me", message_to_send)
我试过了,但还是得到了同样的错误:400“收件人地址要求”。问题在于电子邮件的原始数据我看到了,在这种情况下,我建议您在Gmail用户界面中打开任何电子邮件,单击“三点”
显示原始的
,然后使用中的,至
及以下所有内容内容类型:
作为您邮件的模板-以确保格式正确。请查看我尝试使用电子邮件的原始邮件格式时,它始终给出相同的错误信息masrooratwork@gmail.com
您的主要电子邮件?尝试:从:
,不要删除边界和--
。删除日期
和消息ID
是正确的。是的,你的每一步都让我更接近答案。我找到了一些处理RFC格式的方法。我将更新我的答案。我尝试过,但仍然得到相同的错误:400“收件人地址必需”。问题在于电子邮件的原始数据我看到了,在这种情况下,我建议您在Gmail用户界面中打开任何电子邮件,单击“三点”显示原始的
,然后使用
中的,至
及以下所有内容内容类型:
作为您邮件的模板-以确保格式正确。请查看我尝试使用电子邮件的原始邮件格式时,它始终给出相同的错误信息masrooratwork@gmail.com
您的主要电子邮件?尝试:从:
,不要删除边界和--
。删除日期
和消息ID
是正确的。是的,你的每一步都让我更接近答案。我找到了一些处理RFC格式的方法。我会更新我的答案。你也可以在使用Gmail API时尝试使用Aurinko的统一API。你也可以在使用Gmail API时尝试使用Aurinko的统一API