You are here

function _mimemail_file in Mime Mail 7

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

Helper function to extract local files.

Parameters

string $url: (optional) The URI or the absolute URL to the file.

string $content: (optional) The actual file content.

string $name: (optional) The file name.

string $type: (optional) The file type.

string $disposition: (optional) The content disposition. Defaults to inline.

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
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 166
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) {
    $image = preg_match('!\\.(png|gif|jpg|jpeg)$!i', $url);
    $linkonly = variable_get('mimemail_linkonly', 0);

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

      // The $url is absolute, we're done here.
      $scheme = file_uri_scheme($url);
      if ($scheme == 'http' || $scheme == 'https' || preg_match('!mailto:!', $url) || preg_match('!^data:!', $url)) {
        return $url;
      }
      else {
        $file = drupal_realpath($url) ? drupal_realpath($url) : file_create_url($url);
      }
    }
  }
  elseif ($content) {
    $file = $content;
  }
  if (isset($file)) {
    $is_file = @is_file($file);
    if ($is_file) {
      $access = user_access('send arbitrary files');
      $in_public_path = strpos(@drupal_realpath($file), drupal_realpath('public://')) === 0;
      if (!$in_public_path && !$access) {
        return $url;
      }
    }
    if (!$name) {
      $name = $is_file ? basename($file) : 'attachment.dat';
    }
    if (!$type) {
      $type = $is_file ? file_get_mimetype($file) : file_get_mimetype($name);
    }
    $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;
}