Raj Raj - 7 months ago 71
Ruby Question

Rails Viewpoint Send-message attachments

I've incorporated viewpoint gem to send emails using Microsoft exchange services. I don't have any issues of sending plain html email. I am not able to send a email with document attached. Someone please help me in this.

please find the sample below

endpoint = "http:///.asmx"
ep_user = ""
ep_password = ""
viewclient = Viewpoint::EWSClient.new ep_user, ep_password

view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML")


-Raj

Solution for my problem updated on 04/27/2016

I tweaked my code in such a way to make it workable

mail(:from=>"", :to =>"", :subject => "", :doc_path => 'public/images/1.doc')

endpoint = "http:///.asmx"
ep_user = ""
ep_password = ""
viewclient = Viewpoint::EWSClient.new ep_user, ep_password
data_file = message[:doc_path].value
data = [File.open(data_file), "r"]
view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML", :file_attachments => data)

Answer

The send_message options hash accepts a file_attachments option as explained in the gem's code. This option should be of type Array<File>. So I guess your code would look like:

...
array_of_files = [File.join(Rails.root, 'whatever_directory', 'whatever_file.ext')]
view_client.send_message (:subject => message.subject, :body => message.body, :body_type => "HTML", :file_attachments => array_of_files)

Update:

Seems gem is broken for the case when you try to send a message with files (I think messages are just kept as drafts and not sent, only files are). So I've updated the gem to fix this case, let me know if it works correctly. Import gem from my repo like this on your Gemfile:

gem 'viewpoint', :git => 'https://github.com/durnin/Viewpoint.git'

And try the above code again. (remember to do bundle install after updating Gemfile)

Comments