如何使用actionmailer发送带有(docx,pdf)附件的邮件

时间:2016-05-10 08:53:05

标签: ruby-on-rails-4 actionmailer

我需要在我的邮件收件箱中上传docx或pdf文件。所以我做了以下代码。

这是我的控制器,



map




my careermailer.rb



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




当我尝试这个时,我没有在我的邮件中获得上传的附件。我的代码有什么问题。

请给我一个解决方案

3 个答案:

答案 0 :(得分:0)

我会看一下mail gem

然后发送附件就像

一样简单
class CareerMailer < ApplicationMailer
  default :from => 'support@gmail.com'

  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

我希望这有助于

答案 1 :(得分:0)

请试试这个

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

答案 2 :(得分:0)

你也可以这样使用。

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