Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Hello,

I have a form that has a file upload field which uploads to the server, then the form submits to an email address.

I realise I can't add the file as an attachment, but I'd like to provide a direct link to the attachment as part of the email confirmation.

I've experimented with the show_securefile tags and also found a secret function used in the admin when showing the file name, but I can't get it to work. Any ideas?

Code: Select all
<cms:send_mail from='******' to="*******" html='1' subject='Job Application Form Filled Out'>
                           <p><strong>Name:</strong> <cms:show frm_name /></p>
                           <p><strong>Telephone:</strong> <cms:show frm_tel /></p>
                           <p><strong>Email Address:</strong> <cms:show frm_email /></p>
                           <p><strong>Address:</strong> <cms:nl2br><cms:show frm_address /></cms:nl2br></p>
                                    <p><strong>Position:</strong> <cms:show frm_position /></p>
                           <cms:show_securefile 'frm_ulfile' >
                              <cms:dump/>
                              <cms:php>
                                 $data = "<cms:show file_id />" . '|0|0|0|0|2|0|0';
                                 $link = K_ADMIN_URL . 'download.php?auth=' . urlencode( $data . '|' . $FUNCS->hash_hmac($data, $FUNCS->hash_hmac($data, $FUNCS->get_secret_key())) );
                                 echo '<p><strong><a href="' . $link . '">Download CV</a></strong></p>';
                              </cms:php>   
                           </cms:show_securefile>
                        </cms:send_mail>
Hi,

You mentioned -
I realise I can't add the file as an attachment,

That actually is no longer the case - please see viewtopic.php?f=5&t=10750

I suggest you please try using the method given the mentioned post and let us know if you still have any problem.

Hope this helps.
Thanks, I'll take a look.

Out of interest, is the way I was attempting achievable?
Out of interest, is the way I was attempting achievable?

Yes, I think it should be achievable - I did not look deep enough into your code to find what is going wrong though as there is a better alternative available.
One thing I forgot to add is that at the same time as submitting the form, the details are also being added using db_persist_form so each of the fields are of 'bound' type - is that going to make a difference?

Code: Select all
<div>
                                <label for="name">Name</label>
                                <cms:input type="bound" name="name" class="input" placeholder="Please enter a name" required='1' "required" />
                            </div>
                            <div>
                                <label for="email">Email Address</label>
                                <cms:input type="bound" name="email" class="input" placeholder="Please enter a valid email address" required='1' validator='email' />
                            </div>
                            <div>
                                <label for="tel">Telephone</label>
                                <cms:input type="bound" name="tel" class="input" placeholder="Please enter a telephone number" required='1' "required" />
                            </div>
                            <div>
                                <label for="address">Address</label>
                                <cms:input type="bound" name="address" class="input textarea" placeholder="Address" />
                            </div>
                            <div>
                                <label for="ulfile">Upload Your CV</label>
                        <div class="file">
                           <cms:input id="ulfile" name="ulfile" type="bound" />
                        </div>
                     </div>
This is what I have now:

Code: Select all
<cms:send_mail from='*******' to="<cms:show recipient />" html='1' subject='Job Application Form Filled Out'>
   <p><strong>Name:</strong> <cms:show frm_name /></p>
   <p><strong>Telephone:</strong> <cms:show frm_tel /></p>
   <p><strong>Email Address:</strong> <cms:show frm_email /></p>
   <p><strong>Address:</strong> <cms:nl2br><cms:show frm_address /></cms:nl2br></p>
   <p><strong>Position:</strong> <cms:show frm_position /></p>
   <cms:pages masterpage=k_template_name id=k_last_insert_id show_future_entries='1'>
      <cms:show_securefile 'ulfile' >
         <cms:set my_file_link = "<cms:securefile_link file_id />" />
         <cms:attachment file=my_file_link  />
      </cms:show_securefile>
   </cms:pages>
</cms:send_mail>


