Question

I'm trying to get an upload form working in Sinatra using Pony. Right now everything works fine, the file's getting read, the emails gets mailed successfully, I just can't seem to get the attachment to attach. I don't think I'm calling the file's path correctly? I'm not entirely sure, new to the whole Ruby/Sinatra/Pony scene. Any help? MUCH appreciated!

Here's what I have right now:

 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
Was it helpful?

Solution

The :attachments key should be part of the first hash:

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',
        })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top