Uniform => empty mail body

Hi everyone,
im using uniform for two mail forms, form and application. Validation works great and so does sending mails.

The only thing I can’t get to work is displaying the sent form data in the email body, neither with uniform-email-default nor with uniform-email-table. I dont care about the layout, as long as I get the data in there…

Here is my controller for the form template:

<?php

return function($site, $pages, $page) {

    $form = uniform(
        'contact-form',
        array(
            'quard' => 'honeypot',
            'required' => [
                '_name'  => '',
                '_from' => 'email'
            ],
            'actions' => [
                [
                    '_action' => 'email',
                    'to'      => (string) $page->email(),
                    'sender'  => 'service@example.com',
                    'subject' => 'Example | New Application',
                    'snippet' => 'uniform-email-default',
                    'receive-copy' => false
                ]
            ]
        )
    );

   return compact('form');
};

?>

uniform-email-default is at the right place in the snippets folder. When I use uniform-email-table, I get the Markup printed out in the mail sent to me, but not the data.

Please help me, what am I doing wrong / did I miss sth in the docs?
Thanks a lot, kind regards,

Sorry, maybe move this to Questions if it doesnt fit into Plugins category.

From this part of your configuration array:

'required' => [
    '_name'  => '',
    '_from' => 'email'
],

I take it that all of your form field names begin with an underscore. Uniform treats field names beginning with an underscore as special fields that are not added to the email body (e.g. _from is already present in the email header so it doesn’t have to be present in the email body, too).

So either you rename your form fields so they don’t begin with an underscore or you modify the email snippet so it doesn’t ignore such fields any more.

1 Like

