how to send message using Gmail API with Ruby Google API Client?
I think
@gmail.users.messages.send(:get) is equal to @gmail.users.messages.get
because ".send" is ruby method
so now this method is working with
@gmail.users.messages.to_h['gmail.users.messages.send']
example:
msg = Mail.new
msg.date = Time.now
msg.subject = options[:subject]
msg.body = Text.new(options[:message])
msg.from = {@_user.email => @_user.full_name}
msg.to = {
options[:to] => options[:to_name]
}
@email = @google_api_client.execute(
api_method: @gmail.users.messages.to_h['gmail.users.messages.send'],
body_object: {
raw: Base64.urlsafe_encode64(msg.to_s)
},
parameters: {
userId: 'me',
}
)
Thanks.
Sending an email with ruby gmail api v0.9
Ok. So the answer... thanks for all your help Holger...
Was that the documentation is wrong. It asks you to encode to base64.
The base64 encoding is not required (it is done internally by the api client).
The correct way to send is
msg = m.encoded
# or m.to_s
# this doesn't base64 encode. It just turns the Mail::Message object into an appropriate string.
message_object = Google::Apis::GmailV1::Message.new(raw:m.to_s)
client.send_user_message("me", message_object)
Hope that saves someone else from being patronised by an overzealous mod.
Send gmail messages with google-api-ruby-client '0.9.pre3'
Steve Bazyl seems to be correct. The documentation on send_user_message is wrong as of (0.9.13). For raw, it says: "The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied. Corresponds to the JSON property raw." As far as I can tell, this is simply incorrect.
I encountered this issue when updating from google-api-client 0.8 to 0.9 and removing the base64 encoding solved the problem. I.e. call in 0.8:
response = @service.execute(
api_method: api.users.messages.to_h['gmail.users.messages.send'],
body_object: {
raw: Base64.urlsafe_encode64(mail.to_s)
},
parameters: {
userId: 'me',
}
)
became
message = { raw: mail.to_s }
res = @service.send_user_message('me', message, {})
in 0.9.
Reported as https://github.com/google/google-api-ruby-client/issues/474.
Unable to send Gmail Message with Gmail API
We can easily offload the effort of forming a standardized and formatted email to this gem. Just include the gem in your project and do this
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)
Rails: Send email using Gmail API with attachment return only encoded file not
I think you are missing somethings with regard to the attacment
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)
For multi-part email with the attachment:
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
reply to thread google-api-ruby-client
Looking at the source on GitHub for send_user_message
shows that it doesn't take a thread_id
as a parameter. However the Message
class does have it as an attribute.
So perhaps trying this should work:
service ||= Google::Apis::GmailV1::GmailService.new
message = RMail::Message.new
message.header['To'] = params[:gmail][:to]
message.header['From'] = current_user_google_user_id
message.header['Subject'] = params[:gmail][:subject]
message.header['Subject'] = params[:gmail][:subject]
message.body = params[:gmail][:body]
message.thread_id = params[:gmail][:thread_id]
service.send_user_message(
current_user_google_user_id,
upload_source: StringIO.new(message.to_s),
content_type: 'message/rfc822'
)
Related Topics
Yaml Indentation for Array in Hash
Ruby Method That Returns Itself
Safe Navigation Operator (&.) for Nil
Ruby Regex Error: Incompatible Encoding Regexp Match (Ascii-8Bit Regexp with Utf-8 String)
Invalid Gemspec -Illformed Requirement ["#<Yaml::Syck::Defaultkey:0Xb5F9C990> 3.2.0"]
How to Deal with Memory Leaks in Rmagick in Ruby
Heroku: Gemfile.Lock Is Required Issue
Is There a Cucumber Hook to Run Before and After Each Feature
Show Full Path Name of the Ruby File When It Get Loaded
Parse Email Addresses for "From" and "To" Fields in Ruby
What Does ::Myclass Ruby Scope Operator Do
Ruby-Debug19 on Ruby-1.9.3-Preview1
Problems Setting a Custom Primary Key in a Rails 4 Migration
Deleting a Modified Object from a Set in a No-Op
Jekyll Serve Dependency Error - Could Not Open 'Lib Curl'