如何将联系表单中上传的pdf文件作为邮件附件发送

时间:2017-07-14 07:47:17

标签: ruby-on-rails ruby

请原谅我的noob问题。

我想将联系表单中上传的pdf文件作为邮件的附件发送。

  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

我的邮件

    class ContactMailer < ApplicationMailer
     default to: 'sss@mail.com'


    def contact_email(contact_page)
            @contact_page = contact_page
          mail(from: 'sss@mail.com', 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>

电子邮件已成功触发。如何将文档作为电子邮件的附件发送?

任何帮助都得到了高度的赞赏。谢谢你们提前!!

1 个答案:

答案 0 :(得分:2)

试试这个:

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