2

Controller method

def email_pdf
    @job = Job.find_by_id(params[:id].to_i)
    job_pdf = WickedPdf.new.pdf_from_string(render_to_string(:pdf => 'job', :template => 'job/show.pdf.erb', layout: 'mailer.html.erb'))
    JobMailer.send_jobs_email(params[:id].to_i, 'vir.jain@gmail.com', 'Mahavir Jain', job_pdf).deliver_now

    respond_to do |format|
        format.json do
            render :json => {:success => true}, :status => 200
        end
    end

end

Job Mailer

class JobMailer < ApplicationMailer
    def send_jobs_email(job_id, email, name, job_pdf)
        @name = name
        @job = Job.find_by_id(job_id)
        puts 'hello1'
        attachments['job'] = job_pdf
        puts @name
        mail(to: email, subject: 'Job', from: 'info@janatrak.com', from_name: 'JanaTrak Admin')
    end
end

Output

Rendered job/show.pdf.erb within layouts/mailer.html.erb (15.0ms)
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:///var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf20151007-4801-h2de03.html\", \"/var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf_generated_file20151007-4801-7uwh23.pdf\"]***************"
"***************[\"/usr/local/bin/wkhtmltopdf\", \"-q\", \"file:///var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf20151007-4801-mp6b6i.html\", \"/var/folders/dk/t3scf65x5vx23qq1fsm53s3r0000gn/T/wicked_pdf_generated_file20151007-4801-3bayit.pdf\"]***************"

Rendered job_mailer/send_jobs_email.html.erb within layouts/mailer (0.1ms)
[{"email"=>"vir.jain@gmail.com", "status"=>"sent", "_id"=>"dc93087c39a949de827df06a1edb116e", "reject_reason"=>nil}]

Problem

From output, I can see, I am able to generate pdf properly but for some reason, it's not sending pdf as attachment. Also it's not sending email content. But when I remove attachment from code, it do properly send content of email.

I did checked Rails 3 ActionMailer and Wicked_PDF and tried both solution but none worked.

I'm using rails 4.2, ruby 2.1.2, wkhtmltopdf 0.12.2.1 (with patched qt).

Any help is really appreciated

***************** UPDATE ************************

Controller Method

def email_pdf
        @job = Job.find_by_id(params[:id].to_i)
        self.instance_variable_set(:@lookup_context, nil)
        self.instance_variable_set(:@_lookup_context, nil)
        job_pdf = WickedPdf.new.pdf_from_string(render_to_string(:template => 'job/show.pdf.erb', layout: 'mailer.html.erb'))
        #job_pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
        save_path = Rails.root.join('pdfs','job.pdf')
        dir = File.dirname(save_path)
        FileUtils.mkdir_p(dir) unless File.directory?(dir)
        File.open(save_path, 'wb') do |file|
            file << job_pdf
        end
        JobMailer.send_jobs_email(params[:id].to_i, 'vir.jain@gmail.com', 'Mahavir Jain', save_path).deliver_now

        respond_to do |format|
            format.json do
                render :json => {:success => true}, :status => 200
            end
        end

    end

Mailer Method

def send_jobs_email(job_id, email, name, job_pdf)
        @name = name
        @job = Job.find_by_id(job_id)
        puts 'hello1'
        attachments['job.pdf'] = File.read(job_pdf)
        puts job_pdf
        mail(to: email, subject: 'Job', from: 'info@janatrak.com', from_name: 'JanaTrak Admin')
    end

PDF is generating properly & i can see saved pdf.. But again somehow actionmailer not attaching pdf as well as not displaying content..

Am I anything missing related to actionmailer attachment?

Community
  • 1
  • 1
Veer
  • 2,071
  • 19
  • 24
  • 1
    Could you please check what is the size of the pdf ? – Arihant Godha Oct 07 '15 at 09:49
  • hi, PDF with simple text "Hello World!" also giving same problem.. – Veer Oct 07 '15 at 10:36
  • have you tried setting up the mime type ? – Arihant Godha Oct 07 '15 at 11:23
  • @ArihantGodha how to set mime type? through initializers? I have set through initializer – Veer Oct 07 '15 at 11:38
  • For this you have to create a template first and generate the attachment separately. Pass the file name and content and Action Mailer and the Mail gem will automatically guess the mime_type, set the encoding and create the attachment. attachments['filename.jpg'] = File.read('/path/to/filename.jpg'). Please refer this http://stackoverflow.com/questions/12339314/sending-email-with-attachments – Arihant Godha Oct 07 '15 at 12:05
  • @ArihantGodha added updated code.. filename is .pdf so, I hope it will automatically guess mime type.. – Veer Oct 07 '15 at 13:53

0 Answers0