mailjet / mailjet-gem

[API v3] Mailjet official Ruby GEM
https://dev.mailjet.com
Other
130 stars 72 forks source link

Template Language not working with ActionMailer #170

Closed jjf21 closed 3 years ago

jjf21 commented 5 years ago

When using action Mailer Template Variables are not working

# config/application.rb
config.action_mailer.delivery_method = :mailjet

Created a Mailer

class MailjetMailer < ApplicationMailer

  def transaction
    headers['X-MJ-TemplateLanguage'] = "true"
    headers['X-MJ-TemplateID'] = "742300"
    headers['X-MJ-TemplateErrorReporting'] = "deliver"
    headers['X-MJ-TemplateErrorDeliver'] = 'myemail@email.com'
    headers['X-MJ-Vars'] = {
      "step": "welcome",
      "name": "name_test",
    }
    mail(
      to: "abc@email.com",
      subject: "subject",
      delivery_method_options: {
        version: 'v3.1',
      }
    )
  end
end

Email is sent with template, but the template variables are not working.

I tried to add those parameters to delivery_method_options

        "Variables": {
          step: 'welcome',
          name: 'name_test',
        },
        "TemplateLanguage": true,
        "TemplateErrorDeliver": 'deliver',
        "TemplateErrorReporting": 'myemail@email.com',

but it didn't do anything.

If TemplateID isn't sent through headers['X-MJ-TemplateID'] = "742300" the template wasn't working at all.

Mailjet Support told me that my TemplateLanguage was not enabled but I can't find a way to send those mails as I have no clue why variables don't work and I didn't receive any mail related to TemplateErrorReporting

josei commented 4 years ago

I made it work with:

mail(
  to: "abc@email.com",
  subject: "subject",
  delivery_method_options: {
    TemplateID: 'your-template-id',
    TemplateLanguage: true,
    Variables: {...},
  },
)
rdubigny commented 4 years ago

@jjf21 I know it's been a while now but did you manage to achieve something eventually ? I am running into the very same issue with no idea how to fix it.

@josei can you be more explicit. Did you use delivery_method :mailjet or :mailjet_api ? can you give us a complete example with Variables actually set ? did you also set the headers ?

rdubigny commented 4 years ago

I did not find any way to make this work with mailjet SMTP relay. I did not find the proper tools to debug this efficiently.

I ended up using the API v3.1 which did the job. This is probably what @josei did.

josei commented 4 years ago

Yes, I used mailjet API. Sorry about the confusion. This is my configuration:

ActionMailer::Base.delivery_method = :mailjet_api
ActionMailer::Base.mailjet_api_settings = {
  api_key: 'API_KEY',
  secret_key: 'SECRET_KEY',
  version: 'v3.1'
}
xab3r commented 3 years ago

Closing as resolved.

jeremylynch commented 1 year ago

Just for reference, in case anyone else is having issues here, the following worked for me:

def example_email
    headers['TemplateID'] = "123"
    headers['TemplateLanguage'] = "true"
    mail(
      to: "test@test.com",
      from: "test@test.com",
      subject: "Subject",
      delivery_method_options: { 
        version: 'v3.1',
        api_key: "abc123", 
        secret_key: "abc123",
        Variables: {
          var_1: "test",
          var_2: "test",
        },
      },
    )
end