You are here

function _contact_attach_process_attachments in Contact Attach 5

Same name and namespace in other branches
  1. 6 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).

3 calls to _contact_attach_process_attachments()
contact_attach_contact_mail_page_submit in ./contact_attach.module
Override contact_mail_page_submit().
contact_attach_contact_mail_user_submit in ./contact_attach.module
Override contact_mail_user_submit().
contact_attach_og_contact_mail_page_submit in ./contact_attach.module
Override og_contact_mail_page_submit().

File

./contact_attach.module, line 441
This is the main code file for the Contact attach module. This module gives users the ability of attaching one or more files to e-mails sent using the site-wide contact form.

Code

function _contact_attach_process_attachments($message) {

  // Set an initial value.
  $return_message = array();

  // Loop through each possible attachment.
  for ($i = 1; $i <= variable_get('contact_attach_number', '3'); $i++) {
    if ($file = file_check_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)) {
    $boundary_id = md5(uniqid(time()));
    $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 .= "\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'] = $headers;
    $return_message['body'] = $body;
  }
  return $return_message;
}