1

我正在尝试使用 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
4

1 回答 1

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',
        })
于 2012-08-27T18:12:41.077 回答