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,6 +442,8 @@ function booking_manual_email_from_validation($form, &$form_state) {
*/
function booking_manual_email_form_submit($form, &$form_state) {
$counter = 0;
$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)));
@@ -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++;

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') {
function _booking_custom_email($nid, $email_type, $sender = 'contact', $attachment = NULL) {
global $event;
global $user;
$language = user_preferred_language($user);
@@ -327,13 +327,6 @@ function _booking_custom_email($nid, $email_type, $sender = 'contact') {
$from = t('!event Registrations <!email>', 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
@@ -358,6 +351,16 @@ function _booking_custom_email($nid, $email_type, $sender = 'contact') {
$themed_body = _booking_generate_html_body($subject, $message_body, $tokens);
$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);
}