RoRstarter
RoRstarter

Reputation: 13

How to send mail with (docx,pdf) attachments using actionmailer

i need to get uploaded docx or pdf file in my mail inbox.so that i did the following code.

here is my controller,

def create
    @apply = Apply.new(apply_params)
      if @apply.save
        [email protected](:thumb)
        logger.info attachment.inspect
        CareerMailer.send_career_email(@user, attachment).deliver
      end
end

my careermailer.rb

class CareerMailer < ApplicationMailer
default :from => '[email protected]'

  def send_career_email(user,attachment)
    @user = user
    email='[email protected]'
    attachments["file-name.docx"] = File.read("#{attachment}",mode: "rb") {|io| a = a + io.read}	
    mail( :to => email,
          :subject => 'You have been signed up' )
  end
end

when i tried this i'm not getting the uploaded attachments in my mail. what is wrong in my code.

Please give me a solution

Upvotes: 1

Views: 1820

Answers (3)

VijayGupta
VijayGupta

Reputation: 675

you can also use like this.

class ApplicationMailer < ActionMailer::Base
   def send_career_email(user,attachment)
     @user = user
     email='[email protected]'
     attachments['free_book.pdf'] =  open(attachment).read
     mail(:to => email, :subject => "You have been signed up")
   end
 end

Upvotes: 0

Amol Udage
Amol Udage

Reputation: 3075

Please try this

 class ApplicationMailer < ActionMailer::Base
   def send_career_email(user,attachment)
     @user = user
     email='[email protected]'
     attachments['free_book.pdf'] = File.read(attachment)
     mail(:to => email, :subject => "You have been signed up")
   end
 end

Upvotes: 1

MZaragoza
MZaragoza

Reputation: 10111

I Would take a look at the mail gem

then sending an attachment is as simple as

class CareerMailer < ApplicationMailer
  default :from => '[email protected]'

  def send_career_email(user,attachment)
    Mail.deliver do
      to       "#{user.email.to_s}"
      subject  'You have been signed up'
      body      File.read('body.txt')
      add_file '/full/path/to/somefile.png'
    end
  end
end

I hope that this helps

Upvotes: -1

Related Questions