
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.
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.
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).
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).