ChronoForms 3.1 for Joomla! site Cookbook
上QQ阅读APP看书,第一时间看更新

Attaching a "standard" file to the e-mail

In the preceding recipe, we saw how to attach a file uploaded by the user, here we'll look at the similar task of attaching a file to an e-mail sent to the user. This time the file is not uploaded but is in a folder on the server somewhere.

Very often these are "terms and conditions" that you want to send out but we'll stay with the Newsletter theme and assume that we have a sample newsletter in a PDF file.

Getting ready

We'll be using the same form but this time, please make sure that the User Email Setup is enabled. You can also disable the administrator Email Setup if you like; we won't need it for this recipe.

Tip

You can enable or disable an Email Setup in the Email Properties box for the setup. Set the Enabled drop-down to YES or NO and click Apply.

You'll also need a file to attach. We'll use a file called newsletter.pdf that's been uploaded to the site with the site Media Manager into the root/images folder (that's the default folder for the Media Manager).

How to do it...

  1. ChronoForms doesn't have a box that you can fill in to do this so we need to add some code to the OnSubmit Before box in the Form Editor.

Note

If you already have code in the box - we added some in a previous recipe - that's fine. This code block can go before (or after) that one in the box.

ChronoForms adds the form attachments to a $attachments array and we are going to add an extra entry to that. The code is short, once you know what it is:

<?php
$form_id = $MyForm->formrow->id;
$MyUploads =& CFUploads::getInstance($form_id);
$MyUploads->attachments[] = 'images'.DS.'newsletter.pdf';
?>

Taking this line by line:

  • $form_id = $MyForm->formrow->id;

    This gets the form ID, the entry you see in the first column of the Forms Manager. This is the internal identifier for the form and must be unique.

  • $MyUploads =& CFUploads::getInstance($form_id);

    This gets a copy of a ChronoForms object that holds the uploads information for this form. In this case it's empty but if there had been files uploaded for the form, the information about them would be temporarily stored here.

  • $MyUploads->attachments[] = 'images'.DS.'newsletter.pdf';

    This adds a new "upload" entry to the ChronoForms Uploads Object, though in this case it's not an upload but our static file. ChronoForms isn't fussy and will quite happily accept this.

Note

Note that the path uses .DS. instead of / or \, because the Default Separator (DS) can vary depending on the server Joomla! adopts this more flexible version. We've used the partial path here 'images'.DS.'newsletter.pdf', to be a bit more thorough we could have added the fuller JPATH_ROOT.DS.'images'.DS.'newsletter.pdf'.

With those three lines in place ChronoForms will attach the sample newsletter file to each e-mail that it sends out.

Note

Note that if you have more than one e-mail setup then the file will be attached to both of them unless you disable attachments on one or the other. At the moment, there is no way of having two Email Setups with different file attachments (though it could be achieved by hand coding an e-mail setup).

How it works...

ChronoForms has built-in code to attach an uploaded file to an e-mail. We are hijacking that code to get it to send the standard file.

As we go on to more complex recipes we will quite often come back to this idea of "adopting and adapting" the standard features of ChronoForms.