by
KK » Wed May 02, 2012 6:51 am
Hi Emanuale,
As you mentioned, attachments are not currently supported by cms:form (as well as the cms:send_mail tag).
I have, however, created an extension for you that tries to add the missing functionality.
These are the two steps you need to carry out to get the extension working (will require at least Couch v1.2)-
1. Download the attached 'emailex.php' (unzip it first) and place it within your couch installation folder.
2. Edit (or create a file named) 'kfunctions.php' (we discussed this in
using Shortcodes) within your site's root folder and add the highlited line below at the top of it
<?php
if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly
require_once( K_COUCH_DIR.'emailex.php' );
This will make our extension ('emailex.php') now a part of Couch.
Now for the Contact form.
There are two distinct things that need to be done
1. Upload a file
2. Send the uploaded file as Email attachment.
Upload a fileSince there is no cms:input tag in Couch (currently) that allows uploading of files, we have to take a little roundabout way.
In your form define the normal HTML input tag that is used for uploading e.g.
- Code: Select all
<input type="file" name="my_file_ex">
Now immediately below it add a Couch input tag like this:
- Code: Select all
<cms:input
type="hidden"
name="my_file"
required='1'
validator='EmailEx::validate=max_size:512&allowed_ext:jpeg,jpg,gif,png'
value='1'
/>
This tag works as a sort of a proxy for the real 'input type="file"' tag above it and will do all the grunge work for us.
V.IMP: The thing that associates this tag with the real tag is the 'name' parameter. If the cms:input tag's name is 'my_file', the real input tag's name should have a '_ex' appended to make it 'my_file_ex'.Note the 'validator' parameter where we make use of a routine provided by our extension.
The 'max_size' is the maximum size (in KB) you wish to permit for the uploaded files.
The 'allowed_ext' is a list of permitted extensions. For the CVs that will be uploaded in your case, this setting could be
validator='EmailEx::validate=max_size:512&allowed_ext:doc,txt'
The 'required' parameter can be used to make the uploading of file mandatory or not.
This will take care of uploading a file. Now to send the email with this uploaded file attached.
Send the uploaded file as Email attachmentNormally, we check for the 'k_success' condition in our form before using cms:send_mail to send the mail with the submitted values.
Since the cms:send_mail does not support attachments, we'll now use a PHP routine provide by our extension instead. e.g.
- Code: Select all
<cms:php>
EmailEx::send_mail( 'sender@mail.com', 'receiver@mail.com', 'Subject', 'Message' );
</cms:php>
This will automatically attach any uploaded to the mail being sent.
Following is a complete working sample form that sends email with attachments:
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:form method="POST" enctype="multipart/form-data" anchor='0'>
<cms:if k_success >
<h3>Thank you for contacting us!</h3>
<cms:capture into='my_message'>
The following is an email sent by a visitor to your site:
Name: <cms:show frm_name />
Email: <cms:show frm_email />
Message: <cms:show frm_message />
</cms:capture>
<cms:php>
EmailEx::send_mail( '<cms:show frm_email />', 'youraddress@gmail.com', 'Feedback from your site', '<cms:show my_message />' );
</cms:php>
<cms:else />
<cms:if k_error >
<ul>
<cms:each k_error >
<li><cms:show item /></li>
</cms:each>
</ul>
</cms:if>
Name: <cms:input type="text" name="name" /><br />
Email: <cms:input type="text" name="email" validator='email' required='1' /><br />
Message: <cms:input type="textarea" name="message" /><br />
Upload File: <input type="file" name="my_file_ex"><br />
<cms:input
type="hidden"
name="my_file"
required='1'
validator='EmailEx::validate=max_size:512&allowed_ext:jpeg,jpg,gif,png,txt'
value='1'
/>
<input type="submit" value="Submit" name='submit'>
</cms:if>
</cms:form>
<?php COUCH::invoke(); ?>
Take care to set the
enctype="multipart/form-data" attribute of your form for attachments to work. Also, use you own email address as recipient.
Please test this extension out and let me know it does the job.
Thanks.