2015-09-25 4 views
5

smtpapi-ruby gemを使用してSendgridにレールメーラーを結び付けるにはどうすればよいですか?私は彼らのlimited documentationに従ってきましたが、私の電子メールは通過していません。普通の電子メールを送信するとSendGridの実装がうまく動作することを確認しました。これは私が持っているものです。SendGridテンプレートを使用しているRailsメーラー

user_controller.rb

def create 
    @user = User.new(user_params) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render :show, status: :created, location: @user } 


     header = Smtpapi::Header.new 
     header.add_to(@user.email) 
     header.add_substitution('user', [@user.name]) 
     header.add_substitution('body', "You've registered! This is from the controller.") 
     header.add_filter('templates', 'enable', 1) 
     header.add_filter('templates', 'template_id', 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx') 
     header.to_json 

     UserNotifier.welcome(header).deliver 
     else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

メーラー/ user_notifier.rb

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(header) 
    headers['X-SMTPAPI'] = hdr.to_json 
    mail(subject: "Welcome to the site!") 
    end 
end 

ビュー/ user_notifier/welcome.html.erb

<html> 
<body> 
    Hi -user-<br /> 
    Thanks so much for joining us! 

    <p>-body-</p> 

    Thanks,<br /> 
    The Microblog Team 
</body> 
</html> 

SendGridのアクティビティログに何も表示されないので、少なくともそこに送信されるわけではありません。少なくとも私の推測です。

私は間違っていますか?

答えて

6

私はあなたがあなたの変数を混同したと思います。 hdr.to_jsonと呼び出し、パラメータ名はheaderで、既にjsonに変換されています。

あなたが直接UserNotifierにヘッダメタデータを含める必要があります。

headers "X-SMTPAPI" => { 
    "sub": { 
     "%name%" => [user.name] 
    }, 
    "filters": { 
     "templates": { 
     "settings": { 
      "enable": 1, 
      "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' 
     } 
     } 
    } 
    }.to_json 

# the 'to' email can be overridden per action 
mail(
    from: '[email protected]', 
    to: '[email protected]', 
    subject: "Hello World" 
) 

UserNotifier.welcomeは、あなたのアプリケーションの他の部分で使用されている場合は、コンテンツを渡すことができます:あなたのテンプレートで

UserNotifier.welcome(user: @user, subject: "Welcome!").deliver_now 

# user_notifier.rb 

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(user: , subject: , template: "default") 

    # template's default view is "default" 
    headers "X-SMTPAPI" => { 
    "sub": { 
     "%name%" => [user.name] 
    }, 
    "filters": { 
     "templates": { 
     "settings": { 
      "enable": 1, 
      "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' 
     } 
     } 
    } 
    }.to_json 

    mail(
     from: '[email protected]', 
     to: user.email, 
     subject: subject, 
     template_path: 'path/to/view', 
     template_name: template 
    ) 
    # this would try to render the view: `path/to/view/default.erb` 
    end 
end 

を、タグの名前を含めて置換タグを含めることができます。

<h1>Hello %name%!</h1> 

More information about substitution tags

理にかなっている、using their template system

3

あなたのような何かにあなたのメーラーにコードを変更する必要があります。

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(hdr) 
    headers['X-SMTPAPI'] = hdr.asJSON() 
    mail(subject: "Welcome to the site!") 
    end 
end 

例:https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html

+0

OKをSendgridドキュメントをチェックアウトするが、今私は#のための '未定義のローカル変数やメソッド'「ヘッダを取得 ' – Godzilla74

+0

あなた' ApplicationMailer' inherithsを'ActionMailer :: Base'から? 'class UserNotifier Aguardientico

+0

' headers ['X-SMTPAPI'] = hdr.to_json'に行を変更しなければならなかったので、まだ実行していませんスルーします。 – Godzilla74

関連する問題