attachment is not proper in mail in rails

心已入冬 提交于 2019-12-24 22:58:27

问题


i'm sending a mail with attachment(1.pdf) but in mail it doesnt shows 1.pdf instead it shows some random file named "ATT008220.dat".

i'm using Rails 3.0 following is the code i'm using:

        @file = File.read('c:/1.pdf')
        @file.force_encoding('BINARY')
        attachment "application/octet-stream" do |a|
            a.body = @file
        end

anybody knows why its happening? any idea?

Thanks & Regards,

Harsh Raval.

EDIT::---- The mail sending method:

def contact(recipient, subject, message, sent_at = Time.now)   
    @subject = subject
    @recipients = recipient
    @from = 'harsh@apprika.com'
    @sent_on = sent_at
    @body = message
    #@file = File.read('c:/1.pdf')
    #@file.force_encoding('US_ASCII')
    #attachment "multipart/alternative" do |a|
    #   a.body = @file
    #end
    attachments['1.pdf'] =  {:mime_type => 'application/pdf',:content => File.read('c:/1.pdf')}
    @headers = {}
end

回答1:


I had a similar problem trying to send a mail with an .xlsx file as attachment. To make it work I had to do:

attachments['filename.xlsx'] = {
  :encoding => 'base64',
  :content  => Base64.encode64(File.read(filename))
}

This is mentioned partially in section 2.3.2 of the Mailer guide: http://guides.rubyonrails.org/action_mailer_basics.html#complete-list-of-action-mailer-methods




回答2:


I think, you need to specify file name

@file = File.read('c:/1.pdf')
@file.force_encoding('BINARY')
attachment "application/octet-stream" do |a|
  a.body = @file
  a.filename = "1.pdf"
end

And I would use "application/pdf" for pdf file.

EDIT:

I looked to Rails 3 guides and I don't see any example with above syntax. Instead they use something like this:

attachments['1.pdf'] = File.read('c:/1.pdf')

Or with additional options:

attachments['1.pdf'] = {:mime_type => 'application/octet-stream',                              
                           :content => File.read('c:/1.pdf') }

Take a look here for more informations.

EDIT 2:

I want to answer your questions from comments. I haven't used mailer in Rails 3, but I use it in Rails 2.3.X and here is some code that works for me:

attachment :content_type => "application/msword",
  :body => File.read("files/word.doc"),
  :filename => "word.doc"

attachment "application/pdf" do |a|
  a.body = File.read("files/some_pdf.pdf")
  a.filename = "umowa.pdf"
end

In Rails 3 mailer API has changed. And you should use new API. Btw. you can also try if my first example works - it uses hash instead of block.

ANOTHER EDIT:

I think you should use mail object to send mail. Here is example from Rails Guides:

def welcome_email(user)
  @user = user
  @url  = user_url(@user)
  mail(:to => user.email,
     :subject => "Welcome to My Awesome Site")
end

And message should be rendered in mailer view. I think you have problems with it, because you are mixing old mailer API with new one. Take a look here to see how to do it in "new way" step by step.




回答3:


As of Rails 3(x) your content disposition is what determines the way the file name is received by the end user.

Instead of:

attachments['1.pdf'] =  {:mime_type => 'application/pdf',:content => File.read('c:/1.pdf')}

Use this:

# I like to rewrite the file name to exclude any whitespaces
safe_name = file_name.gsub(/[^0-9a-z\.]/i,"-")
attachments[safe_name] = {
  :content  => File.read(file_name),
  :content_disposition => "attachment; filename=\"#{safe_name}\""
}

You can include the mime_type but unless you explain the order in which the attachments should be included you could end up with some unsavory results unless you specify in your mailer something like so:

class SampleNotifications < ActionMailer::Base

  # my_mailer
  :content_type => 'multipart/alternative',
  :parts_order => [ "text/plain", "text/enriched", "text/html", "application/octet-stream" ]

  def notify
    ... something like the code above
  end

end



回答4:


Do as follows:

add following to actionmailer:-

def send_mail
    attachments['1.pdf'] = File.read('c:/1.pdf')
    mail(:to => "harsh@xyz.com", :subject => "xyz", :from=>"harsh@xyz.com")
    mail.deliver
end

Notes:- Make sure that the smtp settings are correct and the file corresponding to the action (In this example send_mail.rhtml) is present under appropriate folder.

Hope this helps.



来源:https://stackoverflow.com/questions/4560896/attachment-is-not-proper-in-mail-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!