6.x-2.x branch
Creates file attachments from email attachments
| Name | Description |
|---|---|
| file_mailsave_mail | Implementation of hook_mail(). |
| file_mailsave_mailsave | Implementation of hook_mailsave(). |
| file_mailsave_perm | Implementation of hook_perm(). |
| _file_mailsave_mail_text | Returns a mail string for a variable name. |
- <?php
-
- /**
- * @file
- * Creates file attachments from email attachments
- */
-
- //////////////////////////////////////////////////////////////////////////////
- // Core API hooks
-
- /**
- * Implementation of hook_perm().
- */
- function file_mailsave_perm() {
- return array('attach files from email');
- }
-
- /**
- * Implementation of hook_mail().
- */
- function file_mailsave_mail($key, &$message, $params) {
- $language = $message['language'];
- $account = isset($params['account']) ? $params['account'] : (object)array();
- $variables = array_merge(user_mail_tokens($account, $language), $params['variables'] ? $params['variables'] : array());
- $message['subject'] .= _file_mailsave_mail_text($key .'_subject', $language, $variables);
- $message['body'][] = _file_mailsave_mail_text($key .'_body', $language, $variables);
- }
-
- //////////////////////////////////////////////////////////////////////////////
- // Mailsave API hooks
-
- /**
- * Implementation of hook_mailsave().
- */
- function file_mailsave_mailsave($node, $result, $i, $header, $mailbox) {
- if (!user_access('attach files from email'))
- return;
-
- $node->files = array();
- $i = 0;
- foreach ($node->mailsave_attachments as $file) {
- $file = (object)$file;
-
- // Validate file upload.
- $errors = file_validate_file($file);
- if (!empty($errors)) {
- $account = user_load($node->uid);
- $variables = array('!filename' => $file->filename, '!subject' => $header->subject, '!errors' => strip_tags(implode("\n", $errors)));
- $params = array('account' => $account, 'variables' => $variables);
- $message = drupal_mail('file_mailsave', 'file', $account->mail, user_preferred_language($account), $params);
- }
- else {
- $file_node = (object)array('nosave' => TRUE);
- if (!file_node_save($file_node, $file)) {
- watchdog('file_mailsave', 'Error saving file %file. Please, contact site administrator.', array('%file' => $file->filename), WATCHDOG_ERROR);
- }
- else {
- $file_node->file->list = 1;
- $file_node->file->weight = 0;
- $file_node->file->sid = 's_'. $i;
- $node->files['s_'. $i] = (array)$file_node->file;
- }
- }
- // mailsave creates temporal files with and without '_0' suffix.
- file_delete($file->filepath);
- file_delete(drupal_substr($file->filepath, 0, drupal_strlen($file->filepath)-2));
- $i++;
- }
- unset($node->mailsave_attachments);
- return $node;
- }
-
- //////////////////////////////////////////////////////////////////////////////
- // Mail strings
-
- /**
- * Returns a mail string for a variable name.
- *
- * Used by file_mailsave_mail() and the settings forms to retrieve strings.
- */
- function _file_mailsave_mail_text($key, $language = NULL, $variables = array()) {
- $langcode = isset($language) ? $language->language : NULL;
-
- if ($admin_setting = variable_get('file_mailsave_mail_'. $key, FALSE)) {
- // An admin setting overrides the default string.
- return t($admin_setting, $variables, $langcode);
- }
- else {
- // No override, return default string.
- switch ($key) {
- case 'file_subject':
- return t('Re: !subject', $variables, $langcode);
- case 'file_body':
- return t("Saving file !filename at !site failed with the following error:\n\n!errors", $variables, $langcode);
- }
- }
- }
-
-