1

我正在尝试使用 Rubys ActionMailer 发送文件附件。但是,当我发送文件时,我添加的回车“\r”被删除。

string = "the quick brown\r\nfox jumped over\r\nthe bridge"

File.open(file = "attachment_#{Time.now.to_i}.txt", "w+") do |f|
  f.write(string)
end

attachments['test_file.txt'] = {
  mime_type: 'text/plain',
  content: string
}

mail(
  :to => 'somebody@example.com',
  :from => 'somebody@example.com',
  :subject => 'Message Test'
).deliver

写入的文件具有正确的行结尾,但附加的文件删除了回车符。我怎样才能防止这种情况发生?

4

1 回答 1

1

So just wanted to post my solution in case anyone else ends up with this issue...

After checking the base64 encoded attachment from the email, I found that the string, did in fact, not have the carriage return.

1.9.3-p448 :001 > Base64.decode64('dGhlIHF1aWNrIGJyb3duCmZveCBqdW1wZWQgb3Zlcgp0aGUgYnJpZGdlCg==')
=> "the quick brown\nfox jumped over\nthe bridge\n" 

This led me to believe that the ActionMailer was in fact reformatting my email before it was encoded. I figured that I could just encode the message body manually and send it over ....

encoded = Base64.encode64(string)
attachments['test_file.txt'] = {
  mime_type: 'text/plain;charset=utf-8',
  encoding: 'base64',
  content: encoded
}

And that seems to have done the trick. My attachment now contains carriage return and line feed endings ("\r\n")

I'm not sure if this is expected functionality for the ActionMailer. I definitely didn't expect it.

于 2013-10-16T22:50:34.480 回答