Template Language not working with ActionMailer
Closed this issue · 6 comments
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
I made it work with:
mail(
to: "abc@email.com",
subject: "subject",
delivery_method_options: {
TemplateID: 'your-template-id',
TemplateLanguage: true,
Variables: {...},
},
)
@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 ?
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.
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'
}
Closing as resolved.
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