Thanks a lot, I seem to have missed that part of the docs…
I’ve got another question though, because my fileupload is constantly running into the if (!array_key_exists($field, $_FILES) option - does the fileupload only try to upload filled fields? I need to have non-mandatory file uploads and just upload/validate the ones that contain a file. Atm I’m not even getting to successfully finish the upload action.

Template is:

          <li>
            <label for="file"><?php echo l::get('foto_upload'); ?> (max. 1MB)</label>
            <input type="file" accept="image/*" name="foto_upload" id="foto_upload"/>
          </li>
          <li class="half">
            <label for="passport_1"><?php echo l::get('passport_1'); ?></label>
            <input type="file" accept="image/*" name="passport_1" id="passport_1"/>
          </li>
          <li class="half">
            <label for="passport_2"><?php echo l::get('passport_2'); ?></label>
            <input type="file" accept="image/*" name="passport_2" id="passport_2"/>
          </li>

Controller is:

<?php
uniform::$actions['upload'] = function($form, $actionOptions) {
   foreach ($actionOptions['files'] as $field) {
      if (!array_key_exists($field, $_FILES)) {
         return [
            'success' => false,
            'message' => "- Notice: File {$field} was not submitted"
         ];
      }
   }

   foreach ($actionOptions['files'] as $field) {
      move_uploaded_file($_FILES[$field]['tmp_name'], $actionOptions['target'].'/'.$_FILES[$field]['tmp_name']);
   }

   return [
      'success' => true
   ];
};

return function($site, $pages, $page) {
    $form = uniform(
        'contact-form',
        array(
            'quard' => 'honeypot',
            'required' => [
                'salutation'  => '',
                'birthday'  => '',
                'first_name'  => '',
                'last_name'  => '',
                'email'  => 'email',
                'emailconfirmation'  => 'email',
                'phone'  => '',
                'street'  => '',
                'streetnr'  => '',
                'city'  => '',
                'zipcode'  => '',
                'country'  => '',
                'earliest_month'  => '',
                'earliest_year'  => '',
                'latest_month'  => '',
                'latest_year'  => '',
                'purpose'  => '',
                '_terms'  => ''
            ],
            'actions' => [
                [
                    '_action' => 'email',
                    'to'      => (string) $page->email(),
                    'sender'  => 'service@easy-street.de',
                    'subject' => 'Easy Street | New Application',
                    'snippet' => 'uniform-email-default',
                    'receive-copy' => false
                ], [
                    '_action' => 'upload',
                    'files' => ['foto_upload', 'passport_1', 'passport_2'],
                    'target' => 'content/'.$page->diruri().'/files',
                ]
            ]
        )
    );

   return compact('form');
};

?>

Thanks a lot

Since Uniform can’t validate form fields in the $_FILES array the upload action has a separate validation mechanism. Currently it requires all fields of the files array to be filled, that’s why submitting of your form doesn’t work if one field is not set.

If you don’t want the file upload fields to be mandatory, simply remove the first foreach loop from the upload action and move the if condition to the second loop like this:

foreach ($actionOptions['files'] as $field) {
   if (array_key_exists($field, $_FILES)) {
      move_uploaded_file($_FILES[$field]['tmp_name'], $actionOptions['target'].'/'.$_FILES[$field]['tmp_name']);
   }
}

Now the file upload fields are optional.

Hi @mzur.
Thanks for your support. This is probably going to be my last question on this topic :slightly_smiling:

In my email, I now get the filename of the file sent along. There are no errors - but: I can’t see the file being uploaded anywhere on my server. Is my path wrong? I expect the file to be found under content/3-application/files.

<?php

uniform::$actions['upload'] = function($form, $actionOptions) {
    foreach ($actionOptions['files'] as $field) {
        if (array_key_exists($field, $_FILES)) {
          move_uploaded_file($_FILES[$field]['tmp_name'], $actionOptions['target'].'/'.$_FILES[$field]['tmp_name']);
        }
    }

    return [
        'success' => true
    ];
};


return function($site, $pages, $page) {

    $form = uniform(
        'contact-form',
        array(
            'quard' => 'honeypot',
            'required' => [
                'salutation'  => '',
                'birthday'  => '',
                'first_name'  => '',
                'last_name'  => '',
                'email'  => 'email',
                ...
            ],
            'actions' => [
                [
                    '_action' => 'upload',
                    'files' => ['foto_upload', 'image_1', 'image_2'],
                    'target' => 'content/'.$page->diruri().'/files',
                ], [
                    '_action' => 'email',
                    'to'      => (string) $page->email(),
                    'sender'  => 'service@example.de',
                    'subject' => 'Example | New Application',
                    'snippet' => 'uniform-email-default',
                    'receive-copy' => false
                ]
            ]
        )
    );

   return compact('form');
};

?>

Does the content/3-application/files directory exist? The file upload won’t create it and will fail if it doesn’t exist already. Take a look at your PHP error log to see if any errors are reported there.

Edit: You can also check $_FILES[$field]['error'] === UPLOAD_ERR_OK to see whether the file was uploaded correctly.

It’s obviously not about the directory, if I log the error it says:

[17-Feb-2016 18:36:54 UTC] PHP Notice:  Undefined index: foto_upload in /Applications/MAMP/htdocs/example/site/controllers/application.php on line 9
[17-Feb-2016 18:36:54 UTC] 
[17-Feb-2016 18:36:54 UTC] PHP Notice:  Undefined index: passport_1 in /Applications/MAMP/htdocs/example/site/controllers/application.php on line 9
[17-Feb-2016 18:36:54 UTC] 
[17-Feb-2016 18:36:54 UTC] PHP Notice:  Undefined index: passport_2 in /Applications/MAMP/htdocs/example/site/controllers/application.php on line 9
[17-Feb-2016 18:36:54 UTC] 

So the $_FILES array is obviously empty. In this log I tried to upload foto_upload only

But how are these errors possible if you explictly check for the existence of the array keys?

if (array_key_exists($field, $_FILES)) {

Did you change the code of the action?

Also make sure you have the right encoding type for the form:

<form enctype="multipart/form-data" action="<?php echo $page->url()?>#form" method="post">

I’m checking for the errors outside the condition, within the foreach.
When I’m doing a vardump on $_POST/$_FILES, in POST i get the filename string set (it consequently also appears in the email). FILES is being empty.

EDIT: encoding is doing the trick. ouch. also, you got to specify the path of the folder you want to upload to from root.

@mzur: Thanks so much for your help. Where can I order you a :beers: ?

You’re welcome. You can give Uniform a star on GitHub if you like :wink: