add attachment to email if it exists

This commit is contained in:
Nathan Coad
2018-06-16 20:10:28 +10:00
parent 04fc135818
commit 84fcd20507
2 changed files with 26 additions and 26 deletions

View File

@@ -442,7 +442,9 @@ function booking_manual_email_from_validation($form, &$form_state) {
*/ */
function booking_manual_email_form_submit($form, &$form_state) { function booking_manual_email_form_submit($form, &$form_state) {
$counter = 0; $counter = 0;
$update_messages = array(); $message = "";
$attachment = NULL;
$update_messages = array();
$checkboxes = $form_state['values']['table']; //$values['booking_price_active']; $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', '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))); //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))); watchdog('booking_debug', 'File attachment detected: @info', array ('@info' => print_r($attachment, TRUE)));
} }
foreach($checkboxes as $key => $value) foreach($checkboxes as $key => $value) {
{
$message = "";
//do a sanity check on the key => value pair from the form submission //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 //check the person exists in the database
$person = db_query("SELECT person.nid " . $person = db_query("SELECT person.nid " .
"FROM {booking_person} person " . "FROM {booking_person} person " .
"WHERE nid = :nid", "WHERE nid = :nid",
array(':nid' => $key)) array(':nid' => $key))
->fetchObject(); ->fetchObject();
if ($person) if ($person) {
{
if ($form_state['values']['email-type'] == 'registration') { if ($form_state['values']['email-type'] == 'registration') {
$message = t('Processing a manual registration email to id @info', array ('@info' => $key)); $message = t('Processing a manual registration email to id @info', array ('@info' => $key));
_booking_registration_email($key, false, true); _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) { 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)); $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) { 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)); $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 //increase the counter of people we've emailed
$counter++; $counter++;

View File

@@ -303,7 +303,7 @@ function _booking_partialbalance_payment_email($nid) {
* @param $email_type - select which custom email template to use * @param $email_type - select which custom email template to use
* @return nothing * @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 $event;
global $user; global $user;
$language = user_preferred_language($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 //calculate the from email address for a logistics email
$from = t('!event Registrations <!email>', array('!event' => $event->booking_eventname, $from = t('!event Registrations <!email>', array('!event' => $event->booking_eventname,
'!email' => variable_get('booking_logistics_email', variable_get('site_mail', ini_get('sendmail_from'))) '!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 //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 = isset($message_body['format']) ? $message_body['value'] : $message_body;
$message_body = token_replace($message_body, $tokens); $message_body = token_replace($message_body, $tokens);
$themed_body = _booking_generate_html_body($subject, $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 //send the email to the person
drupal_mail('booking', 'booking_email_custom', $to, $language, $params, $from); drupal_mail('booking', 'booking_email_custom', $to, $language, $params, $from);