2011-08-10 4 views
16

SendGrid APIを使用するカスタムメールヘッダーを作成しようとしています。カスタムメールヘッダーの作成方法

class Mailman < ActionMailer::Base 
    default :from => "[email protected]" 

    def send_message(name, email, message) 
    @name = name 
    @email = email 
    @message = message 

    mail(:to => '[email protected]', 
    :from => email, 
    :subject => "Message from the site", 
    :headers['X-SMTPAPI'] => "category: Drip Email" 
    ) 
    end 

end 

すべてのヘルプは感謝:が、その動作していない -

は、ここで私がやっているものです。あなたがactionmailerのの#headersメソッドを使用することができます

おかげで、 アダム

答えて

44

は、私がどのように表示するようにあなたの例を編集した:

class Mailman < ActionMailer::Base 
    default :from => "[email protected]" 

    def send_message(name, email, message) 
    @name = name 
    @email = email 
    @message = message 

    headers['X-SMTPAPI'] = '{"category": "Drip Email"}' 

    mail(:to => '[email protected]', 
    :from => email, 
    :subject => "Message from the site" 
    ) 
    end 

end 

代わりに、(引数としてハッシュを渡すことができますメソッド#headersに)あまりにも:

headers {"SPECIFIC-HEADER-1" => "value", "ANOTHER-HEADER" => "and so..."} 

私は、これはあなたを助けることができると思いますし、そうでない場合は、必ずレールガイドを確認することができます:http://edgeguides.rubyonrails.org/action_mailer_basics.htmlを。

+0

ありがとうリカルド - 私の顔の前に答えがありました。私はこれを試して、それを働かせました。その後、私の解決策を確認した投稿をしました。再度、感謝します。 – Northband

+0

ニース!フィードバックをお寄せいただきありがとうございます。 –

+0

これは私には役に立たなかった。私の答えを見てください! –

2

ヘッダーメソッドには有効なJSONが必要です。

ヘッダ[「X-SMTPAPI」] =「{ 『カテゴリ』: 『ドリップメール』}」だから、リカルドのソリューションではなく、このラインを必要と私はコードの下に使用しています

3

をし、正常に動作し、単にハッシュを変換しますsendgrid以内に抑制基官能に退会グループを使用するにはto_json

headers['X-SMTPAPI'] = { 
    category: "Weekly Newsletter", 
    unique_args: { user_id: user.id } 
}.to_json 
1

とJSONに、私が働いていた次の構文を使用していました。

headers['X-SMTPAPI'] = '{"asm_group_id": 1111}' 
関連する問題