views:

410

answers:

0

Hi I have an email messaging system built within my application. There are many different users that can sign on and send emails to certain groups of people. I want to add the capability to send an attachment with these emails. I have no problem getting a set attachment from my HD to send, but clearly I need to allow users to add an attachment from their computer and send it with the message. I am really not sure I am even on the right track here, but I used Paperclip to have the attachment uploaded into my Message model. If I were simply trying to upload it that works great. The problem is this, when you "send" (create) a new message part of the send_mail file that is called looks for the file path of the attachment. But the attachment is not their yet because the create has not completed. It looks in the right spot just doesn't work because the upload it not complete. My code is shown below. I have no idea if I am just screwing something up by using Paperclip, I keep looking for examples but seem to only find ones where people are sending the same attachments that already exist. I really appreciate any help and please let me know if I have been unclear.

Message new.html.erb <% form_for [@message], :html => { :multipart => true } do |f| %> <%= error_messages_for :message %>

To:<br /><%= f.text_field :cc_mail %><br />
<p>Attachment:<br /><%= f.file_field :attachment %><br /> </p>
<p>Subject:<br /><%= f.text_field :subject, :size => 90 %><br /> </p>  

</p>
<%= f.submit "Send" %>
<% end %>

message_mail.rb

class MessageMailer < ActionMailer::Base

    def send_email(message)  
      recipients message.to_email
      bcc message.bcc_email
      cc message.cc_mail
      from message.to_email 
      #reply_to message.replyto
      subject message.subject

      sent_on Time.now 

      attachment  "application/octet-stream" do |a|
          a.body = File.read(message.attachment.path)
          a.filename = message.attachment_file_name
      end
  end
end