###############################################################################
# CP_DoEmailAccounts.src                                                      #
###############################################################################
# UltraBoard 2000 by UltraScripts.com, Inc.                                   #
# Scripts written by Jacky Wing Wa Yung, WebMaster@UltraScripts.com           #
# Available from http://www.UltraBoard.org/                                   #
# =========================================================================== #
# COPYRIGHT NOTICE :                                                          #
#                                                                             #
# Copyright (c) 1999-2000 UltraScripts.com, Inc. All Rights Reserved.         #
#                                                                             #
# This program is being distributed as shareware. It may be used and modified #
# free for personal, government or non-profit. Any commercial use must be     #
# registered. However, modified versions cannot be sold or distributed. You   #
# cannot alter the copyright and "powered by" notices throughout the scripts. #
# These notices must be clearly visible to the end users.                     #
#                                                                             #
# WARRANTY DISCLAIMER:                                                        #
#                                                                             #
# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT #
# ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR       #
# FITNESS FOR A PARTICULAR PURPOSE.                                           #
###############################################################################

package Source;

use strict;

sub DoEmailAccounts {
  unless ($SYSTEM::SENDMAIL) {
    &HTML::Error("Members Accounts Management / Manage member accounts / Email member(s)", "Sorry, you have to enable sendmail function first!");
  }

# =========================================================================== #
# Verify Variables

  # checking missing fields
  if ($UB::QUERY{'FORM_SUBJECT'} eq "") { &HTML::Error("Members Accounts Management / Manage member accounts / Email member(s)", "You forgot to fill the \"Subject\" field.") };
  if ($UB::QUERY{'FORM_MESSAGE'} eq "") { &HTML::Error("Members Accounts Management / Manage member accounts / Email member(s)", "You forgot to fill the \"Message\" field.") };

  my $SendMail = 0;
  if ($SYSTEM::SENDMAIL == 1) {
    if ( ($SYSTEM::MAIL_SYSTEM eq "SMTP") && ($SYSTEM::SMTP_PATH ne "") ) {
      $SendMail = &Standard::Require("SMTP.lib", "$UB::LIBRARIES_PATH/SMTP.lib");
    } elsif ( ($SYSTEM::MAIL_SYSTEM eq "Sendmail") && ($SYSTEM::SENDMAIL_PATH ne "") ) {
      $SendMail = &Standard::Require("Sendmail.lib", "$UB::LIBRARIES_PATH/Sendmail.lib");
    }
  }

  my ($Accounts, $AccountsIndex) = &DB::Query (
    TABLE   => "Accounts"      ,
    COLUMNS => ["USERNAME", "EMAIL"],
    SORT    => "POSITION"      ,
    BY      => "A-Z"
  );

  my @Email;
  foreach my $Username ( split (/,/, $UB::QUERY{'UIDS'}) ) {
    if ($AccountsIndex->{$Username}) {
      push (@Email, $Accounts->[$AccountsIndex->{$Username}]->{'Email'});
    }
  }

  if ($SendMail) {
    &Mail::Send (
      SUBJECT => $UB::QUERY{'FORM_SUBJECT'},
      MESSAGE => $UB::QUERY{'FORM_MESSAGE'},
      TO      => $GENERAL::CONTACT_EMAIL        ,
      HEADER  => ($UB::QUERY{'FORM_FORMAT'} eq "HTML") ? "Content-type: text/html" : "Content-type: text/plain",
      BCC     => join (",", @Email)
    );
  }

  &Success();
}

###############################################################################
# Success function
# - function of printing success message of sending members
sub Success {
  &Standard::PrintHTMLHeader();
  print &HTML::Header;
  print<<HTML;
<table width="100%" border="0" cellspacing="0" cellpadding="5">
  <tr bgcolor="Gray">
    <td><font color="White"><b>
      <a href="$SYSTEM::CP_URL/Panel.$SYSTEM::EXT?action=body" target="UBBODY" class="address">Control Panel</a> / Members Accounts Management / Manage member accounts / Email member(s)
    </b></font></td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td>
      <table width="100%" border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>
            <table width="100%" border="1" cellspacing="0" cellpadding="3" bordercolordark="White" bordercolorlight="Gray">
              <tr>
                <td>
                  <table width="100%" border="0" cellspacing="0" cellpadding="2">
                    <tr>
                      <td>Sent email to those members.</td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>
HTML

  exit;
}

###############################################################################
1; # end of CP_DoEmailAccounts.src file
###############################################################################