繁体   English   中英

使用mailer rails4通过邮件发送附件

[英]send an attachment in mail using mailer rails4

我已包含给定的代码

def send_help_enterprise
    p '-----------------'
    p params
    Mailer.help_enterprise_issue(params[:app], params[:version], params[:name], params[:description][:text])
    respond_to do |format|
      format.js {
        render :layout => false
      }
    end
  end

和获取参数

{"utf8"=>"✓", "app"=>"test", "version"=>"1.1", "name"=>"faltuz", "description"=>{"text"=>"dcdfwedfed"}, "remotipart_submitted"=>"true", "authenticity_token"=>"rAykheNgAcEZF/M36i+hkpMzs+X1QZA+56hFoXAdQfXyDkGQU7K441nDylKKvj4cuxs/bfJgg7SEM0k9Kr+IGQ==", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01", "file"=>#<ActionDispatch::Http::UploadedFile:0xb483c5d4 @tempfile=#<Tempfile:/tmp/RackMultipart20150916-3796-okttg1.jpeg>, @original_filename="images1.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"images1.jpeg\"\r\nContent-Type: image/jpeg\r\n">, "controller"=>"enterprises", "action"=>"send_help_enterprise"}

并且在我的邮件中,我包含了给定的代码

def help_enterprise_issue(app,version,name,description)
    @app = app
    @version = version
    @name = name
    @description = description
    @email = 'test@gmail.com'
    mail :to => @email,
         :subject => I18n.t('mailer.info.help_enterprise_issue')
  end

请指导我如何在此邮件中附加文件,我要附加在params [:file]中提取的给定文件。 请帮我解决这个问题。 提前致谢

使用附件属性attachments['file-name.pdf'] = File.read('file-name.pdf').

def welcome(user)
  attachments['file-name.pdf'] = File.read('path/to/file-name.pdf')
  mail(:to => user, :subject => "Welcome!")
end

请参阅此(2.3): http : //guides.rubyonrails.org/action_mailer_basics.html

修改您的代码为此:

def send_help_enterprise
  p '-----------------'
  p params
  Mailer.help_enterprise_issue(params[:app], params[:version], params[:name], params[:description][:text], params[:file])
  respond_to do |format|
    format.js {
      render :layout => false
    }
  end
end

在您的邮件中

def help_enterprise_issue(app,version,name,description,file)
  @app = app
  @version = version
  @name = name
  @description = description
  @email = 'test@gmail.com'
  attachments['attachment.extension'] = file
  mail :to => @email,
       :subject => I18n.t('mailer.info.help_enterprise_issue')
end

这应该工作

基于http://guides.rubyonrails.org/action_mailer_basics.html ,您必须添加attachments['file-name.jpg'] = File.read('file-name.jpg') 因此,您可以放入此方法。

def help_enterprise_issue(app,version,name,description)
  attachments['file-name.jpg'] = File.read('file-name.jpg')
  @app = app
  @version = version
  @name = name
  @description = description
  @email = 'test@gmail.com'
  mail :to => @email,
     :subject => I18n.t('mailer.info.help_enterprise_issue')
end

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM