I've made a simple form for sending emails with attachments. The problem is that I don't know how to make it work in the mailer. All tutorials that I've found so far are covering the scenario when the file for the attachment is already somewhere one the server and I wasn't able to find anything about validating email contents.
So, I've got 2 questions for you:
- How can I let users send emails with attachments uploaded by them?
- How can I validate user's inputs and extensions of attachments?
My email form...
<div id="form_wrapper">
<%= form_for(:kontakt, :url => {:action => 'kontakt'}, :html => { :multipart => true }, :remote=>true) do |f| %>
<ul>
<li>
<%= f.label(:email) %>
<%= f.text_field(:email) %>
</li>
<li>
<%= f.label(:content) %>
<%= f.text_area(:content, :size => "42x7") %>
</li>
<li>
<%= f.label(:preview, :class=>:preview )%>
<%= f.file_field :preview %>
</li>
</ul>
<%= image_submit_tag("blank.gif",:id=>"send_email", :class=>"action submit") %>
<%= link_to("Reset", {:controller=>'frontend',:action => 'index'},:remote => true, :class => 'action reset') %>
<% end %>
</div>
<%= javascript_include_tag 'jquery.form.js','jquery.rails','jquery.remotipart' %>
and my mailer...
class Contact < ActionMailer::Base
default :from => "xxxxx@gmail.com"
def wiadomosc(email)
@content=email[:content]
file=email[:preview]
attachments[file.original_filename] =File.read(file.path)
mail(
:subject=>"www.XXXXXX.pl - you've got a new message",
:reply_to =>"xxxxx@gmail.com",
:to => email[:email]
)
end
end
I came up with attachments[file.original_filename] =File.read(file.path)
and it's adding attachments but the files are coruppted and cannot be opened...
Any thoughts or links would be greatly appreciated.