You are here

public static function DrupalMandrill::getAttachmentStruct in Mandrill 7

Return an array structure for a message attachment.

Parameters

string $path: Attachment path.

Return value

array Attachment structure.

Throws

MandrillException

Exception

File

lib/mandrill.inc, line 426
Wrapper class around the Mandrill API.

Class

DrupalMandrill
Class DrupalMandrill.

Code

public static function getAttachmentStruct($path) {
  $struct = array();
  try {
    if (!@is_file($path)) {
      throw new Exception($path . ' is not a valid file.');
    }
    $filename = basename($path);
    if (!function_exists('get_magic_quotes')) {
      function get_magic_quotes() {
        return FALSE;
      }
    }
    if (!function_exists('set_magic_quotes')) {
      function set_magic_quotes($value) {
        return TRUE;
      }
    }
    if (strnatcmp(phpversion(), '6') >= 0) {
      $magic_quotes = get_magic_quotes_runtime();
      set_magic_quotes_runtime(0);
    }
    $file_buffer = file_get_contents($path);
    $file_buffer = chunk_split(base64_encode($file_buffer), 76, "\n");
    if (strnatcmp(phpversion(), '6') >= 0) {
      set_magic_quotes_runtime($magic_quotes);
    }
    $mime_type = file_get_mimetype($path);
    if (!DrupalMandrill::isValidContentType($mime_type)) {
      throw new Exception($mime_type . ' is not a valid content type (it should be ' . implode('*,', self::getValidContentTypes()) . ').');
    }
    $struct['type'] = $mime_type;
    $struct['name'] = $filename;
    $struct['content'] = $file_buffer;
  } catch (Exception $e) {
    throw new MandrillException('Error creating the attachment structure: ' . $e
      ->getMessage());
  }
  return $struct;
}