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

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

File generators

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 "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?