Exemple #1
0
/**
 * Send e-mail to Claroline users form their ID a user of Claroline
 *
 * Send e-mail to Claroline users form their ID a user of Claroline
 * default from clause in email address will be the platorm admin adress
 * default from name clause in email will be the platform admin name and surname
 *
 * @author Hugues Peeters <*****@*****.**>
 * @param  int or array $userIdList - sender id's
 * @param  string $message - mail content
 * @param  string $subject - mail subject
 * @param  string $specificFrom (optional) sender's email address
 * @param  string  $specificFromName (optional) sender's name
 * @return int total count of sent email
 */
function claro_mail_user($userIdList, $message, $subject, $specificFrom = '', $specificFromName = '')
{
    if (!is_array($userIdList)) {
        $userIdList = array($userIdList);
    }
    if (count($userIdList) == 0) {
        return 0;
    }
    $tbl = claro_sql_get_main_tbl();
    $tbl_user = $tbl['user'];
    $sql = 'SELECT DISTINCT email
            FROM `' . $tbl_user . '`
            WHERE user_id IN (' . implode(', ', array_map('intval', $userIdList)) . ')';
    $emailList = claro_sql_query_fetch_all_cols($sql);
    $emailList = $emailList['email'];
    $emailList = array_filter($emailList, 'is_well_formed_email_address');
    $mail = new ClaroPHPMailer();
    if ($specificFrom != '') {
        $mail->From = $specificFrom;
    } else {
        $mail->From = get_conf('administrator_email');
    }
    if ($specificFromName != '') {
        $mail->FromName = $specificFromName;
    } else {
        $mail->FromName = get_conf('administrator_name');
    }
    $mail->Sender = $mail->From;
    if (strlen($subject) > 78) {
        $message = $subject . "\n" . $message;
        $subject = substr($subject, 0, 73) . '...';
    }
    $mail->Subject = $subject;
    $mail->Body = $message;
    $emailSentCount = 0;
    if (claro_debug_mode()) {
        $message = '<p>Subject : ' . claro_htmlspecialchars($subject) . '</p>' . "\n" . '<p>Message : <pre>' . claro_htmlspecialchars($message) . '</pre></p>' . "\n" . '<p>From : ' . claro_htmlspecialchars($mail->FromName) . ' - ' . claro_htmlspecialchars($mail->From) . '</p>' . "\n" . '<p>Dest : ' . implode(', ', $emailList) . '</p>' . "\n";
        pushClaroMessage($message, 'mail');
    }
    foreach ($emailList as $thisEmail) {
        $mail->AddAddress($thisEmail);
        if ($mail->Send()) {
            $emailSentCount++;
        } else {
            if (claro_debug_mode()) {
                pushClaroMessage($mail->getError(), 'error');
            }
        }
        $mail->ClearAddresses();
    }
    return $emailSentCount;
}
Exemple #2
0
 /**
  * Send a mail to the user list
  *
  * @param int $userIdList list of the user
  * @param string $message body of the mail
  * @param string $subject subject of the mail
  * @param string $specificFrom email of the sender
  * @param string $specificFromName name to display
  * @param string $altBody link of the message in case of problem of read
  * 
  */
 protected static function emailNotification($userIdList, $message, $subject, $specificFrom = '', $specificFromName = '', $altBody = '')
 {
     if (!is_array($userIdList)) {
         $userIdList = array($userIdList);
     }
     if (count($userIdList) == 0) {
         return 0;
     }
     $tbl = claro_sql_get_main_tbl();
     $tbl_user = $tbl['user'];
     $sql = 'SELECT DISTINCT email
             FROM `' . $tbl_user . '`
             WHERE user_id IN (' . implode(', ', array_map('intval', $userIdList)) . ')';
     $emailList = claro_sql_query_fetch_all_cols($sql);
     $emailList = $emailList['email'];
     $emailList = array_filter($emailList, 'is_well_formed_email_address');
     $mail = new ClaroPHPMailer();
     $mail->IsHTML(true);
     if (!empty($altBody)) {
         $mail->AltBody = $altBody;
     }
     if ($specificFrom != '') {
         $mail->From = $specificFrom;
     } else {
         $mail->From = get_conf('administrator_email');
     }
     if ($specificFromName != '') {
         $mail->FromName = $specificFromName;
     } else {
         $mail->FromName = get_conf('administrator_name');
     }
     $mail->Sender = $mail->From;
     if (strlen($subject) > 78) {
         $message = get_lang('Subject') . ' : ' . $subject . "<br />\n\n" . $message;
         $subject = substr($subject, 0, 73) . '...';
     }
     $mail->Subject = $subject;
     $mail->Body = $message;
     if (claro_debug_mode()) {
         $message = '<p>' . get_lang('Subject') . ' : ' . claro_htmlspecialchars($subject) . '</p>' . "\n" . '<p>' . get_lang('Message') . ' : <pre>' . claro_htmlspecialchars($message) . '</pre></p>' . "\n" . '<p>' . get_lang('Sender') . ' : ' . claro_htmlspecialchars($mail->FromName) . ' - ' . claro_htmlspecialchars($mail->From) . '</p>' . "\n" . '<p>' . get_lang('Recipient') . ' : ' . implode(', ', $emailList) . '</p>' . "\n";
         pushClaroMessage($message, 'mail');
     }
     $error_list = array();
     foreach ($emailList as $thisEmail) {
         try {
             $mail->AddAddress($thisEmail);
             $mail->Send();
         } catch (phpmailerException $exception) {
             if (claro_debug_mode()) {
                 pushClaroMessage('Mail Notification Failed ' . $exception->__toString());
                 $error_list[] = $thisEmail;
             }
         }
         $mail->ClearAddresses();
     }
 }