modify ready for multiple email attachments

This commit is contained in:
Nathan Coad
2018-06-26 17:58:40 +10:00
parent 7e4f90cc54
commit 01b0b05428
3 changed files with 40 additions and 11 deletions

View File

@@ -129,7 +129,7 @@ function booking_manual_email_form($form, &$form_state)
'wrapper' => 'booking_email_default_ids_wrapper',
),
);
/*
$form['booking-email-attachment'] = array(
'#type' => 'managed_file',
'#title' => t('Email attachment'),
@@ -140,11 +140,39 @@ function booking_manual_email_form($form, &$form_state)
'file_validate_extensions' => array(variable_get('booking_email_allowed_attachments', 'pdf docx')),
),
);
*/
/*
// See https://drupal.stackexchange.com/questions/108583/how-to-save-file-uploaded-with-plupload-integration-module-and-make-it-managed
// For info on saving the attachments from plupload
// Submit handler:
if (!empty($form_state['values']['upload'])) {
$form_state['uploaded_files'] = quotes_file_save_files($form_state['values']['upload']);
}
function quotes_file_save_files($files) {
$saved_files = array();
$scheme = 'private://'; // Change to "public://" if you want to move to public files folder.
foreach ($files as $uploaded_file) {
if ($uploaded_file['status'] == 'done') {
$source = $uploaded_file['tmppath'];
$destination = file_stream_wrapper_uri_normalize($scheme . $uploaded_file['name']);
$destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
$file = plupload_file_uri_to_object($destination);
file_save($file);
// Check the $file object to see if all necessary properties are set. Otherwise, use file_load($file->fid) to populate all of them.
// $file = file_load($file->fid);
$saved_files[] = $file;
}
}
return $saved_files;
}
$form['booking-email-attachment'] = array(
'#type' => 'plupload',
'#title' => t('Email attachment'),
'#description' => t('Attach a file to be emailed. Note that this only works with custom email types. File must be 10MB or less, and extension must be one of: ' . variable_get('booking_email_allowed_attachments', 'pdf docx')),
'#description' => t('Attach any files to be attached to email. Note that this only works with custom email types.'),
'#upload_validators' => array(
'file_validate_size' => array(10*1024*1024),
'file_validate_extensions' => array(variable_get('booking_email_allowed_attachments', 'pdf docx')),
@@ -154,6 +182,7 @@ function booking_manual_email_form($form, &$form_state)
'chunk_size' => '1mb',
),
);
*/
$form['booking-email-preview'] = array(
'#type' => 'container',
@@ -439,14 +468,14 @@ function _booking_email_get_default_selection_callback($form, $form_state) {
function booking_manual_email_form_submit($form, &$form_state) {
$counter = 0;
$message = "";
$attachment = NULL;
$attachments = array();
$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)));
if (isset($form_state['values']['booking-email-attachment']) && $form_state['values']['booking-email-attachment'] != 0) {
$attachment = file_load($form_state['values']['booking-email-attachment']);
$attachments[] = file_load($form_state['values']['booking-email-attachment']);
watchdog('booking_debug', 'File attachment detected: @info', array ('@info' => print_r($attachment, TRUE)));
// NOTE: Since we do nothing to save this file, it should get cleaned up by cron
// As per https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x#managed_file
@@ -509,7 +538,7 @@ function booking_manual_email_form_submit($form, &$form_state) {
}
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', $attachment);
_booking_custom_email($key, $form_state['values']['email-type'], 'contact', $attachments);
}
//increase the counter of people we've emailed
$counter++;

View File

@@ -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', $attachment = NULL) {
function _booking_custom_email($nid, $email_type, $sender = 'contact', $attachments = NULL) {
global $event;
global $user;
$language = user_preferred_language($user);
@@ -352,12 +352,12 @@ function _booking_custom_email($nid, $email_type, $sender = 'contact', $attachme
$params['body'] = $themed_body;
// If there was an attachment, then add it
if ($attachment) {
foreach ($attachments as $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);
watchdog('booking_debug', "Adding attachment !name to custom email.", array('!name' => $attachment->filename));
$params['attachment'][] = file_load($attachment->fid);
}
}

View File

@@ -978,7 +978,7 @@ function booking_mail($key, &$message, $params) {
// Add attachment when available.
// From https://drupal.stackexchange.com/questions/101035/send-attachments-with-drupal-mail
if (isset($params['attachment'])) {
$message['params']['attachments'][] = $params['attachment'];
$message['params']['attachments'] = $params['attachment'];
}
//watchdog('booking_debug', "<pre>Mail hook:\n@info</pre>", array('@info' => print_r( $message, true)));