You are here

function _mimemail_file in Mime Mail 6

Same name and namespace in other branches
  1. 5 mimemail.inc \_mimemail_file()
  2. 7 mimemail.inc \_mimemail_file()

Helper function to extract local files.

Parameters

$url: The URL of the file.

$content: The actual file content.

Return value

The Content-ID and/or an array of the files on success or the URL on failure.

3 calls to _mimemail_file()
mimemail_extract_files in ./mimemail.inc
Helper function to extracts links to local images from HTML documents.
mimemail_html_body in ./mimemail.inc
Generate a multipart message body with a text alternative for some HTML text.
_mimemail_replace_files in ./mimemail.inc
Callback function for preg_replace_callback().

File

./mimemail.inc, line 142
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

function _mimemail_file($url = NULL, $content = NULL, $name = '', $type = '', $disposition = 'inline') {
  static $files = array();
  static $ids = array();
  if ($url) {

    // The file exists on the server as-is. Allows for non-web-accessible files.
    if (@is_file($url)) {
      $file = $url;
    }
    else {
      $url = _mimemail_url($url, 'TRUE');

      // If the url is absolute, we're done here.
      if (strpos($url, '://') !== FALSE || preg_match('!(mailto|callto|tel)\\:!', $url)) {
        return $url;
      }
      else {

        // Make sure ImageCache images are existing before trying to attach.
        if (module_exists('imagecache') && strpos($url, 'imagecache') !== FALSE) {
          _mimemail_imagecache($url);
        }

        // Download method is private, and the URL needs conversion.
        if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && strpos($url, 'system/files/') !== 0) {
          $file = file_create_path(drupal_substr($url, strpos($url, 'system/files/') + drupal_strlen('system/files/')));
        }
        else {
          $file = $url;
        }
      }
    }
  }
  elseif ($content) {
    $file = $content;
  }
  if (isset($file) && (@is_file($file) || $content)) {
    $public_path = file_directory_path();
    $no_access = !user_access('send arbitrary files - Warning: has security implications!');
    $not_in_public_path = strpos(realpath($file), realpath($public_path)) === FALSE;
    if (@is_file($file) && $not_in_public_path && $no_access) {
      return $url;
    }
    if (!$name) {
      $name = @is_file($file) ? basename($file) : 'attachment.dat';
    }
    if (!$type) {
      $type = $name ? file_get_mimetype($name) : file_get_mimetype($file);
    }
    $id = md5($file) . '@' . $_SERVER['HTTP_HOST'];

    // Prevent duplicate items.
    if (isset($ids[$id])) {
      return 'cid:' . $ids[$id];
    }
    $new_file = array(
      'name' => $name,
      'file' => $file,
      'Content-ID' => $id,
      'Content-Disposition' => $disposition,
      'Content-Type' => $type,
    );
    $files[] = $new_file;
    $ids[$id] = $id;
    return 'cid:' . $id;
  }
  elseif ($url) {
    return $url;
  }
  $ret = $files;
  $files = array();
  $ids = array();
  return $ret;
}