質問

私が使っている以下のコードを送信するレール:

class InvoiceMailer < ActionMailer::Base

  def invoice(invoice)
    from          CONFIG[:email]
    recipients    invoice.email
    subject       "Bevestiging Inschrijving #{invoice.course.name}"
    content_type  "multipart/alternative"

    part "text/html" do |p|
      p.body = render_message 'invoice_html', :invoice => invoice
    end

    part "text/plain" do |p|
      p.body = render_message 'invoice_plain', :invoice => invoice
    end

    pdf = Prawn::Document.new(:page_size => 'A4')
    PDFRenderer.render_invoice(pdf, invoice)
    attachment :content_type => "application/pdf", :body => pdf.render, :filename => "factuur.pdf"

    invoice.course.course_files.each do |file|
      attachment :content_type => file.content_type, :body => File.read(file.full_path), :filename => file.filename
    end
  end

end

そうで微細なく、メールあたか,という話がわからないのGmailのwebインタフェース.メール(リンゴのプログラム)しか取得で1アタッチメントを付け(上映されるととも2)があります。しかできないようにべきだと思います。

コピーして、メールの履歴:

Sent mail to xxx@gmail.com

From: yyy@gmail.com
To: xxx@gmail.com
Subject: Bevestiging Inschrijving Authentiek Spreken
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=mimepart_4a5b035ea0d4_769515bbca0ce9b412a


--mimepart_4a5b035ea0d4_769515bbca0ce9b412a
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: Quoted-printable
Content-Disposition: inline



  
  
  
    

拝啓

= --mimepart_4a5b035ea0d4_769515bbca0ce9b412a Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: Quoted-printable Content-Disposition: inline Dear sir * Foo= --mimepart_4a5b035ea0d4_769515bbca0ce9b412a Content-Type: application/pdf; name=factuur.pdf Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=factuur.pdf JVBERi0xLjMK/////woxIDAgb2JqCjw8IC9DcmVhdG9yIChQcmF3bikKL1By b2R1Y2VyIChQcmF3bikKPj4KZW5kb2JqCjIgMCBvYmoKPDwgL0NvdW50IDEK ... ... ... MCBuIAp0cmFpbGVyCjw8IC9JbmZvIDEgMCBSCi9TaXplIDExCi9Sb290IDMg MCBSCj4+CnN0YXJ0eHJlZgo4Nzc1CiUlRU9GCg== --mimepart_4a5b035ea0d4_769515bbca0ce9b412a Content-Type: application/pdf; name=Spelregels.pdf Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=Spelregels.pdf JVBERi0xLjQNJeLjz9MNCjYgMCBvYmoNPDwvTGluZWFyaXplZCAxL0wgMjEx NjYvTyA4L0UgMTY5NTIvTiAxL1QgMjEwMDAvSCBbIDg3NiAxOTJdPj4NZW5k ... ... ... MDIwNzQ4IDAwMDAwIG4NCnRyYWlsZXINCjw8L1NpemUgNj4+DQpzdGFydHhy ZWYNCjExNg0KJSVFT0YNCg== --mimepart_4a5b035ea0d4_769515bbca0ce9b412a--
役に立ちましたか?

解決

私は、問題はあなたが同じメッセージのちょうど別のビューで各パーツを示唆し、マルチパート/代替として、全体的な電子メールを定義しているある疑います。

私は、添付ファイルと混合したHTML /無地電子メールを送信するには、次のようなものを使用し、OKに動作するようです。

class InvoiceMailer < ActionMailer::Base

  def invoice(invoice)
    from          CONFIG[:email]
    recipients    invoice.email
    subject       "Bevestiging Inschrijving #{invoice.course.name}"
    content_type  "multipart/mixed"

    part(:content_type => "multipart/alternative") do |p|
      part "text/html" do |p|
        p.body = render_message 'invoice_html', :invoice => invoice
      end

      part "text/plain" do |p|
        p.body = render_message 'invoice_plain', :invoice => invoice
      end
    end

    pdf = Prawn::Document.new(:page_size => 'A4')
    PDFRenderer.render_invoice(pdf, invoice)
    attachment :content_type => "application/pdf", :body => pdf.render, :filename => "factuur.pdf"

    invoice.course.course_files.each do |file|
      attachment :content_type => file.content_type, :body => File.read(file.full_path), :filename => file.filename
    end
  end

end

他のヒント

@jcolemanは正しいがない場合は、その逸品をこのシーン:

class MyEmailerClass < ActionMailer::Base
  def my_email_method(address, attachment, logo)

    # Add inline attachments first so views can reference them
    attachments.inline['logo.png'] = logo

    # Call mail as per normal but keep a reference to it
    mixed = mail(:to => address) do |format|
      format.html
      format.text
    end

    # All the message parts from above will be nested into a new 'multipart/related'
    mixed.add_part(Mail::Part.new do
      content_type 'multipart/related'
      mixed.parts.delete_if { |p| add_part p }
    end)
    # Set the message content-type to be 'multipart/mixed'
    mixed.content_type 'multipart/mixed'
    mixed.header['content-type'].parameters[:boundary] = mixed.body.boundary

    # Continue adding attachments normally
    attachments['attachment.pdf'] = attachment
  end
