Problems, need help? Have a tip or advice? Post it here.
14 posts Page 2 of 2
@madeby, you had asked -
@kk So this is the only way of having a file attached to a sent form?

I would need it as part of the registration process for the members module, that is why I am asking :)

To which my reply was -
@madebym, for 'members' module this seems to be the only available option, I'm afraid.
For 'extended-users', however, we could use 'securefile' regions for the attachments during registration.

As it turns out, we can, in fact, use 'secure' file with members module also.
Let me explain how.

As an example let us take the sample member registration form from the docs -
Code: Select all
<cms:form enctype="multipart/form-data" method='post' anchor='0'>
    <cms:if k_success >
        <!--
            The 'member_process_registration_form' tag below expects fields named
            'member_displayname', 'member_name' (optional), 'member_email',
            'member_password' and 'member_password_repeat'
        -->
        <cms:member_process_registration_form />
       
        <cms:if k_success >
            <cms:set_flash name='success_msg' value='1' />
            <cms:redirect k_page_link />
        </cms:if>
    </cms:if>

    <cms:if k_error >
        <font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
    </cms:if>
   
   
    Screen Name:<br />
    <cms:input name='member_displayname' type='text' /><br />
   
    Email Address:<br />
    <cms:input name='member_email' type='text' /><br />
       
    Password:<br />
    <cms:input name='member_password' type='password' /><br />
   
    Repeat Password:<br />
    <cms:input name='member_password_repeat' type='password' /><br />
   
    <input type="submit" name="submit" value="Create account"/>
   
</cms:form>

To add a 'secure' file to the mix, first define a securefile region in the members template e.g. as follows
Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
    <cms:template clonable='1' title='Members'>
       
        <cms:member_define_fields />
       
        <!--
            Fields for 'email', 'password' and 'active' already come pre-defined.
            If more fields are required, they can be defined here below in the usual manner.
        -->     

       <cms:editable type='securefile' name='avatar' allowed_ext='gif, jpg, png'  show_preview='1' required='1' /> 


    </cms:template>
<?php COUCH::invoke(); ?>

Then amend the registration form as follows -
Code: Select all
<cms:form enctype="multipart/form-data" method='post' anchor='0' 
    masterpage=k_member_template
    mode='create'
>
    <cms:if k_success >
        <!--
            The 'member_process_registration_form' tag below expects fields named
            'member_displayname', 'member_name' (optional), 'member_email',
            'member_password' and 'member_password_repeat'
        -->
        <cms:member_process_registration_form
            _send_mail='0'
            avatar=frm_avatar
        />
       
        <cms:if k_success >
            <cms:set_flash name='success_msg' value='1' />
            <cms:redirect k_page_link />
        </cms:if>
    </cms:if>

    <cms:if k_error >
        <font color='red'><cms:each k_error ><cms:show item /><br /></cms:each></font>
    </cms:if>
   
   
    Screen Name:<br />
    <cms:input name='member_displayname' type='text' /><br />
   
    Email Address:<br />
    <cms:input name='member_email' type='text' /><br />
       
    Password:<br />
    <cms:input name='member_password' type='password' /><br />
   
    Repeat Password:<br />
    <cms:input name='member_password_repeat' type='password' /><br />
   
    Avatar:<br />
    <cms:input name='avatar' type='bound' /><br />
   
    <br />
    <input type="submit" name="submit" value="Create account"/>
   
</cms:form>

And now a registering user can upload her avatar along with the other data.

The trick above was to convert the form into a 'Databound' form by adding the 'masterpage' attribute pointing to the member template. The changes are highlighted below -
<cms:form enctype="multipart/form-data" method='post' anchor='0'
masterpage=k_member_template
mode='create'

>
<cms:if k_success >
<cms:member_process_registration_form
_send_mail='0'
avatar=frm_avatar
/>
...
</cms:if>

..

Avatar:<br />
<cms:input name='avatar' type='bound' /><br />

..
</cms:form>

Hope it helps.
Hi, @KK

To date, this viewtopic.php?f=8&t=6959 is the only way to send attachments with email? Just to make sure, as that topic is a pretty dated one and new couch versions have come since that. Need to know this, as I have to send up to 3 attached files in email.
Thanks!
@trendoman, yes, there is still no native way available to add attachments to emails.
Hello KK,
I a using the phpmailer addon with file attachment but I keep getting this error: uploaded_img: File extension not allowed. I downloaded and installed the latest couch version even though I had done that already, tried uploading all the file types allowed in the allowed_ext attribute but still getting the same error.

This is my code:
Code: Select all
<cms:if "<cms:get_flash 'submit_success' />" >
    <h1>Thank you for contacting us!</h1>
</cms:if>

<cms:form method="POST" enctype="multipart/form-data" anchor='0'>
    <cms:if k_success >
        <!-- 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 />,
            Message: <cms:show frm_message />
           
            Attachment:
          <cms:if frm_uploaded_img >
           <img src="<cms:attachment field='uploaded_img' inline='1' />" />
          </cms:if>

        </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 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 />

Attachement: <cms:input type='uploadfile' name='uploaded_img' required='0' allowed_ext='jpeg,jpg,gif,png' max_size='512' /><br />

    <input type="submit" value="Submit" name='submit'>
</cms:form>
14 posts Page 2 of 2