You are here

function regcode_mailer_list_action_mail in Registration codes 6

Submit action for the form

1 string reference to 'regcode_mailer_list_action_mail'
regcode_mailer_admin_list_form in regcode_mailer/regcode_mailer.module
Return the code list page content with(in) the according filter form

File

regcode_mailer/regcode_mailer.module, line 102

Code

function regcode_mailer_list_action_mail($form, $form_state) {

  // Grab the list of filtered codes
  $resource = regcode_admin_list_getresource();

  // Iterate the email addresses provided
  $emails = array_filter(explode("\n", $form_state['values']['regcode_mailer_emails']));
  array_map('trim', $emails);
  foreach ($emails as $email) {

    // Grab a code or break
    $regcode = db_fetch_object($resource);
    if (!$regcode) {
      drupal_set_message(t('Ran out of codes, stopped sending before %email', array(
        '%email' => $email,
      )));
      break;
    }

    // Format the email
    $params = array(
      'regcode' => $regcode,
      'message' => $form_state['values']['message'],
      'subject' => $form_state['values']['subject'],
    );

    // Send
    $message = drupal_mail('regcode_mailer', 'regcode', $email, language_default(), $params);
    drupal_set_message(t("Sent message with code %code to %email", array(
      '%code' => $regcode->code,
      '%email' => $email,
    )));

    // Update the database
    db_query('INSERT INTO {regcode_mailer} (rid, mailed, recipient) VALUES (%d, NOW(), "%s")', $regcode->rid, trim($email));
  }

  // Go back to the list
  drupal_goto('admin/user/regcodes/list');
}