You are here

function _contact_attach_process_attachments in Contact Attach 6

Same name and namespace in other branches
  1. 5 contact_attach.module \_contact_attach_process_attachments()
  2. 7 contact_attach.module \_contact_attach_process_attachments()

Check for attachments and process them, if one or more exists.

Parameters

message: The message, as it exists so far.

Return value

The message, including processed attachment(s).

1 call to _contact_attach_process_attachments()
contact_attach_mail in ./contact_attach.module
Implementation of hook_mail().

File

./contact_attach.module, line 445
Allows attaching files to e-mails sent using the site-wide contact form.

Code

function _contact_attach_process_attachments($message) {
  $return_message = array();

  // Loop through each possible attachment.
  for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
    if ($file = file_save_upload('contact_attach_' . $i)) {

      // Check to see if the attachment exists.
      if ($file->filesize > 0) {

        // An attachment exists, so save it to an array for later processing.
        $files[] = $file;
      }
    }
  }

  // If the array cantains something, we have one or more attachments to
  // process. If it does not contain anything, we send back an empty $body,
  // indicating no attachments exist.
  if (!empty($files)) {

    // Set initial values.
    $body = '';
    $return_message = array();
    $boundary_id = md5(uniqid(time()));
    $message['headers']['Content-Type'] = 'multipart/mixed; boundary="' . $boundary_id . '"';
    $body = "\n--{$boundary_id}\n";
    $body .= "Content-Type: text/plain; charset=UTF-8; format=flowed;\n\n";

    // sets the mime type
    // Prepare the body:
    $body .= implode("\n\n", $message['body']);
    $body .= "\n\n\n";

    // Add the attachments.
    // Loop through each possible attachment.
    for ($i = 0; $i < count($files); $i++) {

      // Process the attachment.
      $body .= "--{$boundary_id}\n";
      $body .= _contact_attach_add_attachment($files[$i]);
      $body .= "\n\n";
    }
    $body .= "--{$boundary_id}--\n\n";
    $return_message['headers'] = $message['headers'];
    $return_message['body'] = $body;
  }
  return $return_message;
}