end

このコードの始まりを、以下のMIME階層:

  • multipart/related
    • multipart/alternative
      • text/html
      • text/plain
    • image/png

呼び出し後に mail 新しい multipart/related 一部に追加の子どもたちに、既存の部分を除去した。その Content-Type する multipart/mixed 継続追加の付属により、MIME階層:

  • multipart/mixed
    • multipart/related
      • multipart/alternative
        • text/html
        • text/plain
      • image/png
    • application/pdf
それは助けたとして、

この上のジェームズへのうなずきは、私は右の作業私たちのメーラを取得します。

これにわずかな改良は:まず、私たちは(私はしなかったとき、私は問題を抱えていた)部品を追加するために、ブロック内のブロックの引数を使用します。

あなたはレイアウトを使用したい場合は、

また、あなたが#render直接使用する必要があります。ここでは仕事の両方で原則の例です。上に示したように、あなたが最後のHTML部分を維持することを確認する必要があります。

  def message_with_attachment_and_layout( options )
    from options[:from]
    recipients options[:to]
    subject options[:subject]
    content_type    "multipart/mixed"
    part :content_type => 'multipart/alternative' do |copy|
      copy.part :content_type => 'text/plain' do |plain|
        plain.body = render( :file => "#{options[:render]}.text.plain", 
          :layout => 'email', :body => options )
      end
      copy.part :content_type => 'text/html' do |html|
        html.body = render( :file => "#{options[:render]}.text.html", 
          :layout => 'email', :body => options )
      end
    end
    attachment :content_type => "application/pdf", 
      :filename => options[:attachment][:filename],
      :body => File.read( options[:attachment][:path] + '.pdf' )
  end

この例では、このように使用することになり、両方の添付ファイルやレイアウト、と一般的なマルチパートメッセージを作成するためのオプションのハッシュを使用します:

TestMailer.deliver_message_with_attachment_and_layout( 
  :from => 'a@fubar.com', :to => 'b@fubar.com', 
  :subject => 'test', :render => 'test', 
  :attachment => { :filename => 'A Nice PDF', 
    :path => 'path/to/some/nice/pdf' } )

(私たちは、実際にこれをしない:それはあなたのためにこれらの詳細の多くでは、各メーラの塗りつぶしを持っているよりよいのですが、私はそれはそれは簡単にコードを理解するために作るだろうと思った)。

お役に立てば幸いです。最高の幸運ます。

よろしく、 ダン

レールの3つの取り扱うメールの異なる、シンプルになる場合や、正しいMIME階層マルチパートのメールの両方の代替コンテンツ種類(インライン)の添付とっても複雑なので主としての階層に必要です。)

