Topic: upload an attachment and then send via a contact form

I have created a contact form which will email me once sent. Now I need to be able to have the option of attaching a document that will be emailed to me also. i dont need to save it in a db as it will be with the email and downloaded from there.

Im new to rails so would like to see what other people have done in this situation, ive done some reading and see that i will need the gem paperclip in most instances? Also i have looked at jquery/paperclip in github but this seems a lot of work for what i hope is a small piece of work? I could be wrong here mind, so apologies if i am

Any help greatly appreciated

Re: upload an attachment and then send via a contact form

You don't need paperclip to simply send an attachment with an email.

For example,  I have a simple 'get appointed link' on my site, meaning a user wants to become appointed as an agent for our insurance wholesale operation.  They fill out a simple form (contact name, agency name,  email),  and I send them an application form (pdf), to fill out.  Here is what it looks like:
app/mailers/employee_mailer.rb:

  def broker_apply(email,agency,contact)
    attachments['NEEIS Broker Agreement.pdf'] = File.read('public/pdf/BrokerContract.pdf')
    mail(:to=>email,:from=>'andee@neeis.com',:subject=>"NEEIS Broker Agreement")    
  end

Antibot is just what I call the email param,  because spammers will look for a param named email and try and spam your site,  so renaming email to antibot will help prevent bots from  scanning your site,  and trying to send your site bogus email.


Then in app/views/employee_mailer/broker_apply.html.erb:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Broker Appointment Application</title>
</head>
<body>
  <p>
  Thanks for your interest,  attached is your application
  </p>
</body>

To kick this all off,  in the controller:

     EmployeeMailer.broker_apply(params[:antibot],params[:agency],params[:contact]).deliver

I use antibot instead of email as a form name to prevent bots from scanning the site and sending spam.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: upload an attachment and then send via a contact form

Hi Brad,

Many thanks for your answer, Im not sure if i explained correctly or misunderstand your explanation but what i would like is as a user to fill in a contact from off my site ( which is already in place, created a model etc for this, normal things such as name, email, message) and then upload a document which will then be sent with the email to me from the site. Does your explanation cover this functionality? apologies if i did not explain correctly first time round

Re: upload an attachment and then send via a contact form

Ok,  got it, here is what you do:

You put a file_field in your form:

Select File<%= f.file_field :file %>

Now you have to make sure your form is multipart:

<%= form_for @contact, :multipart=>true %>

When you submit that form,  the :file param submitted has some extra magic,  i.e.

params[:file].read # will return the contents of the uploaded file
params[:file].content_type # will tell the type of file (mime type, i.e. application/msword)
params[:file].original_path # name off file user selected to upload

So now you have to add the attachment to the email you send to yourself.

You can just pass params[:file] to your mailer from your controller

ContactMailer.contact(params[:file],.....)

To keep the example simple,  lets just assume they always upload pdf's

Then in mailers/contact_mailer.rb

def contact(thefile,...)
  attachments["what_he_sent.pdf"] = thefile.read
  mail(:to=>"you@you.com"....

Last edited by BradHodges (2012-05-23 10:23:23)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: upload an attachment and then send via a contact form

Thanks Brad, will look at this later tonight.  What if I need users to upload odt files or .doc files, do i specify this in the contact_mailer? next to the attachments?

Finally ["what_he_sent.pdf"], what does this do, the naming convention has thrown me a little

Thanks Again Brad

Re: upload an attachment and then send via a contact form

attachments is a hash that ActionMailer understands.  It allows you to define an attachment to be sent along with the email that ActionMailer will send.  So what you are doing is adding an entry to the attachments hash.  BTW,  there was a mistake in what I originally posted,  original_path is OLD ActionMailer syntax,  since rails 3.x it is original_filename.

The key of the hash entry is used as the filename of the attachment,  what it will be called when attached to the email.  If the value of the hash is itself a hash,  that hash is expected to contain certain keys describing the attachment.

Here is a more complete example that takes into account multiple filetypes:

attachments[thefile.original_filename] =  {
   :content=>thefile.read, 
   :mime_type=>thefile.content_type
}

If you want to rename the attachment,  so you always get the same filename,  but keep the original file extension,  you could do this:

attachments['uploaded'] =  {
   :content=>thefile.read, 
   :mime_type=>thefile.content_type
}

Here is some simple stuff to illustrate basic ruby syntax

  ahash = {}
  ahash['a'] = 'The Letter A'
  puts ahash['a']

that code would print 'The Letter A'

  ahash = {}
  ahash['a'] = {:content=>'The Letter A',:type=>'text'}
  puts ahash['a'][:content]

same result

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: upload an attachment and then send via a contact form

Opps,  ignore the 'rename'  example,  won't work as posted.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: upload an attachment and then send via a contact form

Thakns Brad, makes it a little clearer.( Will read multiple times to get understanding)

Re: upload an attachment and then send via a contact form

Hi Brad

Just wondering about ContactMailer.contact(params[:file],.....). I havent got a ContactMailer, its called NotificationMailer.. Would i need to change the syntax to match?

Re: upload an attachment and then send via a contact form

No, syntax is the same,  I just had to guess at what you called your mailer.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: upload an attachment and then send via a contact form

so as i have a model called 'message' for my emails would it look like this

NotificationsMailer.message(params[:file])

Re: upload an attachment and then send via a contact form

Its ok now, got it working... thanks for your help Brad