4 * @file classes/mail/PKPMailTemplate.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PKPMailTemplate
12 * @brief Subclass of Mail for mailing a template email.
15 // $Id: PKPMailTemplate.inc.php,v 1.7 2009/09/22 21:18:27 asmecher Exp $
20 define('MAIL_ERROR_INVALID_EMAIL', 0x000001);
22 class PKPMailTemplate
extends Mail
{
24 /** @var $emailKey string Key of the email template we are using */
27 /** @var $locale string locale of this template */
30 /** @var $enabled boolean email template is enabled */
33 /** @var $errorMessages array List of errors to display to the user */
36 /** @var $persistAttachments array List of temporary files belonging to
37 email; these are maintained between requests and only sent to the
38 attachment handling functions in Mail.inc.php at time of send. */
39 var $persistAttachments;
40 var $attachmentsEnabled;
42 /** @var $skip boolean If set to true, this message has been skipped
43 during the editing process by the user. */
46 /** @var $bccSender boolean whether or not to bcc the sender */
49 /** @var boolean Whether or not email fields are disabled */
50 var $addressFieldsEnabled;
54 * @param $emailKey string unique identifier for the template
55 * @param $locale string locale of the template
56 * @param $enableAttachments boolean optional Whether or not to enable article attachments in the template
57 * @param $includeSignature boolean optional
59 function PKPMailTemplate($emailKey = null, $locale = null, $enableAttachments = null, $includeSignature = true) {
61 $this->emailKey
= isset($emailKey) ?
$emailKey : null;
63 // Use current user's locale if none specified
64 $this->locale
= isset($locale) ?
$locale : Locale
::getLocale();
66 // Record whether or not to BCC the sender when sending message
67 $this->bccSender
= Request
::getUserVar('bccSender');
69 // If enableAttachments is null, use the default value from the
71 if ($enableAttachments === null) {
72 $enableAttachments = Config
::getVar('email', 'enable_attachments')?
true:false;
75 $user =& Request
::getUser();
76 if ($enableAttachments && $user) {
77 $this->_handleAttachments($user->getId());
79 $this->attachmentsEnabled
= false;
82 $this->addressFieldsEnabled
= true;
86 * Disable or enable the address fields on the email form.
87 * NOTE: This affects the displayed form ONLY; if disabling the address
88 * fields, callers should manually clearAllRecipients and add/set
89 * recipients just prior to sending.
90 * @param $addressFieldsEnabled boolean
92 function setAddressFieldsEnabled($addressFieldsEnabled) {
93 $this->addressFieldsEnabled
= $addressFieldsEnabled;
97 * Get the enabled/disabled state of address fields on the email form.
100 function getAddressFieldsEnabled() {
101 return $this->addressFieldsEnabled
;
105 * Check whether or not there were errors in the user input for this form.
106 * @return boolean true iff one or more error messages are stored.
108 function hasErrors() {
109 return ($this->errorMessages
!= null);
113 * Assigns values to e-mail parameters.
114 * @param $paramArray array
117 function assignParams($paramArray = array()) {
118 $subject = $this->getSubject();
119 $body = $this->getBody();
121 // Replace variables in message with values
122 foreach ($paramArray as $key => $value) {
123 if (!is_object($value)) {
124 $subject = str_replace('{$' . $key . '}', $value, $subject);
125 $body = str_replace('{$' . $key . '}', $value, $body);
129 $this->setSubject($subject);
130 $this->setBody($body);
134 * Returns true if the email template is enabled; false otherwise.
137 function isEnabled() {
138 return $this->enabled
;
142 * Processes form-submitted addresses for inclusion in
144 * @param $currentList array Current recipient/cc/bcc list
145 * @param $newAddresses array "Raw" form parameter for additional addresses
147 function &processAddresses($currentList, &$newAddresses) {
148 foreach ($newAddresses as $newAddress) {
150 // Match the form "My Name <my_email@my.domain.com>"
151 if (String::regexp_match_get('/^([^<>' . "\n" . ']*[^<> ' . "\n" . '])[ ]*<(?P<email>' . PCRE_EMAIL_ADDRESS
. ')>$/i', $newAddress, $regs)) {
152 $currentList[] = array('name' => $regs[1], 'email' => $regs['email']);
154 } elseif (String::regexp_match_get('/^<?(?P<email>' . PCRE_EMAIL_ADDRESS
. ')>?$/i', $newAddress, $regs)) {
155 $currentList[] = array('name' => '', 'email' => $regs['email']);
157 } elseif ($newAddress != '') {
158 $this->errorMessages
[] = array('type' => MAIL_ERROR_INVALID_EMAIL
, 'address' => $newAddress);
165 * Displays an edit form to customize the email.
166 * @param $formActionUrl string
167 * @param $hiddenFormParams array
170 function displayEditForm($formActionUrl, $hiddenFormParams = null, $alternateTemplate = null, $additionalParameters = array()) {
172 $form = new Form($alternateTemplate!=null?
$alternateTemplate:'email/email.tpl');
174 $form->setData('formActionUrl', $formActionUrl);
175 $form->setData('subject', $this->getSubject());
176 $form->setData('body', $this->getBody());
178 $form->setData('to', $this->getRecipients());
179 $form->setData('cc', $this->getCcs());
180 $form->setData('bcc', $this->getBccs());
181 $form->setData('blankTo', Request
::getUserVar('blankTo'));
182 $form->setData('blankCc', Request
::getUserVar('blankCc'));
183 $form->setData('blankBcc', Request
::getUserVar('blankBcc'));
184 $form->setData('from', $this->getFromString(false));
186 $form->setData('addressFieldsEnabled', $this->getAddressFieldsEnabled());
188 $user =& Request
::getUser();
190 $form->setData('senderEmail', $user->getEmail());
191 $form->setData('bccSender', $this->bccSender
);
194 if ($this->attachmentsEnabled
) {
195 $form->setData('attachmentsEnabled', true);
196 $form->setData('persistAttachments', $this->persistAttachments
);
199 $form->setData('errorMessages', $this->errorMessages
);
201 if ($hiddenFormParams != null) {
202 $form->setData('hiddenFormParams', $hiddenFormParams);
205 foreach ($additionalParameters as $key => $value) {
206 $form->setData($key, $value);
214 * Aside from calling the parent method, this actually attaches
215 * the persistent attachments if they are used.
216 * @param $clearAttachments boolean Whether to delete attachments after
218 function send($clearAttachments = true) {
219 if ($this->attachmentsEnabled
) {
220 foreach ($this->persistAttachments
as $persistentAttachment) {
221 $this->addAttachment(
222 $persistentAttachment->getFilePath(),
223 $persistentAttachment->getOriginalFileName(),
224 $persistentAttachment->getFileType()
229 $user =& Request
::getUser();
231 if ($user && $this->bccSender
) {
232 $this->addBcc($user->getEmail(), $user->getFullName());
235 if (isset($this->skip
) && $this->skip
) {
238 $result = parent
::send();
241 if ($clearAttachments && $this->attachmentsEnabled
) {
242 $this->_clearAttachments($user->getId());
249 * Assigns user-specific values to email parameters, sends
250 * the email, then clears those values.
251 * @param $paramArray array
254 function sendWithParams($paramArray) {
255 $savedHeaders = $this->getHeaders();
256 $savedSubject = $this->getSubject();
257 $savedBody = $this->getBody();
259 $this->assignParams($paramArray);
261 $ret = $this->send();
263 $this->setHeaders($savedHeaders);
264 $this->setSubject($savedSubject);
265 $this->setBody($savedBody);
271 * Clears the recipient, cc, and bcc lists.
272 * @param $clearHeaders boolean if true, also clear headers
275 function clearRecipients($clearHeaders = true) {
276 $this->setData('recipients', null);
277 $this->setData('ccs', null);
278 $this->setData('bccs', null);
280 $this->setData('headers', null);
285 * Adds a persistent attachment to the current list.
286 * Persistent attachments MUST be previously initialized
287 * with handleAttachments.
289 function addPersistAttachment($temporaryFile) {
290 $this->persistAttachments
[] = $temporaryFile;
294 * Handles attachments in a generalized manner in situations where
295 * an email message must span several requests. Called from the
296 * constructor when attachments are enabled.
298 function _handleAttachments($userId) {
299 import('file.TemporaryFileManager');
300 $temporaryFileManager = new TemporaryFileManager();
302 $this->attachmentsEnabled
= true;
303 $this->persistAttachments
= array();
305 $deleteAttachment = Request
::getUserVar('deleteAttachment');
307 if (Request
::getUserVar('persistAttachments') != null) foreach (Request
::getUserVar('persistAttachments') as $fileId) {
308 $temporaryFile = $temporaryFileManager->getFile($fileId, $userId);
309 if (!empty($temporaryFile)) {
310 if ($deleteAttachment != $temporaryFile->getId()) {
311 $this->persistAttachments
[] = $temporaryFile;
313 // This file is being deleted.
314 $temporaryFileManager->deleteFile($temporaryFile->getId(), $userId);
319 if (Request
::getUserVar('addAttachment') && $temporaryFileManager->uploadedFileExists('newAttachment')) {
320 $user =& Request
::getUser();
322 $this->persistAttachments
[] = $temporaryFileManager->handleUpload('newAttachment', $user->getId());
326 function getAttachmentFiles() {
327 if ($this->attachmentsEnabled
) return $this->persistAttachments
;
332 * Delete all attachments associated with this message.
333 * Called from send().
336 function _clearAttachments($userId) {
337 import('file.TemporaryFileManager');
338 $temporaryFileManager = new TemporaryFileManager();
340 $persistAttachments = Request
::getUserVar('persistAttachments');
341 if (is_array($persistAttachments)) foreach ($persistAttachments as $fileId) {
342 $temporaryFile = $temporaryFileManager->getFile($fileId, $userId);
343 if (!empty($temporaryFile)) {
344 $temporaryFileManager->deleteFile($temporaryFile->getId(), $userId);