使用邮件程序rails4在邮件中发送附件

时间:2021-02-24 18:11:41

I have included given code

我已经包含了给定的代码

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

and fetch parameters

并获取参数

{"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"}

and in my mailer I have included given code

在我的邮件中,我已经包含了给定的代码

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

Please guide how I can attach file in this mail I want to attach given file which I am fetching in params[:file]. Please help me out in solving this. Thanks in advance

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

2 个解决方案

#1


0  

use attachment attribute attachments['file-name.pdf'] = File.read('file-name.pdf').

使用附件属性附件['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

refer this (2.3) : http://guides.rubyonrails.org/action_mailer_basics.html

参考这个(2.3):http://guides.rubyonrails.org/action_mailer_basics.html

Modify your code to this:

将您的代码修改为:

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

in your mailer

在你的邮件

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

Thisshould work

#2


0  

Based on http://guides.rubyonrails.org/action_mailer_basics.html, you have to add attachments['file-name.jpg'] = File.read('file-name.jpg'). So, you can put in this method.

基于http://guides.rubyonrails.org/action_mailer_basics.html,您必须添加附件['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

I hope it can help you.

我希望它可以帮到你。

#1


0  

use attachment attribute attachments['file-name.pdf'] = File.read('file-name.pdf').

使用附件属性附件['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

refer this (2.3) : http://guides.rubyonrails.org/action_mailer_basics.html

参考这个(2.3):http://guides.rubyonrails.org/action_mailer_basics.html

Modify your code to this:

将您的代码修改为:

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

in your mailer

在你的邮件

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

Thisshould work

#2


0  

Based on http://guides.rubyonrails.org/action_mailer_basics.html, you have to add attachments['file-name.jpg'] = File.read('file-name.jpg'). So, you can put in this method.

基于http://guides.rubyonrails.org/action_mailer_basics.html,您必须添加附件['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

I hope it can help you.

我希望它可以帮到你。