Im using Sinatra and this gem, and Im able to send email ok. However, when I send an ERB file, it comes through as plain text and it doesnt render the ruby code. For example:
mail = Mail.deliver do
to user_email
from 'support#iconosites.com'
subject 'Your Upgrade is being processed'
body File.read('views/email.erb')
end
Is there a way to have it render the Ruby code?
Thanks!!
I haven't tested this but it looks like you're trying to render an erb template as the body of your email.
I think you'd need to do something more along the lines of:
require 'erb'
template = ERB.new(File.read('views/email.erb'))
mail = Mail.deliver do
to user_email
from 'support#iconosites.com'
subject 'Your Upgrade is being processed'
body template.result
end
There's a couple of email examples on the ruby-docs page for erb templates here also: http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html
Related
I have this Sinatra::Base code:
class Crush < Sinatra::Base
post '/upload' do
erb params.inspect
end
end
I am using Postman and its interface for uploading a file. So I send a POST request with form-data, where in the body of the request the name is hello and the value is a file test.txt which contains just a simple string hey there.
When I do params.inspect I get this long string
{"------WebKitFormBoundaryocOEEr26iZGSe75n\r\nContent-Disposition: form-data; name"=>"\"hello\"; filename=\"test.txt\"\r\nContent-Type: text/plain\r\n\r\nhey there\r\n------WebKitFormBoundaryocOEEr26iZGSe75n--\r\n"}
So basically a long has with a single key and a single value. Reading most Sinatra tutorials (where the file is accepted from a form), there's a nice way Sinatra handles this using params[:file], but this doesn't seem to be the case when the file is coming straight from the body of an HTTP request.
I tried a non-modular approach too withou Sinatra::Base, thinking it's some parsing middle-ware missing, but got the same result.
Is there something I'm missing here? Must I go and make my own custom parser to get the content of this long hash? Or is there an easier way?
I figured it's Postman issue. When I switch from 'x-www-form-urlencoded' to 'form-data' in Postman, in the Header section, the field: Content-Type => application/x-www-form-urlencoded is NOT removed. So for those who encounter this problem, make sure you remove it manually.
I am using RestClient ruby gem to make a multipart call like below and it works fine
RestClient.post 'my_url', {:myfile => File.new("/path/to/image.jpg", 'rb') ,:multipart => true}
In second case I have a html string like this "<html> <title>sometitle</title></html>"
I want to post this html as a file in above call. Is it possible to post the above html in the call below.
One option is to create a new file with above html content and use that but I don't want to do this. I am new to ruby . Can anyone help.
I use Grape api and i need to write a test with custom header
my code:
it "should accept message" do
post "/api/v1/my/route", post_data, secret: "ASDFGHJKL"
last_response.status.should == 201
end
but the route gets no headers at all,
i also tried headers['secret'] = "ASDFGHJKL"
and also request.env['secret']
nothing works.
how can i pass headers in rspec to grape route?
Did you try header 'secret', 'ASDFGHJKL'?
More on the rack-test docs
Try using #request variable, e.g. #request.env['secret'] = 'ASDF..'. Hope this helps.
Also, take a look at this - Sending custom headers through RSpec
I want to be able to match a route that looks something like
/2/monkey/session
I have the following in sinatra but
/:version_number/:name/session
And I keep getting the Sinatra doesn’t know this ditty. Anyone knows a way to getting this to work so that I can have params[:version_number] and params[:name] matched.
I wrote the code below (Ruby 2.0.0 / Sinatra 1.4.3).
require "sinatra"
get "/:version_number/:name/session" do
params.inspect
end
The response seems like correctly.
{"splat"=>[], "captures"=>["2", "monkey"], "version_number"=>"2", "name"=>"monkey"}
Why don't you check HTTP method or comment out other code?
I'm trying to read an email using ruby mail gem.
But mail.body.decoded returns me not just the body message. How can I clean up this body message and remove unwanted text like:
-20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-
message = $stdin.read
mail = Mail.read_from_string(message)
puts mail.body.decoded
--20cf30433c9a437cc304939017ef\nContent-Type: text/plain; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n REAL BODY TEXT \\n\n--20cf30433c9a437cc304939017ef\nContent-Type: text/html; charset=ISO-8859-1\nContent-Transfer-Encoding: quoted-printable\n\n<br clear=3D\"all\">--20cf30433c9a437cc304939017ef--
How can I clean up this email body mail message extracting only the REAL BODY TEXT , without ANY header ?
I'm creating a simple Ticket System based in Ruby on Rails, and a ticket is created when an email is received by ticket#mydomain.com. But when the message is in HTML format the BODY TEXT is surrounded by HEADERs text.
If you have a properly formatted email, you can use Mail helper methods:
mail = Mail.new(email_string)
mail.text_part # finds the first text/plain part
mail.html_part # finds the first text/html part
This doesn't always work if you have e.g. single part messages (text only) or receive email from the internet at large since you can't rely on formatting from every client out there. Believe me, I've learned the hard way.
looks like you've got a multipart email, so you can use
mail.parts[0].body.decoded
These will probably come in handy too:
mail.multipart?
mail.parts.length
The gem documentation at github is pretty decent
With the mail gem, you can do:
text = mail.multipart? ? mail.text_part.decoded : mail.body.decoded`
Add the mail gem and just use email body format with mail.parts[1].body.decoded.