在Sinatra上传Pony的文件表格

时间:2012-08-27 17:11:40

标签: ruby sinatra email-attachments pony

我正在尝试使用Pony在Sinatra上运行上传表单。现在一切正常,文件被读取,电子邮件成功邮寄,我似乎无法获得附件附件。我不认为我正确地调用文件的路径?我不完全确定,对整个Ruby / Sinatra / Pony场景都是新手。有帮助吗?非常感谢!

这就是我现在所拥有的:

 post '/upload' do
    unless params[:file] &&
           (tmpfile = params[:file][:tempfile]) &&
           (name = params[:file][:filename])
      @error = "No file selected"
      return :success
    end
    STDERR.puts "Uploading file, original name #{name.inspect}"
    while blk = tmpfile.read(65536)
      # here you would write it to its final location
      STDERR.puts blk.inspect
    end

    logger.info "some"
    Pony.mail(
        :from => params[:uname] + "<" + params[:email] + ">",
        :to => 'example@example.com',
        :subject => "Internship Prospect " + params[:uname] + " has contacted you",
        :body => "Hello,\n\nYou have a new contact request\n\nName: "+params[:uname]+"\nEmail: "+params[:email]+"\n\nMessage:\n"+params[:message]+"\n\nThanks,\The Team",
        :port => '587',
        :via => :smtp,
        :via_options => { 
          :address              => 'smtp.gmail.com', 
          :port                 => '587', 
          :enable_starttls_auto => true, 
          :user_name            => 'name@example.com', 
          :password             => 'password', 
          :authentication       => :plain, 
          :domain               => 'localhost.localdomain',
          :attachments => {params[:file][:filename] => File.read(params[:file][:tempfile])}

        })

  redirect "/success"
  end

1 个答案:

答案 0 :(得分:1)

:attachments密钥应该是第一个哈希的一部分:

Pony.mail(
        :from => params[:uname] + "<" + params[:email] + ">",
        :to => 'example@example.com',
        :subject => "Internship Prospect " + params[:uname] + " has contacted you",
        :body => "Hello,\n\nYou have a new contact request\n\nName: "+params[:uname]+"\nEmail: "+params[:email]+"\n\nMessage:\n"+params[:message]+"\n\nThanks,\The Team",
        :attachments => {params[:file][:filename] => File.read(params[:file][:tempfile])}
        :port => '587',
        :via => :smtp,
        :via_options => { 
          :address              => 'smtp.gmail.com', 
          :port                 => '587', 
          :enable_starttls_auto => true, 
          :user_name            => 'name@example.com', 
          :password             => 'password', 
          :authentication       => :plain, 
          :domain               => 'localhost.localdomain',
        })