6

メールの送信に sendgrid を使用しています。約 20 のメール テンプレートがあります。

sendgrid アプリの「Subscription Tracking」の設定で、退会テンプレートを設定しました。

私の要件は、メール テンプレートごとに登録解除リンクのテキストが異なることです。

unsubscribe link現在、 sendgrid アプリ「サブスクリプション トラッキング」で設定されている1 つの静的のみが来ています。

user_mailerクラスで購読解除リンクを動的に設定する方法を教えてください。

このリンクをたどって、 sendgrid XSMTPAPI header を使用してメールで購読解除リンクを提供します。しかし、それをルビーで実装する方法がわかりません。

以下は、私がuser_mailer classまだ試したコードです。

    def abuse_notification(post,current_user,eventid)
     headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'.to_json()
  UserNotifier.smtp_settings.merge!({:user_name => "info@xxxx.example.com"})

    @recipients  = "test@xxx.example.com"
    @from        = "xxxx"
    @subject     = "Report Abuse Notification"
    @sent_on     = Time.now
    @body[:user] = current_user
    @body[:event] = post

  end
4

1 に答える 1

6

正しい方向に進んでいますが、SendGrid SMTP API を使用するには、設定ではなく各メールにヘッダーを追加します。SMTP 設定では、(少なくとも) user_namepasswordaddressSendGrid Docs、さらに詳細な構成を保存します。次のようにActionMailer構成します。

ActionMailer::Base.smtp_settings = {
  :user_name => 'sendgridusername',
  :password => 'sendgridpassword',
  :domain => 'yourdomain.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}

ActionMailer を構成したら、UserNotifierクラスを次のように設定する必要があります。個々のメソッドごとにX-SMTPAPIヘッダーが設定されます。

class UserNotifier < ActionMailer::Base
  default :from => "bob@example.com"

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

    headers['X-SMTPAPI'] = '{"filters":{"subscriptiontrack":{"settings":{"enable":1,"text/html":"Unsubscribe <%Here%>","text/plain":"Unsubscribe Here: <% %>"}}}}'

    mail(
      :to => 'george@example.com',
      :from => email,
      :subject => 'Example Message'
    )
  end

end

ヘッダーは JSON であることに注意してくださいX-SMTPAPI。Ruby オブジェクトを JSON に変換する場合は、JSONgem を使用する必要があります。

于 2013-08-06T23:11:20.013 に答える