Selling files online with the following pieces:

Commerce 7.x-1.8 Commerce File 7.x-1.0 Mime Email 7.x-1.0-beta3

I would like to provide people with a document attached to an email. The commerce-checkout-rules almost gets there. What is the token replacement that 'provides a list of files; one per line'? Or otherwise, how do we attach the files being checked out?

Image token replacements

1 Answer 1


Resolved it.

The trick was to create a new variable in the rule.

  1. Store > Configuration > Checkout Settings > Checkout Rules.

  2. Edit the rule : Send an order notification email.

  3. Create a loop over each commerce-order:commerce-line-items.

  4. Add a variable to the loop.

This was the trick.

The data selector is: list-item:commerce-file-line-item-files:0:file

Don't worry about the the 0 - the loop is sending 1 email per document. The result could be cumbersome, but means that if people order 20 documents and they are large, they should still get through.

I called the variable: added_commerce_file_licence_items.

6) Create a Send HTML e-mail rule. Put it in the loop.

The attachments field contains:


7) Make sure that the permission "Send arbitrary files" is set.

I could also send by hook_mail_alter(&$message), which allowed me to try a few different things such as permutations of the URL and get a working version without too much experimentation.

function commerceemaildoc_mail_alter(&$message) {     
  if (array_key_exists('attachments', $message['params'])) {
    foreach ($message['params']['attachments'] as $attachment) {
        $file_content = file_get_contents($attachment['filepath']);
        $mime_type = file_get_mimetype($attachment['filepath']);
        $attachment = array(
            'filecontent' => $file_content,
            'filename' => basename($attachment['filepath']),
            'filemime' => $mime_type,
        $message['params']['attachments'][] = $attachment;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.