You are here

function webform2pdf_mail_alter in Webform2PDF 7.4

Same name and namespace in other branches
  1. 6.2 webform2pdf.module \webform2pdf_mail_alter()
  2. 6 webform2pdf.module \webform2pdf_mail_alter()
  3. 7.3 webform2pdf.module \webform2pdf_mail_alter()

File

./webform2pdf.module, line 180

Code

function webform2pdf_mail_alter(&$message) {
  if ($message['id'] == 'webform_submission' && isset($message['params']['node']) && isset($message['params']['submission'])) {
    module_load_include('inc', 'webform2pdf', 'includes/webform2pdf.mail');
    $webform2pdf_send2pdf = webform2pdf_send2pdf($message['params']['node'], $message['params']['submission']);
    if (isset($webform2pdf_send2pdf['nid'])) {
      $attach_pdf = 1;
      if (!empty($webform2pdf_send2pdf['nopdf'])) {
        if (in_array($message['to'], $webform2pdf_send2pdf['nopdf'])) {
          $attach_pdf = 0;
        }
      }
      if ($attach_pdf) {
        $node = node_load($webform2pdf_send2pdf['nid']);
        $filename = theme('webform2pdf_filename', array(
          'node' => $node,
          'submission' => $message['params']['submission'],
        ));
        unset($node);
        $html_capable = variable_get('webform_email_html_capable', false);
        if ($html_capable) {
          if (module_exists('mimemail')) {
            $attachment = array();
            $attachment['filecontent'] = theme('webform2pdf_mail2pdf', array(
              'nid' => $webform2pdf_send2pdf['nid'],
              'sid' => $webform2pdf_send2pdf['sid'],
            ));
            $attachment['filename'] = $filename;
            $attachment['filemime'] = 'application/pdf';
            $message['params']['attachments'][] = $attachment;
          }
        }
        else {
          $trenner = '------------' . md5(uniqid(time()));
          $content_type = $message['headers']['Content-Type'];
          $message['headers']['Content-Type'] = 'multipart/mixed; boundary="' . $trenner . '"';
          unset($message['headers']['Content-Transfer-Encoding']);
          $body = "\n\n--" . $trenner . "\n" . "Content-Type: " . $content_type . "\n" . "Content-Transfer-Encoding: 8bit\n\n";
          $body .= is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']);
          unset($message['body']);
          $message['body'][0] = $body;
          unset($body);
          $message['body'][0] .= "\n\n--" . $trenner . "\n";
          $message['body'][0] .= "Content-Type: application/pdf; name=\"" . $filename . "\"\n";
          $message['body'][0] .= "Content-Transfer-Encoding: base64\n";
          $message['body'][0] .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\n\n";
          $message['body'][0] .= chunk_split(base64_encode(theme('webform2pdf_mail2pdf', array(
            'nid' => $webform2pdf_send2pdf['nid'],
            'sid' => $webform2pdf_send2pdf['sid'],
          ))));
          $message['body'][0] .= "\n\n";
          $message['body'][0] .= "--" . $trenner . "--";
        }
      }
    }
  }
}