Rails 3.2 Serialize & Deserialize Mail - ruby-on-rails-3

I'm trying to save email to a database, to send it later via a rake task. It was pretty easy in rails 2.3.8 (TMail), but I'm having trouble with rails 3.2.
Once I get the Mail object (mail), I call mail.encoded to serialize the email. I save this to the database.
My rake task loads the encoded message, but I can't find a way to recreate the mail object and call deliver (deserialize).
Mail.new(mail.encoded) seems like it should work, but the delivery fails because Mail.new doesn't get the default ActionMailer SMTP settings.
Anyone else doing something like this?
Thanks!

Alright, got this working.
To serialize the email I do the following.
email = mail(:to => 'to#me.com', ....)
string = mail.encoded
# later one
mail.new(email.string)
Mail.deliver(mail)
This appears to skip any HTML validation.
I can't set the Mail send settings (SMTP, etc), but I think it defaults to :sendmail, and that's working on the web server.

Related

Rails 3: Error with Authlogic in console

I'm trying create some users in the console. I'm getting the following error:
Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating object
This error occurs in an after_create which only job is to send an email to the user. If I disable this after_create, everything works, but this means no email is sent.
I tried to apply a solution found here, but it didn't work:
http://www.tatvartha.com/2009/09/working-with-authlogic-in-scriptconsole/
What's going on?
Thank you

First time mass-emailing using Sendgrid with rails

I am running a rails app on heroku and would like to send an email to 160 users. This is the first time I am doing this so I would to very whether the method below will lead to a successful outcome.
Sendgrid is all sent up and I have a controller setup that executes the following:
#users = User.all
#users.each do |u|
Email.send_email(p).deliver
end
I am assuming that since the number of recipients is relatively low I would be able to get by without using delayed_job or some other background processing.
SendGrid actually makes it easy to send out emails without having to use a background worker. You can do it using the X-SMTPAPI header and setting an array of email addresses in the to field. For example:
X-SMTPAPI: {
to: ["john.doe#example.com", "jackson#example.com", "freddy#example.com"]
}
In this example, each of these three emails will receive a separate copy of the email. No background workers, no complexity.
There's a gem called sendgrid that does a good job of adding some useful helpers to action mailer. Have a look at the "multiple recipients" section of the README
https://github.com/stephenb/sendgrid
I would advise that you invest the time into some background processing, as this could potentially be a hit or miss, all depending on the emailing service.

Debugging ActiveMerchant; need full request and response. How to?

Rails 3.0.10 and activemerchant gem 1.29.3
My app works fine in sandbox, but transactions in production mode are failing with "Security header is not valid", "ErrorCode"=>"10002"
We initiated a support request with paypal, after reviewing all the configuration parameters a million times and they feel we're hitting an incorrect endpoint. They've asked for a full trace for the transaction, including headers, etc, so I'm trying to figure out how to do that. I found this article
which suggested adding this to the config block
ActiveMerchant::Billing::PaypalGateway.wiredump_device = File.new(File.join([Rails.root, "log", "paypal.log"]), "a")
But that just results in an empty log; nothing gets dumped to it.
So, how can I obtain this info from the GATEWAY object, if possible? Here's the production config, the format of which is identical to what's used in staging env.
::GATEWAY = ActiveMerchant::Billing::PaypalGateway(
:login => 'me_api1.blah...',
:password => 'string...',
:signature => 'longer string...'
)
Thanks.
Needed to add the additional line as follows:
ActiveMerchant::Billing::PaypalGateway.wiredump_device.sync = true
Within the same config block in the environment
Somewhere in the class library you're using there should be a function to output this for you (if it's a well built library, that is.)
Even without that, though, you should be able to look in that PaypalGateway function to see where/how it's setting the endpoint. It's either hard-coding the value or it'll be setting different endpoints based on some sandbox option you have configured somewhere else in the class.
It's hard to tell you more than that without getting a look a the actual class library you're using, but I can concur that it must be either incorrect credentials or an incorrect endpoint. I've never once seen that security header error when it wasn't simply invalid credentials, which means either your values are incorrect or you're hitting the wrong endpoint.
If you want to post that whole function (or maybe even the whole library as the endpoint could be getting set from some other function) I can take a look and find the problem for you.

