<@U04UQ68083H> How do I customize the verification...
# ory-copilot
s
@chilly-king-10285 How do I customize the verification email when using http courier instead of smtp? I am using mailtrap.io APIs for sending emails. I already have configured url, api_key etc. Only thing I need is, How do I instruct Ory Kratos to call mailtrap.io sendemail api with the custom json request body
b
A message from kapa.ai
To customize the verification email when using HTTP courier with Mailtrap.io APIs, you need to configure the HTTP courier and provide a custom Jsonnet template for the request body. Here's how you can do it: 1. First, ensure you have configured the HTTP courier with Mailtrap.io URL and API key as you mentioned. 2. To customize the email content and structure, you need to provide a Jsonnet template for the request body. This template will define how the email data is formatted when sent to Mailtrap.io's API. 3. Here's an example of how you can configure the HTTP courier with a custom body template:
Copy code
yaml courier:   delivery_strategy: http   http:     request_config:       url: <https://api.mailtrap.io/api/send>       method: POST       body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgcmVjaXBpZW50OiBjdHgucmVjaXBpZW50LAogIHN1YmplY3Q6IGN0eC50ZW1wbGF0ZV9kYXRhLnN1YmplY3QsCiAgdGV4dDogY3R4LnRlbXBsYXRlX2RhdGEuYm9keSwKICBodG1sOiBjdHgudGVtcGxhdGVfZGF0YS5ib2R5Cn0>=       headers:         Content-Type: application/json         Api-Token: your_mailtrap_api_token
The
body
field contains a base64-encoded Jsonnet template. When decoded, it looks like this:
Copy code
jsonnet function(ctx) {   recipient: ctx.recipient,   subject: ctx.template_data.subject,   text: ctx.template_data.body,   html: ctx.template_data.body }
This template creates a JSON object that Mailtrap.io's API expects. You can customize this template to match the exact structure required by Mailtrap.io's API. 4. To apply this configuration, you need to update your Ory Identities configuration. You can do this using the Ory CLI or the Ory Console as described in the [Send emails using an HTTP server](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp#send-emails-using-an-http-server) documentation. Remember that the exact structure of the JSON payload might vary depending on Mailtrap.io's API requirements. You may need to adjust the Jsonnet template accordingly. Also, note that you can't use URLs to load templates in Ory Network. You must use base64-encoded templates as shown in the example above. [Custom email templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates) documentation provides more information on how to customize the content of the verification email itself.
s
My email service provider accepts the email request in this format:
Copy code
{
  "from": {
    "email": "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",
    "name": "<http://Warrantify.me|Warrantify.me>"
  },
  "to": [
    {
      "email": "<Email Recipient Goes Here>"
    }
  ],
  "subject": "Email Subject Goes Here",
  "html": "<Email Body Goes Here>"
}
Please generate appropriate jsonnet format to be used it with courier as http
@chilly-king-10285 My email service provider accepts the email request in this format:
Copy code
{
  "from": {
    "email": "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",
    "name": "<http://Warrantify.me|Warrantify.me>"
  },
  "to": [
    {
      "email": "<Email Recipient Goes Here>"
    }
  ],
  "subject": "Email Subject Goes Here",
  "html": "<Email Body Goes Here>"
}
Please generate appropriate jsonnet format to be used it with courier as http
b
A message from kapa.ai
To generate the appropriate Jsonnet format for your email service provider's request structure, you can use the following template:
Copy code
jsonnet function(ctx) {   from: {     email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",     name: "<http://Warrantify.me|Warrantify.me>"   },   to: [     {       email: ctx.recipient     }   ],   subject: ctx.template_data.subject,   html: ctx.template_data.body }
This Jsonnet template will generate the JSON structure that your email service provider expects. To use this with the HTTP courier in Ory Kratos, you need to encode it in base64 and include it in your configuration. Here's how you can set it up: 1. Encode the Jsonnet template to base64:
Copy code
echo 'function(ctx) {   from: {     email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",     name: "<http://Warrantify.me|Warrantify.me>"   },   to: [     {       email: ctx.recipient     }   ],   subject: ctx.template_data.subject,   html: ctx.template_data.body }' | base64
2. Use the resulting base64 string in your Ory Kratos configuration:
Copy code
yaml courier:   delivery_strategy: http   http:     request_config:       url: <https://your-email-service-provider-api-url>       method: POST       body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgZnJvbTogewogICAgZW1haWw6ICJuby1yZXBseUB3YXJyYW50aWZ5Lm1lIiwKICAgIG5hbWU6ICJXYXJyYW50aWZ5Lm1lIgogIH0sCiAgdG86IFsKICAgIHsKICAgICAgZW1haWw6IGN0eC5yZWNpcGllbnQKICAgIH0KICBdLAogIHN1YmplY3Q6IGN0eC50ZW1wbGF0ZV9kYXRhLnN1YmplY3QsCiAgaHRtbDogY3R4LnRlbXBsYXRlX2RhdGEuYm9keQp9>       headers:         Content-Type: application/json         # Add any other required headers for your email service provider
Make sure to replace
<https://your-email-service-provider-api-url>
with the actual URL of your email service provider's API. To apply this configuration, you need to update your Ory Identities configuration using either the Ory CLI or the Ory Console, as described in the [Send emails using an HTTP server](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp#send-emails-using-an-http-server) documentation. Remember that you may need to adjust the Jsonnet template if your email service provider requires additional fields or has different field names. The provided template matches the structure you specified in your question. [Custom email templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates) documentation provides more information on how to customize the content of the emails themselves, which will be used to populate the
subject
and
body
fields in this template.
s
@chilly-king-10285 I did that, But using the above jsonnet template gives me following error: Unable to dispatch message. audience=application error=map[messagejsonnetsecure ERROR: RUNTIME ERROR: Field does not exist: body
b
A message from kapa.ai
I apologize for the confusion in my previous response. The error you're encountering suggests that the
body
field doesn't exist in the
ctx.template_data
object. Let's modify the Jsonnet template to use the correct fields available in the context. Here's an updated version that should work:
Copy code
jsonnet
function(ctx) {
  from: {
    email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",
    name: "<http://Warrantify.me|Warrantify.me>"
  },
  to: [
    {
      email: ctx.recipient
    }
  ],
  subject: if "subject" in ctx.template_data then ctx.template_data.subject else "",
  html: if "body" in ctx.template_data then ctx.template_data.body.html else ""
}
This template includes some error checking to prevent the "field does not exist" error. Here's what changed: 1. We're now checking if the
subject
field exists in
ctx.template_data
before trying to access it. 2. For the HTML content, we're checking if
body
exists in
ctx.template_data
, and then accessing the
html
property within it. To use this updated template: 1. Encode it to base64:
Copy code
echo 'function(ctx) {
  from: {
    email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",
    name: "<http://Warrantify.me|Warrantify.me>"
  },
  to: [
    {
      email: ctx.recipient
    }
  ],
  subject: if "subject" in ctx.template_data then ctx.template_data.subject else "",
  html: if "body" in ctx.template_data then ctx.template_data.body.html else ""
}' | base64
2. Use the resulting base64 string in your Ory Kratos configuration:
Copy code
yaml
courier:
  delivery_strategy: http
  http:
    request_config:
      url: <https://your-email-service-provider-api-url>
      method: POST
      body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgZnJvbTogewogICAgZW1haWw6ICJuby1yZXBseUB3YXJyYW50aWZ5Lm1lIiwKICAgIG5hbWU6ICJXYXJyYW50aWZ5Lm1lIgogIH0sCiAgdG86IFsKICAgIHsKICAgICAgZW1haWw6IGN0eC5yZWNpcGllbnQKICAgIH0KICBdLAogIHN1YmplY3Q6IGlmICJzdWJqZWN0IiBpbiBjdHgudGVtcGxhdGVfZGF0YSB0aGVuIGN0eC50ZW1wbGF0ZV9kYXRhLnN1YmplY3QgZWxzZSAiIiwKICBodG1sOiBpZiAiYm9keSIgaW4gY3R4LnRlbXBsYXRlX2RhdGEgdGhlbiBjdHgudGVtcGxhdGVfZGF0YS5ib2R5Lmh0bWwgZWxzZSAiIgp9>
      headers:
        Content-Type: application/json
        # Add any other required headers for your email service provider
This updated template should resolve the "field does not exist: body" error. If you're still encountering issues, you might want to check the exact structure of the
ctx
object that Ory Kratos is passing to the template. You can do this by adding some debug output to your template, like this:
Copy code
jsonnet
function(ctx) {
  debug: std.trace(std.toString(ctx), "Context:"),
  // ... rest of your template
}
This will print the entire context object to the logs, which can help you understand what fields are available and how to access them correctly.
I apologize for the confusion in my previous response. The error you're encountering suggests that the
body
field doesn't exist in the
ctx.template_data
object. Let's modify the Jsonnet template to use the correct fields available in the context. Here's an updated version that should work:
Copy code
jsonnet function(ctx) {   from: {     email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",     name: "<http://Warrantify.me|Warrantify.me>"   },   to: [     {       email: ctx.recipient     }   ],   subject: if "subject" in ctx.template_data then ctx.template_data.subject else "",   html: if "body" in ctx.template_data then ctx.template_data.body.html else "" }
This template includes some error checking to prevent the "field does not exist" error. Here's what changed: 1. We're now checking if the
subject
field exists in
ctx.template_data
before trying to access it. 2. For the HTML content, we're checking if
body
exists in
ctx.template_data
, and then accessing the
html
property within it. To use this updated template: 1. Encode it to base64:
Copy code
echo 'function(ctx) {   from: {     email: "<mailto:no-reply@warrantify.me|no-reply@warrantify.me>",     name: "<http://Warrantify.me|Warrantify.me>"   },   to: [     {       email: ctx.recipient     }   ],   subject: if "subject" in ctx.template_data then ctx.template_data.subject else "",   html: if "body" in ctx.template_data then ctx.template_data.body.html else "" }' | base64
2. Use the resulting base64 string in your Ory Kratos configuration:
Copy code
yaml courier:   delivery_strategy: http   http:     request_config:       url: <https://your-email-service-provider-api-url>       method: POST       body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgZnJvbTogewogICAgZW1haWw6ICJuby1yZXBseUB3YXJyYW50aWZ5Lm1lIiwKICAgIG5hbWU6ICJXYXJyYW50aWZ5Lm1lIgogIH0sCiAgdG86IFsKICAgIHsKICAgICAgZW1haWw6IGN0eC5yZWNpcGllbnQKICAgIH0KICBdLAogIHN1YmplY3Q6IGlmICJzdWJqZWN0IiBpbiBjdHgudGVtcGxhdGVfZGF0YSB0aGVuIGN0eC50ZW1wbGF0ZV9kYXRhLnN1YmplY3QgZWxzZSAiIiwKICBodG1sOiBpZiAiYm9keSIgaW4gY3R4LnRlbXBsYXRlX2RhdGEgdGhlbiBjdHgudGVtcGxhdGVfZGF0YS5ib2R5Lmh0bWwgZWxzZSAiIgp9>       headers:         Content-Type: application/json         # Add any other required headers for your email service provider
This updated template should resolve the "field does not exist: body" error. If you're still encountering issues, you might want to check the exact structure of the
ctx
object that Ory Kratos is passing to the template. You can do this by adding some debug output to your template, like this:
Copy code
jsonnet function(ctx) {   debug: std.trace(std.toString(ctx), "Context:"),   // ... rest of your template }
This will print the entire context object to the logs, which can help you understand what fields are available and how to access them correctly. For more information on customizing email templates and troubleshooting, you can refer to the [Custom email templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates) and [Troubleshooting](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp#troubleshooting) sections of the Ory documentation.
s
How do I provide from value in http courier configuration
@chilly-king-10285 How do I provide "from" value in http courier configuration
b
A message from kapa.ai
To provide the "from" value in the HTTP courier configuration for Ory Kratos, you need to include it in the Jsonnet template that defines the structure of your email request. Here's how you can do it: 1. First, you need to modify your Jsonnet template to include the "from" field. You can set it directly in the template or use a value from the context if available. Here's an example:
Copy code
jsonnet function(ctx) {   from: {     email: "<mailto:no-reply@example.com|no-reply@example.com>",     name: "Your Company Name"   },   to: [     {       email: ctx.recipient     }   ],   subject: if "subject" in ctx.template_data then ctx.template_data.subject else "",   html: if "body" in ctx.template_data then ctx.template_data.body.html else "" }
2. After creating your Jsonnet template, you need to encode it to base64 and include it in your Ory Kratos configuration. Here's an example of how your configuration might look:
Copy code
yaml courier:   delivery_strategy: http   http:     request_config:       url: <https://your-email-service-provider-api-url>       method: POST       body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgZnJvbTogewogICAgZW1haWw6ICJuby1yZXBseUBleGFtcGxlLmNvbSIsCiAgICBuYW1lOiAiWW91ciBDb21wYW55IE5hbWUiCiAgfSwKICB0bzogWwogICAgewogICAgICBlbWFpbDogY3R4LnJlY2lwaWVudAogICAgfQogIF0sCiAgc3ViamVjdDogaWYgInN1YmplY3QiIGluIGN0eC50ZW1wbGF0ZV9kYXRhIHRoZW4gY3R4LnRlbXBsYXRlX2RhdGEuc3ViamVjdCBlbHNlICIiLAogIGh0bWw6IGlmICJib2R5IiBpbiBjdHgudGVtcGxhdGVfZGF0YSB0aGVuIGN0eC50ZW1wbGF0ZV9kYXRhLmJvZHkuaHRtbCBlbHNlICIiCn0>=       headers:         Content-Type: application/json
The "from" value is now included directly in the Jsonnet template, which is then base64 encoded and used in the configuration. To apply this configuration, you need to update your Ory Identities configuration using either the Ory CLI or the Ory Console, as described in the [Send emails using an HTTP server](https://www.ory.sh/docs/kratos/emails-sms/sending-emails-smtp#send-emails-using-an-http-server) documentation. Remember that the exact structure of the JSON payload might vary depending on your email service provider's API requirements. You may need to adjust the Jsonnet template accordingly. [Custom email templates](https://www.ory.sh/docs/kratos/emails-sms/custom-email-templates#using-custom-message-templates) documentation provides more information on how to customize the content of the emails themselves.