Rails:使用 Gmail API 发送带有附件 return 的电子邮件,只有编码文件没有
Rails: Send email using Gmail API with attachment return only encoded file not
我正在尝试使用 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_name@gmail.com'
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 = 'myemail@gmail.com'
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)
但这是我收到的消息:
-- Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Alex, how's life?
-- Content-Type: image/png; filename=image.png Content-Transfer-Encoding: base64 Content-Disposition: attachment;
filename=image.png Content-ID:
<5a6ebb78429d2_e4a3fd9b60336c4945c7@Jaiss-MacBook-Pro.local.mail>
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 <userone@example.com>"
message.to = 'usertwo@example.com'
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: 'userone@example.com'
},
headers: { 'Content-Type' => 'application/json' }
)
response = JSON.parse(result.body)
对于包含附件的多部分电子邮件:
message = Mail.new
message.date = Time.now
message.subject = 'Supertramp'
message.from = "testone <userone@example.com>"
message.to = 'testtwo@example.com'
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
原始 post 正在创建 Google::Apis::GmailV1::Message
的实例,其中 message
未编码,而不是已编码的 msg
。以下解决方案适用于我:
# Include the mail gem in your Gemfile https://github.com/mikel/mail
# Authentication
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
# Create mail object
message = Mail.new
message.date = Time.now
message.subject = 'test subject'
message.from = self.email
message.to = 'myemail@gmail.com'
# Create the 3 necessary parts of the message
message.part content_type: 'multipart/alternative' do |part|
part.html_part = Mail::Part.new(body: "<p>Hi Alex, how's life?</p>", content_type: 'text/html; charset=UTF-8')
part.text_part = Mail::Part.new(body: "Hi Alex, how's life?")
end
message.attachments['file.pdf'] = File.open("/path/to/file.extension").read
# Encode the message
msg = message.encoded
# Important! Create an instance of Message with the encoded message
message_object = Google::Apis::GmailV1::Message.new(raw: msg.to_s)
gmail.send_user_message('me', message_object)
我正在尝试使用 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_name@gmail.com'
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 = 'myemail@gmail.com'
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)
但这是我收到的消息:
-- Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi Alex, how's life?
-- Content-Type: image/png; filename=image.png Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=image.png Content-ID: <5a6ebb78429d2_e4a3fd9b60336c4945c7@Jaiss-MacBook-Pro.local.mail> 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 <userone@example.com>"
message.to = 'usertwo@example.com'
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: 'userone@example.com'
},
headers: { 'Content-Type' => 'application/json' }
)
response = JSON.parse(result.body)
对于包含附件的多部分电子邮件:
message = Mail.new
message.date = Time.now
message.subject = 'Supertramp'
message.from = "testone <userone@example.com>"
message.to = 'testtwo@example.com'
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
原始 post 正在创建 Google::Apis::GmailV1::Message
的实例,其中 message
未编码,而不是已编码的 msg
。以下解决方案适用于我:
# Include the mail gem in your Gemfile https://github.com/mikel/mail
# Authentication
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
# Create mail object
message = Mail.new
message.date = Time.now
message.subject = 'test subject'
message.from = self.email
message.to = 'myemail@gmail.com'
# Create the 3 necessary parts of the message
message.part content_type: 'multipart/alternative' do |part|
part.html_part = Mail::Part.new(body: "<p>Hi Alex, how's life?</p>", content_type: 'text/html; charset=UTF-8')
part.text_part = Mail::Part.new(body: "Hi Alex, how's life?")
end
message.attachments['file.pdf'] = File.open("/path/to/file.extension").read
# Encode the message
msg = message.encoded
# Important! Create an instance of Message with the encoded message
message_object = Google::Apis::GmailV1::Message.new(raw: msg.to_s)
gmail.send_user_message('me', message_object)