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

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

我需要在我的邮件中获取上传的 docx 或 pdf 文件 inbox.so 我做了以下代码。

这是我的控制器,

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

我的careermailer.rb

class CareerMailer < ApplicationMailer
default :from => 'support@gmail.com'

  def send_career_email(user,attachment)
    @user = user
    email='me@gmail.com'
    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

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

请给我一个解决方案

我会看看 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

希望对您有所帮助

请试试这个

 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

你也可以这样使用

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