2015-09-25 13 views
5

Làm cách nào để gắn trong đường gửi thư của tôi đến Sendgrid bằng đá quý smtpapi-ruby? Tôi đã theo dõi limited documentation của họ, nhưng email của tôi không đi qua, tôi đã xác minh rằng việc triển khai SendGrid của tôi hoạt động tốt khi chỉ gửi một email đơn giản, vì vậy không phải vậy. Đây là những gì tôi có:Rails Mailer bằng cách sử dụng SendGrid Template

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 

bưu phẩm/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 

views/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> 

Tôi không nhìn thấy bất cứ điều gì đi qua trên nhật ký hoạt động SendGrid, vì vậy nó thậm chí không nhận được gửi ở đó, ít nhất đó là dự đoán của tôi.

Tôi đang làm gì sai?

Trả lời

6

Tôi nghĩ rằng bạn đã lẫn lộn các biến của bạn. Bạn gọi hdr.to_json và tên thông số là header cũng đã được chuyển thành json.

Bạn nên bao gồm tiêu đề dữ liệu meta trực tiếp vào 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" 
) 

Bạn cũng có thể vượt qua các nội dung nếu UserNotifier.welcome được sử dụng trong các phần khác của ứng dụng của bạn:

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 

Trong mẫu của bạn, bạn có thể bao gồm các thẻ thay thế của mình bằng cách bao gồm tên của thẻ:

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

More information about substitution tags

Kiểm tra các tài liệu Sendgrid trên using their template system

3

Bạn phải thay đổi mã trong bưu phẩm của bạn để một cái gì đó như:

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

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

Ví dụ: https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html

+0

Ok, có ý nghĩa, nhưng bây giờ tôi nhận được 'không xác định biến hoặc phương pháp địa phương' tiêu đề' cho # ' – Godzilla74

+0

' ApplicationMailer' inheriths của bạn từ 'ActionMailer :: Base'? nếu không, bạn có thể thử sử dụng như sau: 'class UserNotifier Aguardientico

+0

Phải thay đổi dòng thành' tiêu đề ['X-SMTPAPI'] = hdr.to_json' thì nó sẽ gửi ... vẫn không đi mặc dù vậy. – Godzilla74

Các vấn đề liên quan