Rails 2.3.14アプリケーションをRails 3.0に移行しています。その中で、メーラーは添付ファイル付きのメッセージを送信します。以下のコードを使用して、これは2.3.xで問題なく動作しました。 Railsの3.0 ActionMailer instructionsを通じて読むRails 3 ActionMailerが添付ファイルを受け取ることができません
def notification(material, recipient, path_to_file)
enctype = "base64"
@recipients = recipient.email
@from = material.person.email
@reply_to = material.person.email
@subject = "New or updated materials: " + material.name
@sent_on = Time.now
@content_type = "multipart/mixed"
@headers['sender'] = material.person.email
part :content_type => "text/plain",
:body => render_message('notification',
:material => material,
:url => material.full_url_to_material)
attachment :content_type => "application" + "/" + material.file_type,
:body => File.read(path_to_file),
:filename => File.basename(material.file),
:transfer_encoding => enctype,
:charset => "utf-8" if !!material.send_as_attachment
end
は、私は次のようにメソッドを変更した:材料が作成されたときに
def notification(material, recipient, path_to_file)
@material = material
@url = material.full_url_to_material
attachments[material.file_file_name] = File.open(path_to_file, 'rb'){|f| f.read} if material.send_as_attachment?
headers['sender'] = material.person.email
mail(:to => recipient.email,
:subject => "New or updated materials: " + material.name,
:reply_to => material.person.email,
:from => material.person.email)
end
MaterialMailer#通知が呼び出されます。私はこれをテストするために次の仕様を持っています:
it "will include the materials as an attachement with the the send_as_attachment field is set to 1" do
it = Material.create(@materials_hash.merge(:send_notification => "1", :send_as_attachment => "1"))
email = ActionMailer::Base.deliveries[0]
email.body.should =~ Regexp.new("Name of the posted material: " + it.name)
email.has_attachments?.should be_true
end
これは2.3でうまくいきました。私は1つにsend_as_attachmentフラグを設定している場合今、私はemail.body.shouldラインを参照し、次のエラーが発生します。
1) Material will include the materials as an attachement with the the send_as_attachment field is set to 1
Failure/Error: email.body.should =~ Regexp.new("Name of the posted material: " + it.name)
expected: /Name of the posted material: My material/
got: (using =~)
Diff:
@@ -1,2 +1 @@
-/Name of the posted material: My material/
私は仕様を変更し、0にsend_as_attachmentを設定した場合、私は次のエラーを取得しますhas_attachmentsを参照していますか?ライン:
1)材料はsend_as_attachmentフィールドが1 失敗/エラーに設定されているとの添付ファイルのような物質が含まれます:?email.has_attachmentsだから含む
本当であるにはfalseを期待 をbe_true .should添付ファイルが何とかメールを破っています。
私は材料を取り付けるための他の方法を試してみました:私は、既知のファイルへのファイルパスをハードコーディングしようとしている
attachments[material.file_file_name] = {:mime_type => "application" + "/" + material.file_content_type,
:content => File.read(material.file.path),
:charset => "utf-8"}
。しかし、運がない。
その他はどこですか?
アイロニーのアイロニーは、この作品を仕上げた後、私はそれを理解しました。私は身体の電子メールの最初の部分を見る必要があります。 – TDH
mail.parts [0] .body.should =〜Regexp.new( "投稿されたマテリアルの名前:" + it.name) – TDH
これを実行した人の回答を投稿して、それ :} –