Receiving emails with attachments from SendGrid in Rails 3.2.x

I can successfully receive email from SendGrid and process its data.
My problem is the emails attachments.
When I look at what is posted for each attachment I get something in the lines of:
"attachment12"=>#<ActionDispatch::Http::UploadedFile:0x00000008a793f8
#original_filename="image036.png",
#content_type="image/png",
#headers="Content-Disposition: form-data;
name=\"attachment12\";
filename=\"image036.png\"\r\n
Content-Type: image/png\r\n",
#tempfile=#<File:/tmp/RackMultipart20120620-35076-1xav4k1>
>
Rather than the
Array
(
[attachment1] => Array
(
[name] => Upload.csv
[type] => text/csv
[tmp_name] => /tmp/phpo34iHI
[error] => 0
[size] => 76
)
)
Which is quoted in the Parse API shown enter link description here
Obviously, Rails is being elegant as always.
However, my questions are:
Where are the files, where they downloaded as implied? I cannot find them (I would be worried if this happened)
I am actually reading into ActionDispatch::Http::UploadedFile, according to the docs I should be able to do params['attachment12'].read
I will post my findings
Production is Heroku so I would need to either stick the files in some sort of container like the DB, Mongo or S3
I am guessing the file is actually at sendgrid but their support does not understand my question and revert me back to the parse api! There is probably a URL but I cannot get to the bottom of it with their support. Unfortunately.
The files are actually POSTed to your server but you get an ActionDispatch::Http::UploadedFile to work with. If you take a look at the class description you can see you have a couple of things available to you:
The original_filename attribute obviously, but you can use .read to actually read out the contents and save it or you can probably pass the tempfile directly to whatever gem you are using.
If you can tell me what kind of upload solution you use (paperclip, carrierwave) if any, I can help you a bit better.
Yes, the file was actually uploaded to your server already and is being stored in the tempfile attribute of the UploadFile. You can use the following to access the full file path:
params['attachment12'].tempfile.to_path.to_s
Source: How to retrieve a FileBlob from 'ActionDispatch::Http::UploadedFile' instance?
And just to make it clear, the files aren't actually on SendGrid's servers at this point in the process. They have already been posted to you and are stored somewhere on your server (probably in the /tmp directory).

How to read params from Transloadit upon successful upload to s3

i am using Transloadit to process and store my pic to amazon s3. The upload works fine, however upon successful redirect back to my app i get an error when trying to access one of the vales from the hash of params sent by transloadit.
<%= params[:transloadit][:ok] %>
The error returned is
can't convert Symbol into Integer
and the hash of params looks like this:
{"transloadit"=>"{\"ok\":\"ASSEMBLY_COMPLETED\",
\"message\":\"The assembly was successfully completed.\",
\"assembly_id\":\".........\",
\"assembly_url\":\"http://api2.donnie.transloadit.com/assemblies/....\",
\"bytes_received\":351697,
\"bytes_expected\":351697,.........}
I am using the gem transloadit/rails-sdk for easy integration into my app. On their github page they say and i quote:
"If you want to use the automatic transload parameter decoding, you have to include the Transloadit::Rails::ParamsDecoder module into your controller
class YourController
include Transloadit::Rails::ParamsDecoder
end
that way the param[:transloadit] is automatically decoded for you, if it exists"
I am not sure what they mean by this (even if i include this into my controller i get an error with a different set of params). What is the purpose of this line?
All i need is to access the params[:transloadit][:ok] parameter. How can i get hold of this parameter? thanks
I had a similar problem. If you use key names instead of symbols, it might help. I'm not sure why, but that's what I had to do. Try params["transloadit"]["ok"] or some variation of that.