title: Sendgrid v3 not working for me with rails my_mailer.rb
body: <p>I want to send a transactional mail via Sendgrid when a user registers (I use devise for authentication). I had this working fine in my_mailer.rb using SMTP as follows:</p>
  
  <pre><code>  def confirmation_instructions(record, token, opts={})
      # SMTP header for Sendgrid - v2
      # headers["X-SMTPAPI"]= {
      #  "sub": {
      #    "-CONFIRM_TOKEN-": [
      #      token
      #    ]       
      #   },
      #  "filters": {
      #   "templates": {
      #     "settings": {
      #       "enable": 1,
      #       "template_id": "1111111-1111-1111-1111-111111111111"
      #     }
      #   }
      #  }   
      # }.to_json    
  </code></pre>
  
  <p>However, prompted by Sendgrid to use v3 syntax to support newer mail templates, I changed code to the following (from the sendgrid help docs, as opposed to a real understanding):</p>
  
  <pre><code>  def confirmation_instructions(record, token, opts={})
  
      require 'sendgrid-ruby'
      include SendGrid
  
      sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
  
      data = JSON.parse('{
        "substitutions": {
          "-CONFIRM_TOKEN-": [
            token
          ],
        "template_id": "1111111-1111-1111-1111-111111111111"
      }')
  
      response = sg.client.mail._("send").post(request_body: data)
      puts response.status_code
      puts response.body
      puts response.parsed_body
      puts response.headers    
  </code></pre>
  
  <p>Now I get the error message:<br>
     <code>'NoMethodError (undefined method `include' for #&lt;MyMailer:0x0000000003cfa398&gt;):'</code></p>
  
  <p>If I comment out the 'include' line I get:<br>
      <code>'TypeError (no implicit conversion of nil into String):' on the line: "sg = SendGrid..."</code></p>
  
  <p>I use the Gem:  sendgrid-ruby (5.3.0)</p>
  
  <p>Any ideas would be appreciated - I've been trying to hit on the correct syntax by trial-and-error for a while now and finally admit I am stuck.</p>
  
  <p><strong>UPDATE#1:</strong><br>
  The first issue was I was using the wrong API_KEY env. variable (copied from 2 different help docs): "SENDGRID_API_KEY" (in code) vs. SENDGRID_APIKEY_GENERAL (set in Heroku). Fixed.</p>
  
  <p><strong>UPDATE #2:</strong><br>
  With the "include" line commented out I now seem to be getting a JSON parse error:</p>
  
  <p><code>JSON::ParserError (416: unexpected token at 'token</code></p>
  
  <p>So my 2 current issues are now:<br>
   (1) I would like 'token' to be the confirmation token variable but it is not being passed</p>
  
  <p>(2) Sending the below simple (1 line) content of 'data' does not throw up an error, but the appropriate template within Sendgrid is not selected:</p>
  
  <pre><code>data = JSON.parse('{
    "template_id": "1111111-1111-1111-1111-111111111111"    
  }')
  </code></pre>
  
  <p><strong>UPDATE #3:</strong><br>
  Here's an update on the status of my issue and exactly where I am now stuck:</p>
  
  <p>This code works fine (using Sendgrid v2 which I am trying to upgrade from):</p>
  
  <pre><code>  def confirmation_instructions(record, token, opts={})
  
      #
      # SMTP header for Sendgrid     - v2
      #     This works fine
      #
  
      headers["X-SMTPAPI"]= {
       "sub": {
         "-CONFIRM_TOKEN-": [
           token
         ]
        },
       "filters": {
        "templates": {
          "settings": {
            "enable": 1,
            "template_id": "1111111-1111-1111-1111-111111111111"
          }
        }
       }
      }.to_json 
  
  </code></pre>
  
  <p>This Sendgrid v3 code does not work (the email does get sent via Sendgrid but it does not select the template within Sendgrid - it just uses whatever code is in app/views/my_mailer/confirmation_instructions.html.erb):</p>
  
  <pre><code>    #
      # Sendgrid API v3
      #   This sends an email alright but it takes content from app/views/my_mailer/confirmation_instructions.html.erb
      #   It DOES NOT select the template within Sendgrid
      #
  
      data = JSON.parse('{
        "template_id": "1111111-1111-1111-1111-111111111111",
        "personalizations": [
          {
            "substitutions": {
              "-CONFIRM_TOKEN-": "'+token+'"
            } 
          }
        ]
      }')
  
      sg = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY_GENERAL2'])
      response = sg.client.mail._("send").post(request_body: data)
      puts response.status_code
      puts response.body
      puts response.parsed_body
      puts response.headers
  
  </code></pre>
  
  <p>As always, any insight appreciated.</p>