Search code examples
ruby-on-railsruby-on-rails-4gmailactionmailergoogle-schemas

Sending emails with Google Markup in a Rails application


I'm trying to setup Gmail's Email Markup but I'm not able to test it in my Rails application.

To test it, I have changed my ActionMailer SMTP settings to be as follows:

# config/environments/development.rb
config.action_mailer.smtp_settings = {
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => 'gmail.com',
    :user_name => '[email protected]',
    :password => '[REDACTED]',
    :authentication => 'plain',
    :enable_starttls_auto => true
}

My Mailer sets up the default from and to addresses, and defines a method mail_action as follows:

# app/mailers/test_mailer.rb
class TestMailer < ActionMailer::Base
  default from: "[email protected]"
  default to:   "[email protected]"

  def mail_action
    @data = confirm_action

    mail subject: "Mail action"
  end

  def confirm_action
    {
      "@context" => "http://schema.org",
      "@type" => "EmailMessage",
      "action" => {
        "@type" => "ConfirmAction",
        "name" => "Confirm registration",
        "handler" => {
          "@type" => "HttpActionHandler",
          "url" => "http://google.fr",
        },
      },
    }
  end
end

The method confirm_action defines the markup for my action, following Google's documentation.

In the view for the mail_action method, I add the markup in the <head>:

# app/views/test_mailer/mail_action.html.haml
!!! Strict
%html
  %head
    %title Mail Action

    = content_tag :script, type: 'application/ld+json' do
      = JSON.generate(@data).html_safe

  %body
    %h1 Mail Action
    %pre
      = JSON.generate(@data).html_safe

I simply send the email from the Rails console using TestMailer.mail_action.deliver. In the email I receive, I can clearly see the markup, however the action button is not here:

Return-Path: <[email protected]>
Received: from gmail.com ([REDACTED IP ADDRESS])
        by mx.google.com with ESMTPSA id qg11sm20491658wic.17.2014.12.17.03.07.30
        for <[email protected]>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Wed, 17 Dec 2014 03:07:36 -0800 (PST)
Date: Wed, 17 Dec 2014 12:07:29 +0100
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Mail action
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <title>Mail Action</title>
    <script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ConfirmAction","name":"Confirm registration","handler":{"@type":"HttpActionHandler","url":"http://google.fr"}}}
    </script>
  </head>
  <body>
    <h1>Mail Action</h1>
    <pre>{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ConfirmAction","name":"Confirm registration","handler":{"@type":"HttpActionHandler","url":"http://google.fr"}}}</pre>
  </body>
</html>

Gmail Inbox

Does anyone know what I'm missing / doing wrong ? Thanks in advance !


Solution

  • To send test emails, even to yourself, the emails need to be signed with DKIM/SPF to prevent spoofing as mention in this question: Unable to test the GMail custom actions

    You can test your markup sending and email to yourself with Apps Script. When the markup is well tested, you can then register with Google. Here is the information needed https://developers.google.com/gmail/markup/registering-with-google#registration_guidelines