フィルの答えていくように仕事がメールの添付ファイルなのiPhone(その他の機器からのMIME階層が間違っています。

正しいMIME階層両端が見つからないようになります:

  • multipart/mixed
    • multipart/alternative
      • multipart/related
        • text/html
        • image/png (例:のためのインライン別紙;pdfう良い例)
      • text/plain
    • application/zip (e.gの添付はインライン)

私は公表した逸品の正しい階層:https://github.com/jcoleman/mail_alternatives_with_attachments

通常使用時ActionMailer3というメッセージを作成し、以下のコード:

class MyEmailerClass < ActionMailer::Base
  def my_email_method(address)
    mail :to => address, 
         :from => "noreply@myemail.com",
         :subject => "My Subject"
  end
end

この逸品をメールでの両替は、添付するには、次のコードを使用します:

class MyEmailerClass < ActionMailer::Base
  def my_email_method(address, attachment, logo)
    message = prepare_message to: address, subject: "My Subject", :content_type => "multipart/mixed"

    message.alternative_content_types_with_attachment(
      :text => render_to_string(:template => "my_template.text"),
      :html => render_to_string("my_template.html")
    ) do |inline_attachments|
      inline_attachments.inline['logo.png'] = logo
    end

    attachments['attachment.pdf'] = attachment

    message
  end
end

注意:私の手法下記の作品のう場合がございます。しかし、インライン画像-影響を与えてしまうことにもなりに添付しく表示されない現象を写メール、その他おります。 見jcolemanの回答を以下より完全な解決策です。

このことは特筆に値するレールを現在取り扱うことは、少なくともしてい 3.1rc4.からActionMailerガイド:

class UserMailer < ActionMailer::Base
  def welcome_email(user)
    @user = user
    @url  = user_url(@user)
    attachments['terms.pdf'] = File.read('/path/terms.pdf')
    mail(:to => user.email,
         :subject => "Please see the Terms and Conditions attached")
  end
end

フレキシビリティを追加、添付 お電話で mail—の追加、添付後のトリガーの選択肢の問題の質問です。

Railsの3ソリューション、マルチパートの代替電子メール(HTMLとプレーン)PDF添付ファイル付き、ノーインライン添付ファイル

以前は私だけでPDF添付ファイルと、彼らはIOSのか、OSX上mail.appで開かれた体内の平野やHTMLのどちらを示す電子メールを持っていました。 Gmailは問題ではありませんでした。

私は、インライン添付ファイルを必要としませんでしたが、

私は、コリンと同じソリューションを使用しました。それはかなり遠くに私を得た - 一つの問題を除いて - mail.app / iOSのメールではなく、HTML、プレーンテキストを示しました。 (最終的に蒸散場合)これは、代替部品は、経由して来た最初のHTML、テキストれる順番(なぜそれが決定的である必要があり、とにかく私を打つ、しかし)であった。

だから私は、もう一つの変更をしなければならなかった、むしろ愚かな、それが動作します。 .reverseを追加!方法ます。

私が持っているので、

def guest_notification(requirement, message)
 subject     = "Further booking details"
 @booking = requirement.booking
 @message = message

 mixed = mail(:to => [requirement.booking.email], :subject => subject) do |format|
   format.text
   format.html
 end

 mixed.add_part(
  Mail::Part.new do
   content_type 'multipart/alternative'
   # THE ODD BIT vv
   mixed.parts.reverse!.delete_if {|p| add_part p }
  end
 )

 mixed.content_type 'multipart/mixed'
 mixed.header['content-type'].parameters[:boundary] = mixed.body.boundary
 attachments['Final_Details.pdf'] = File.read(Rails.root + "public/FinalDetails.pdf")

end

レール4溶液

私たちのプロジェクトでは、会社のロゴ(インライン添付ファイル)およびPDF(定期的な添付ファイル)が含まれ、顧客に電子メールを送っています。我々は所定の位置に持っていた問題を回避するには、@ user1581404によってここで提供1と同様でした。

あなたがもはやmailコマンドを呼び出した後、添付ファイルを追加するために許可されているためです。

しかしレール4にプロジェクトをアップグレードした後、私たちは、新しいソリューションを見つけなければならなかったん

私たちのメーラーは、ベースメーラーを持って、我々はでメールメソッドをオーバーライドすることでこの問題を修正します

def mail(headers = {}, &block)
    message = super

    # If there are no regular attachments, we don't have to modify the mail
    return message unless message.parts.any? { |part| part.attachment? && !part.inline? }

    # Combine the html part and inline attachments to prevent issues with clients like iOS
    html_part = Mail::Part.new do
      content_type 'multipart/related'
      message.parts.delete_if { |part| (!part.attachment? || part.inline?) && add_part(part) }
    end

    # Any parts left must be regular attachments
    attachment_parts = message.parts.slice!(0..-1)

    # Reconfigure the message
    message.content_type 'multipart/mixed'
    message.header['content-type'].parameters[:boundary] = message.body.boundary
    message.add_part(html_part)
    attachment_parts.each { |part| message.add_part(part) }

    message
  end

N.B.レール3.2ます。

のようにこれらのマルチパートのメールが複数の部品をこの答えは、いまでは解剖め

  1. の順平/htmlフォーマット@Corinの"混合"アプローチが重要になります。このテキストの後、htmlにしてくれた機能を付けることができます.YMMV

  2. 設定コンテンツ-処分によるnil(取り付けられています。)固定のiPhone/iOS添付の閲覧に困難で表その他応答となります。このソリューションについてのために働くという見通しをMac、Mac OS X Mailラマ"をコンセプトとした。疑いその他のメールをお客様にうです。

  3. とは異なり以前のバージョンとのレール、添付ファイルを取り扱いして勤務。私の最大の問題は通常のように古い知ることのみを配合し、問題だった。

武器agiは、dexで下がらないboxerぐいを避ける私の落とし穴とcul-de-袋.

コード:

def example( from_user, quote)
  @quote = quote

  # attach the inline logo
  attachments.inline['logo.png'] = File.read('./public/images/logo.png')

  # attach the pdf quote
  attachments[ 'quote.pdf'] = File.read( 'path/quote.pdf')

  # create a mixed format email body
  mixed = mail( to: @quote.user.email,
                subject: "Quote") do |format|
    format.text
    format.html
  end

  # Set the message content-type to be 'multipart/mixed'
  mixed.content_type 'multipart/mixed'
  mixed.header['content-type'].parameters[:boundary] = mixed.body.boundary

  # Set Content-Disposition to nil to remove it - fixes iOS attachment viewing
  mixed.content_disposition = nil
end
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top