I've tried the file input field with a type of 'bound' (doesn't work) and 'uploadfile' (submits the form, but no attachment in email and doesn't save to server). Not sure what I'm missing...
Any ideas?
@arrivaldesign, there are two distinct types of editable regions we can use in a form for handling uploaded files -
1. Type 'securefile' editable region
2. Type 'uploadfile' editable region
The difference between the two being that a file uploaded through type 'securefile' will also be saved permanently (on the server with an entry in the database ) while that of 'uploadfile' is ephemeral.

The two will require different treatment to convert the uploads into email attachments but you seem to be mixing up both in the same code.

Please first clarify which of two you wish to have.
If it is not necessary for your use-case to save the uploads on the server and only sending as attachment is fine, please go with the full working code given at viewtopic.php?f=5&t=10750
If, however, you need to have the 'securefile' type, let me know and I'll try to rectify your original code.
Hi KK,

I need to be able to save to the server and also send the file uploaded as an attachment.

Thanks.
Hi,

There is a post in the same thread I mentioned showing the use of securefile for attachments -
viewtopic.php?f=5&t=10750#p31284

Anyway, following is a fuller treatment to make things easier for you (and perhaps others).

Assume following is the definition of fields in the clonable template (for this example it is 'target.php') that will be used to persist the submitted forms -
Code: Select all
<cms:template title='Target' clonable='1'>

    <cms:editable name='name' type='text' required='1' />
    <cms:editable name='email' type='text' validator='email' required='1' />
    <cms:editable name='resume' required='1' allowed_ext='pdf, doc, docx, txt' max_size='1024' type='securefile' />

</cms:template>

Notice the use of type 'securefile' region named 'resume' above.

Following is the code for the form that both persists the submitted data as well sends an email with the resume as attachment -
Please make sure to set your actual template's name in its first statement (it is 'target.php' in this sample code).
Code: Select all
<cms:set my_target_template='target.php' />

<cms:if "<cms:get_flash 'submit_success' />" >
    <h1>Thank you for contacting us!</h1>
</cms:if>

<cms:form
    masterpage=my_target_template
    mode='create'
    enctype='multipart/form-data'
    method='post'
    anchor='0'
    >
    <cms:if k_success >
        <!-- try persisting submission in database -->
        <cms:db_persist_form
            _invalidate_cache='0'
            _auto_title='1'
        />

        <cms:if k_success >

          <!-- get the link of the uploaded attachment. For that, fetch the securefile from the page we just persisted.. -->
          <cms:pages masterpage=my_target_template id=k_last_insert_id show_future_entries='1'>
            <cms:show_securefile 'resume' >
                <!-- and get its physical path and extension.. -->
                <cms:set my_attachment_link = "<cms:securefile_link file_id />" scope='global' />
                <cms:set my_attachment_extension = file_ext scope='global' />
            </cms:show_securefile>
          </cms:pages>

          <!-- email submitted data -->
          <cms:send_mail from='contact@mysite.com' to='admin@mysite.com' reply_to=frm_email subject='Contact Form Submission'>
              The following is an email sent by a visitor to your site:
              Name: <cms:show frm_name />,
              Email: <cms:show frm_email />,

              <cms:attachment file=my_attachment_link name="resume.<cms:show my_attachment_extension />" />
          </cms:send_mail>

          <!-- and redirect to refresh page -->
          <cms:set_flash name='submit_success' value='1' />
          <cms:redirect k_page_link />
        </cms:if>
    </cms:if>

    <cms:if k_error >
        <ul>
            <cms:each k_error >
                <li><cms:show item /></li>
            </cms:each>
        </ul>
    </cms:if>

    Name: <cms:input type='bound' name='name' /><br />
    Email: <cms:input type='bound' name='email' /><br />
    Resume: <cms:input name="resume" type="bound" /><br />
    <br />
    <input type="submit" value="Submit" name='submit'>
</cms:form>

This code is fully working and tested so first use it with minimal modifications. Add your changes once you see that it is working as expected.

Hope this helps.
10 posts Page 1 of 1