From 84fcd205074c13c391856bc2be9b7a5f2b4d9e06 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sat, 16 Jun 2018 20:10:28 +1000 Subject: [PATCH] add attachment to email if it exists --- booking.email_manually.inc | 29 +++++++++++++---------------- booking.emails.inc | 23 +++++++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/booking.email_manually.inc b/booking.email_manually.inc index 88de598..c237414 100644 --- a/booking.email_manually.inc +++ b/booking.email_manually.inc @@ -442,7 +442,9 @@ function booking_manual_email_from_validation($form, &$form_state) { */ function booking_manual_email_form_submit($form, &$form_state) { $counter = 0; - $update_messages = array(); + $message = ""; + $attachment = NULL; + $update_messages = array(); $checkboxes = $form_state['values']['table']; //$values['booking_price_active']; //watchdog('booking', 'Formstate when setting buttons: @info', array ('@info' => var_export($form_state['values'], TRUE))); //watchdog('booking', 'Checkboxes when setting buttons: @info', array ('@info' => var_export($checkboxes, TRUE))); @@ -452,22 +454,17 @@ function booking_manual_email_form_submit($form, &$form_state) { watchdog('booking_debug', 'File attachment detected: @info', array ('@info' => print_r($attachment, TRUE))); } - foreach($checkboxes as $key => $value) - { - $message = ""; - + foreach($checkboxes as $key => $value) { //do a sanity check on the key => value pair from the form submission - if (is_numeric($key) && $value != 0) - { + if (is_numeric($key) && $value != 0) { //check the person exists in the database - $person = db_query("SELECT person.nid " . - "FROM {booking_person} person " . - "WHERE nid = :nid", - array(':nid' => $key)) - ->fetchObject(); + $person = db_query("SELECT person.nid " . + "FROM {booking_person} person " . + "WHERE nid = :nid", + array(':nid' => $key)) + ->fetchObject(); - if ($person) - { + if ($person) { if ($form_state['values']['email-type'] == 'registration') { $message = t('Processing a manual registration email to id @info', array ('@info' => $key)); _booking_registration_email($key, false, true); @@ -510,11 +507,11 @@ function booking_manual_email_form_submit($form, &$form_state) { } elseif (strpos($form_state['values']['email-type'], 'customlogistics') !== false) { $message = t('Processing a @custom type email from logistics to id @info', array ('@custom' => $form_state['values']['email-type'], '@info' => $key)); - _booking_custom_email($key, $form_state['values']['email-type'], 'logistics'); + _booking_custom_email($key, $form_state['values']['email-type'], 'logistics', $attachment); } elseif (strpos($form_state['values']['email-type'], 'custom') !== false) { $message = t('Processing a @custom type email to id @info', array ('@custom' => $form_state['values']['email-type'], '@info' => $key)); - _booking_custom_email($key, $form_state['values']['email-type'], 'contact'); + _booking_custom_email($key, $form_state['values']['email-type'], 'contact', $attachment); } //increase the counter of people we've emailed $counter++; diff --git a/booking.emails.inc b/booking.emails.inc index 148e9fe..522d27f 100644 --- a/booking.emails.inc +++ b/booking.emails.inc @@ -303,7 +303,7 @@ function _booking_partialbalance_payment_email($nid) { * @param $email_type - select which custom email template to use * @return nothing */ -function _booking_custom_email($nid, $email_type, $sender = 'contact') { +function _booking_custom_email($nid, $email_type, $sender = 'contact', $attachment = NULL) { global $event; global $user; $language = user_preferred_language($user); @@ -326,14 +326,7 @@ function _booking_custom_email($nid, $email_type, $sender = 'contact') { //calculate the from email address for a logistics email $from = t('!event Registrations ', array('!event' => $event->booking_eventname, '!email' => variable_get('booking_logistics_email', variable_get('site_mail', ini_get('sendmail_from'))) - )); - // Test email attachments with dummy file - $attachment = array( - 'filecontent' => file_get_contents(DRUPAL_ROOT . '/README.txt'), - 'filename' => 'test.txt', - 'filemime' => 'text/plain', - ); - $params['attachment'] = $attachment; + )); } //load the node matching this id @@ -356,7 +349,17 @@ function _booking_custom_email($nid, $email_type, $sender = 'contact') { $message_body = isset($message_body['format']) ? $message_body['value'] : $message_body; $message_body = token_replace($message_body, $tokens); $themed_body = _booking_generate_html_body($subject, $message_body, $tokens); - $params['body'] = $themed_body; + $params['body'] = $themed_body; + + // If there was an attachment, then add it + if ($attachment) { + $file_uri = drupal_realpath($attachment->uri); + // Make sure we can still access the file + if (file_exists($file_uri) && fopen($file_uri, 'r') !== false) { + watchdog('booking_debug', "Adding attachment !name to custom email.", array('!name' => $attachment->filename)); + $params['attachment'] = file_load($attachment->fid); + } + } //send the email to the person drupal_mail('booking', 'booking_email_custom', $to, $language, $params, $from);