访问文件上载而不保存文件,然后通过电子邮件将其作为附件发送

时间:2012-01-12 19:19:02

标签: ruby-on-rails ruby-on-rails-3 actionmailer

我正在尝试制作一个表单,使用Action Mailer发送带有附件的电子邮件。我没有使用模型来支持我上传的对象。我想直接将文件附加到邮件,而不必将其保存到服务器硬盘驱动器。在我的控制器中:

def create
    attachment = params[:attachment].read
    ApplicationRequestMailer.send_application_to_be_entered(current_user.member, attachment).deliver
    render :nothing => true
end

在我的邮件中:

class ApplicationRequestMailer < ActionMailer::Base

    def send_application_to_be_entered(member, file)
        attachment[file.origional_name] = file.read
        mail(:to => 'test@test.com', :subject => "To Be Entered")
    end

end

有没有办法做到这一点?或者我是否需要先使用回形针等文件保存文件?

1 个答案:

答案 0 :(得分:4)

不确定这是否完全正确,但它正在运作:

def create
    ApplicationRequestMailer.send_application_to_be_entered(params[:application].read(), params[:application].original_filename).deliver
    redirect_to dashboards_path, :notice => "Request Sent."  
end


def send_application_to_be_entered(file, filename)
    attachments[filename] = file
    mail(:to => 'test@test.com', :subject => "Application To Be Entered")
end