Ejemplo n.º 1
0
function sendEmail($data = array('recipient' => '', 'toName' => '', 'subject' => '', 'isHTML' => 0, 'html' => '', 'text' => '', 'attach' => array(), 'from' => '', 'fromName' => '', 'sender' => '', 'stringAttach' => array()))
{
    // used to send a standardized email message
    global $phpwcms;
    $mailInfo = array(0 => false, 1 => '');
    $sendTo = array();
    $from = empty($data['from']) || !is_valid_email($data['from']) ? $phpwcms['SMTP_FROM_EMAIL'] : $data['from'];
    $sender = empty($data['sender']) || !is_valid_email($data['sender']) ? $from : $data['sender'];
    $fromName = empty($data['fromName']) ? '' : cleanUpForEmailHeader($data['fromName']);
    $toName = empty($data['toName']) ? '' : cleanUpForEmailHeader($data['toName']);
    $subject = empty($data['subject']) ? 'Email sent by phpwcms' : cleanUpForEmailHeader($data['subject']);
    if (empty($data['html'])) {
        $data['html'] = '';
        $data['isHTML'] = 0;
    } elseif (empty($data['isHTML'])) {
        $data['isHTML'] = 0;
    } else {
        $data['isHTML'] = 1;
    }
    if (empty($data['text'])) {
        $data['text'] = '';
    }
    if (!is_array($data['recipient'])) {
        $recipient = str_replace(' ', '', trim($data['recipient']));
        $recipient = str_replace(',', ';', $recipient);
        $recipient = str_replace(' ', '', $recipient);
        $recipient = explode(';', $recipient);
    } else {
        $recipient = $data['recipient'];
    }
    if (is_array($recipient) && count($recipient)) {
        foreach ($recipient as $value) {
            if (is_valid_email($value)) {
                $sendTo[] = $value;
            }
        }
    }
    if (count($sendTo)) {
        require_once PHPWCMS_ROOT . '/include/inc_ext/phpmailer/PHPMailerAutoload.php';
        $mail = new PHPMailer();
        $mail->Mailer = $phpwcms['SMTP_MAILER'];
        $mail->Host = $phpwcms['SMTP_HOST'];
        $mail->Port = $phpwcms['SMTP_PORT'];
        if ($phpwcms['SMTP_AUTH']) {
            $mail->SMTPAuth = 1;
            $mail->Username = $phpwcms['SMTP_USER'];
            $mail->Password = $phpwcms['SMTP_PASS'];
        }
        if (!empty($phpwcms['SMTP_SECURE'])) {
            $mail->SMTPSecure = $phpwcms['SMTP_SECURE'];
        }
        if (!empty($phpwcms['SMTP_AUTH_TYPE'])) {
            $mail->AuthType = $phpwcms['SMTP_AUTH_TYPE'];
            if ($phpwcms['SMTP_AUTH_TYPE'] === 'NTLM') {
                if (!empty($phpwcms['SMTP_REALM'])) {
                    $mail->Realm = $phpwcms['SMTP_REALM'];
                }
                if (!empty($phpwcms['SMTP_WORKSTATION'])) {
                    $mail->Workstation = $phpwcms['SMTP_WORKSTATION'];
                }
            }
        }
        $mail->CharSet = $phpwcms["charset"];
        $mail->isHTML($data['isHTML']);
        $mail->Subject = $data['subject'];
        if ($data['isHTML']) {
            if ($data['text'] != '') {
                $mail->AltBody = $data['text'];
            }
            $mail->Body = $data['html'];
        } else {
            $mail->Body = $data['text'];
        }
        if (!$mail->setLanguage($phpwcms['default_lang'], PHPWCMS_ROOT . '/include/inc_ext/phpmailer/language/')) {
            $mail->setLanguage('en', PHPWCMS_ROOT . '/include/inc_ext/phpmailer/language/');
        }
        $mail->From = $from;
        $mail->FromName = $fromName;
        $mail->Sender = $sender;
        $mail->addAddress($sendTo[0], $toName);
        unset($sendTo[0]);
        if (is_array($sendTo) && count($sendTo)) {
            foreach ($sendTo as $value) {
                $mail->addBCC($value);
            }
        }
        if (isset($data['attach']) && is_array($data['attach']) && count($data['attach'])) {
            foreach ($data['attach'] as $attach_file) {
                $mail->addAttachment($attach_file);
            }
        }
        if (isset($data['stringAttach']) && is_array($data['stringAttach']) && count($data['stringAttach'])) {
            $attach_counter = 1;
            foreach ($data['stringAttach'] as $attach_string) {
                if (is_array($attach_string) && !empty($attach_string['data'])) {
                    $attach_string['filename'] = empty($attach_string['filename']) ? 'attachment_' . $attach_counter : $attach_string['filename'];
                    $attach_string['mime'] = empty($attach_string['mime']) ? 'application/octet-stream' : $attach_string['mime'];
                    $attach_string['encoding'] = empty($attach_string['encoding']) ? 'base64' : $attach_string['encoding'];
                    $mail->addStringAttachment($attach_string['data'], $attach_string['filename'], $attach_string['encoding'], $attach_string['mime']);
                    $attach_counter++;
                }
            }
        }
        if (!$mail->send()) {
            $mailInfo[0] = false;
            $mailInfo[1] = $mail->ErrorInfo;
        } else {
            $mailInfo[0] = true;
        }
        unset($mail);
    } else {
        $mailInfo[0] = false;
        $mailInfo[1] = 0;
        //means no recipient
    }
    return $mailInfo;
}
Ejemplo n.º 2
0
                 $checkbox_value = str_replace(' checked', '', $checkbox_value);
                 $form_field .= 'value="' . $checkbox_key . '" checked="checked" />';
             }
             $form_field .= $checkbox_value . '</label>';
             $checkbox_counter++;
         }
         $form_field .= $checkbox_class;
         break;
 }
 // try to find correct sender name
 if ($POST_DO && $cnt_form['sendernametype'] == 'formfield_' . $POST_name) {
     $cnt_form['sendername'] = cleanUpForEmailHeader($cnt_form["fields"][$key]['value']);
 }
 // try to build correct subject
 if ($POST_DO && isset($cnt_form['subjectselect']) && $cnt_form['subjectselect'] == 'formfield_' . $POST_name) {
     $cnt_form['subject'] .= ' ' . cleanUpForEmailHeader($POST_val[$POST_name]);
     $cnt_form['subject'] = trim($cnt_form['subject']);
 }
 // Build the form elements
 if ($form_field && $cnt_form["fields"][$key]['type'] != 'hidden') {
     if ($cnt_form['labelpos'] == 2) {
         // custom form template
         $POST_name_quoted = preg_quote($POST_name, '/');
         if (isset($POST_ERR[$key])) {
             // field error
             $form_cnt = preg_replace('/\\[IF_ERROR:' . $POST_name_quoted . '\\](.*?)\\[\\/IF_ERROR\\]/s', '$1', $form_cnt);
             $form_cnt = preg_replace('/\\[ELSE_ERROR:' . $POST_name_quoted . '\\].*?\\[\\/ELSE_ERROR\\]/s', '', $form_cnt);
             $form_cnt = str_replace('{ERROR:' . $POST_name . '}', html_specialchars($POST_ERR[$key]), $form_cnt);
         } else {
             // no field error
             $form_cnt = preg_replace('/\\[IF_ERROR:' . $POST_name_quoted . '\\].*?\\[\\/IF_ERROR\\]/s', '', $form_cnt);