Generating files

Mechanic has two actions that allow you to pass along a set of files:

Mechanic accepts the same data structure for attachments, when using either of these action/option combinations: a simple hash, containing file names and file values.

These are the supported file generators:

File types

Frequently, a file value is a simple string. This works well for text files, CSV tables, or any other text-based file format.

For more complex types that require background processing, Mechanic accepts file values defining the file to be generated.

The "base64" file type

Use the "base64" type when you already have the exact data you wish to use, encoded in base64. Usage is simple: provide the bas64-encoded string.

{
  "base64": "aGVsbG8gd29ybGQh"
}

If necessary for any reason, you may use Liquid's base64 filter to prepare your data. This is only useful for situations in which the data you have on hand cannot be represented in JSON by itself.

{
  "base64": {{ opaque_data | base64 | json }}
}

The "base64" generator can be paired with any filename, with any file extension.

The "pdf" file type

Here's how the "pdf" file type can be used:

{
  "pdf": {
    "html": "<h1>This is a document.</h1>"
  }
}

The PDF is generated from the HTML you provide (using wkhtmltopdf on the backend).

The "zip" file type

Here's how the "zip" file type can be used:

{
  "zip": {
    "password": "opensesame",
    "files": {
      "confirmations.txt": "this data was protected with zipcrypto encryption"
    }
  }
}

The "password" option, given here, is optional. If provided, Mechanic will password-protect the generated zip file.

You'll note that the "zip" file generator accepts an option called "files" – this structure is also evaluated for file generators. This means that you can, for example, generate a PDF file that is then added to a password-protected zip file, which is then emailed or uploaded to an FTP server.

Examples

Diving right in, here's everything but the kitchen sink, used with the "ftp" action:

{% action "ftp" %}
  {
    "host": "example.com",
    "port": 22,
    "user": "user",
    "password": "password",
    "uploads": {
      "/tmp/journal.txt": "hello world!",
      "table.csv": "Title,SKU\nRed T-Shirt,TEE-R",
      "invoice.pdf": {
        "pdf": {
          "html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
        }
      },
      "secure.zip": {
        "zip": {
          "password": "opensesame",
          "files": {
            "confirmations.txt": "protected!",
            "invoice.pdf": {
              "pdf": {
                "html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
              }
            }
          }
        }
      }
    }
  }
{% endaction %}

... and here's the same set of files, used with the "email" action:

{% action "email" %}
  {
    "to": "user@example.com",
    "subject": "This is a test!",
    "body": "Please see attached.",
    "attachments": {
      "journal.txt": "hello world!",
      "table.csv": "Title,SKU\nRed T-Shirt,TEE-R",
      "invoice.pdf": {
        "pdf": {
          "html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
        }
      },
      "secure.zip": {
        "zip": {
          "password": "opensesame",
          "files": {
            "confirmations.txt": "protected!",
            "invoice.pdf": {
              "pdf": {
                "html": "<h1>Order #12345</h1>\n<p>It's due!</p>"
              }
            }
          }
        }
      }
    }
  }
{% endaction %}

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.