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

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

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

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

有没有办法做到这一点?还是我需要先使用回形针之类的东西保存文件?

最佳答案

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

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

关于ruby-on-rails - 访问文件上传而不保存文件,然后将其作为附件通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8840974/

相关文章:

ruby-on-rails - 如何杀死 Sidekiq worker ?

Ruby 批处理对讲数组

ruby-on-rails - 如何从 Rails 的日志中删除邮件 html 内容?

jquery - 如何使用 jQuery 制作交互式 map ?

ruby-on-rails - rails : How can I eager load associations with sorting through instance an method?

ruby-on-rails - 将附加信息传递给 ActionMailer Observer

ruby-on-rails - 重写 Devise Invitable `invite!` 方法以将附件添加到邀请电子邮件 - Rails

javascript - Rails : Not ember, 不是 JS 响应,而是介于两者之间的东西

javascript - AJAX更新微帖子上/下投票部分仅刷新最新帖子

ruby-on-rails - 由 railsadmin 创建的 bool 字段在 mongoid 查询中被忽略