file_mailsave.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/fileframework/contrib/mailsave/file_mailsave.module

Creates file attachments from email attachments

Functions & methods

NameDescription
file_mailsave_mailImplementation of hook_mail().
file_mailsave_mailsaveImplementation of hook_mailsave().
file_mailsave_permImplementation of hook_perm().
_file_mailsave_mail_textReturns a mail string for a variable name.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Creates file attachments from email attachments
  5. */
  6. //////////////////////////////////////////////////////////////////////////////
  7. // Core API hooks
  8. /**
  9. * Implementation of hook_perm().
  10. */
  11. function file_mailsave_perm() {
  12. return array('attach files from email');
  13. }
  14. /**
  15. * Implementation of hook_mail().
  16. */
  17. function file_mailsave_mail($key, &$message, $params) {
  18. $language = $message['language'];
  19. $account = isset($params['account']) ? $params['account'] : (object)array();
  20. $variables = array_merge(user_mail_tokens($account, $language), $params['variables'] ? $params['variables'] : array());
  21. $message['subject'] .= _file_mailsave_mail_text($key .'_subject', $language, $variables);
  22. $message['body'][] = _file_mailsave_mail_text($key .'_body', $language, $variables);
  23. }
  24. //////////////////////////////////////////////////////////////////////////////
  25. // Mailsave API hooks
  26. /**
  27. * Implementation of hook_mailsave().
  28. */
  29. function file_mailsave_mailsave($node, $result, $i, $header, $mailbox) {
  30. if (!user_access('attach files from email'))
  31. return;
  32. $node->files = array();
  33. $i = 0;
  34. foreach ($node->mailsave_attachments as $file) {
  35. $file = (object)$file;
  36. // Validate file upload.
  37. $errors = file_validate_file($file);
  38. if (!empty($errors)) {
  39. $account = user_load($node->uid);
  40. $variables = array('!filename' => $file->filename, '!subject' => $header->subject, '!errors' => strip_tags(implode("\n", $errors)));
  41. $params = array('account' => $account, 'variables' => $variables);
  42. $message = drupal_mail('file_mailsave', 'file', $account->mail, user_preferred_language($account), $params);
  43. }
  44. else {
  45. $file_node = (object)array('nosave' => TRUE);
  46. if (!file_node_save($file_node, $file)) {
  47. watchdog('file_mailsave', 'Error saving file %file. Please, contact site administrator.', array('%file' => $file->filename), WATCHDOG_ERROR);
  48. }
  49. else {
  50. $file_node->file->list = 1;
  51. $file_node->file->weight = 0;
  52. $file_node->file->sid = 's_'. $i;
  53. $node->files['s_'. $i] = (array)$file_node->file;
  54. }
  55. }
  56. // mailsave creates temporal files with and without '_0' suffix.
  57. file_delete($file->filepath);
  58. file_delete(drupal_substr($file->filepath, 0, drupal_strlen($file->filepath)-2));
  59. $i++;
  60. }
  61. unset($node->mailsave_attachments);
  62. return $node;
  63. }
  64. //////////////////////////////////////////////////////////////////////////////
  65. // Mail strings
  66. /**
  67. * Returns a mail string for a variable name.
  68. *
  69. * Used by file_mailsave_mail() and the settings forms to retrieve strings.
  70. */
  71. function _file_mailsave_mail_text($key, $language = NULL, $variables = array()) {
  72. $langcode = isset($language) ? $language->language : NULL;
  73. if ($admin_setting = variable_get('file_mailsave_mail_'. $key, FALSE)) {
  74. // An admin setting overrides the default string.
  75. return t($admin_setting, $variables, $langcode);
  76. }
  77. else {
  78. // No override, return default string.
  79. switch ($key) {
  80. case 'file_subject':
  81. return t('Re: !subject', $variables, $langcode);
  82. case 'file_body':
  83. return t("Saving file !filename at !site failed with the following error:\n\n!errors", $variables, $langcode);
  84. }
  85. }
  86. }