In rails, I want to send email using Action Mailer with attachment that is obtained from form file field and want to delay it through sidekiq.
And I have written code as below.
In view:
<%= form_tag({ controller: 'my_controller', action: 'my_mail', method: 'post' }, { multipart: true }) do %>
<%= form_field_tag(:attachment) %>
<% end %>
In controller:
def my_mail
MyMailer.delay.my_mail(params)
end
In Mailer:
def my_mail(message)
attachments['attachment'] = File.read(message[:attachment].tempfile)
mail(from: ENV['MY_MAIL'], to: ENV['MAIL_RECIVER'], subject: 'this is subject')
end
But, IOError will be raised due to inaccessibility to the file.
And, I perform File read operation in controller as
def my_mail
MyMailer.delay.my_mail(File.read(params[:attachment].tempfile))
end
Now, I can make attachment in Mailer as
attachments['attachment'] = message
And Now, It does work as i want but It's very bad to read file in controller due to security reason.
So, Now I want to know the best way to attach the file obtained from form and send it through sidekiq.
Copyright Notice:Content Author:「Shree Ram Neupane」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31530750/mail-attachment-from-form-file-field-and-send-mail-through-sidekiq