current_user
current_user

Reputation: 1202

How to send the pdf file uploaded in the contact form as an attachment to the mailer

Excuse me for the noob question.

I Want to send the pdf file uploaded in the contact form as an attachment to the mailer.

  class ContactPagesController < ApplicationController
  def new
   @contact_page =  ContactPage.new
   end

   def create
    @contact_page = ContactPage.new(contact_page_params)
   if    @contact_page.save

     contact_page = @contact_page

     ContactMailer.contact_email(contact_page).deliver_now
     flash[:notice]="we have recieved your details successfully"
     redirect_to root_path
    else

     if @contact_page.errors.any?
    flash[:notice]= "Please fill all the manditory fields"
    end
     render :new
    end
  end

  private
    def contact_page_params
      params.require(:contact_page).permit( :name, :email, :phone, :messsage, :document)
    end
  end

my mailer

    class ContactMailer < ApplicationMailer
     default to: '[email protected]'


    def contact_email(contact_page)
            @contact_page = contact_page
          mail(from: '[email protected]', subject: 'Recieved A Contact Enquery')
        end
       end

contact_email.html.erb

    <td ><%= @contact_page.email %></td>
    <td ><%= @contact_page.phone %></td>
    <td ><%= @contact_page.name %></td>
    <td ><%= @contact_page.messsage %></td>

The email is successfully triggered.How can I Send the document as an attachment to the email?

Any Help is Highly Appreciated.Thanks in Advance!!

Upvotes: 0

Views: 655

Answers (1)

Mayur Shah
Mayur Shah

Reputation: 3449

Try this one:

def contact_email(pdf,email,contact_page)
   @contact_page = contact_page
    mail(to: email,
    subject: "Recieved A Contact Enquery")
    mail.attachments['test.pdf'] = { mime_type: 'application/pdf;  charset=UTF-8 ;', content: pdf } 

                               OR

    mail.attachments['test.pdf'] = File.read('path/to/file.pdf')
 end

Upvotes: 2

Related Questions