邮件中的附件不正确 [英] attachment is not proper in mail in rails
问题描述
我发送邮件附件(1.pdf),但在邮件中它不显示1.pdf,而是显示一些名为ATT008220.dat的随机文件。
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".
我正在使用Rails 3.0
以下是我使用的代码:
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?
谢谢&问候,
恶劣的Raval。
: ----
邮件发送方式:
:---- 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
推荐答案
我有一个类似的问题,试图发送邮件与.xlsx文件作为附件。为了使它工作,我不得不做:
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))
}
Mailer指南第2.3.2节: http:// guides.rubyonrails.org/action_mailer_basics.html#complete-list-of-action-mailer-methods
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
这篇关于邮件中的附件不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!