2012-08-27 49 views
1

我正在嘗試使用Pony在Sinatra上傳文件。現在一切正常,文件正在閱讀,電子郵件成功郵寄,我似乎無法得到附件附加。我不認爲我正確地調用文件的路徑?我不完全確定,新的Ruby/Sinatra/Pony場景。任何幫助?非常感激!上傳文件格式爲Pony in Sinatra

這就是我現在所擁有的:

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 => '[email protected]', 
     :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   => '[email protected]', 
      :password    => 'password', 
      :authentication  => :plain, 
      :domain    => 'localhost.localdomain', 
      :attachments => {params[:file][:filename] => File.read(params[:file][:tempfile])} 

     }) 

    redirect "/success" 
    end 

回答

1

:attachments關鍵應該是第一個哈希的一部分:

Pony.mail(
     :from => params[:uname] + "<" + params[:email] + ">", 
     :to => '[email protected]', 
     :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   => '[email protected]', 
      :password    => 'password', 
      :authentication  => :plain, 
      :domain    => 'localhost.localdomain', 
     }) 
+0

YES!非常感謝你! – elnatnal