Example #1
0
 function send_mail_phpmailer($email, $subject, $message = "", $from = "", $reply_to = "", $html_template = "", $templatevars = null, $from_name = "", $cc = "", $bcc = "")
 {
     # if ($use_phpmailer==true) this function is used instead.
     # Mail templates can include lang, server, site_text, and POST variables by default
     # ex ( [lang_mycollections], [server_REMOTE_ADDR], [text_footer] , [message]
     # additional values must be made available through $templatevars
     # For example, a complex url or image path that may be sent in an
     # email should be added to the templatevars array and passed into send_mail.
     # available templatevars need to be well-documented, and sample templates
     # need to be available.
     # Include footer
     global $email_footer, $storagedir;
     $phpversion = phpversion();
     if ($phpversion >= '5.3') {
         if (file_exists(dirname(__FILE__) . "/../lib/phpmailer_v5.2.6/class.phpmailer.php")) {
             include_once dirname(__FILE__) . "/../lib/phpmailer_v5.2.6/class.phpmailer.php";
             include_once dirname(__FILE__) . "/../lib/phpmailer_v5.2.6/extras/class.html2text.php";
         }
     } else {
         // less than 5.3
         if (file_exists(dirname(__FILE__) . "/../lib/phpmailer/class.phpmailer.php")) {
             include_once dirname(__FILE__) . "/../lib/phpmailer/class.phpmailer.php";
             include_once dirname(__FILE__) . "/../lib/phpmailer/class.html2text.php";
         }
     }
     global $email_from;
     if ($from == "") {
         $from = $email_from;
     }
     if ($reply_to == "") {
         $reply_to = $email_from;
     }
     global $applicationname;
     if ($from_name == "") {
         $from_name = $applicationname;
     }
     #check for html template. If exists, attempt to include vars into message
     if ($html_template != "") {
         # Attempt to verify users by email, which allows us to get the email template by lang and usergroup
         $to_usergroup = sql_query("select lang,usergroup from user where email ='" . escape_check($email) . "'", "");
         if (count($to_usergroup) != 0) {
             $to_usergroupref = $to_usergroup[0]['usergroup'];
             $to_usergrouplang = $to_usergroup[0]['lang'];
         } else {
             $to_usergrouplang = "";
         }
         if ($to_usergrouplang == "") {
             global $defaultlanguage;
             $to_usergrouplang = $defaultlanguage;
         }
         if (isset($to_usergroupref)) {
             $modified_to_usergroupref = hook("modifytousergroup", "", $to_usergroupref);
             if ($modified_to_usergroupref !== null) {
                 $to_usergroupref = $modified_to_usergroupref;
             }
             $results = sql_query("select language,name,text from site_text where page='all' and name='{$html_template}' and specific_to_group='{$to_usergroupref}'");
         } else {
             $results = sql_query("select language,name,text from site_text where page='all' and name='{$html_template}' and specific_to_group is null");
         }
         global $site_text;
         for ($n = 0; $n < count($results); $n++) {
             $site_text[$results[$n]["language"] . "-" . $results[$n]["name"]] = $results[$n]["text"];
         }
         $language = $to_usergrouplang;
         if (array_key_exists($language . "-" . $html_template, $site_text)) {
             $template = $site_text[$language . "-" . $html_template];
         } else {
             global $languages;
             # Can't find the language key? Look for it in other languages.
             reset($languages);
             foreach ($languages as $key => $value) {
                 if (array_key_exists($key . "-" . $html_template, $site_text)) {
                     $template = $site_text[$key . "-" . $html_template];
                     break;
                 }
             }
             // Fall back to language file if not in site text
             global $lang;
             if (isset($lang[$html_template])) {
                 $template = $lang[$html_template];
             }
         }
         if (isset($template) && $template != "") {
             preg_match_all('/\\[[^\\]]*\\]/', $template, $test);
             foreach ($test[0] as $variable) {
                 $variable = str_replace("[", "", $variable);
                 $variable = str_replace("]", "", $variable);
                 # get lang variables (ex. [lang_mycollections])
                 if (substr($variable, 0, 5) == "lang_") {
                     global $lang;
                     ${$variable} = $lang[substr($variable, 5)];
                 } else {
                     if (substr($variable, 0, 7) == "server_") {
                         ${$variable} = $_SERVER[substr($variable, 7)];
                     } else {
                         if (substr($variable, 0, 15) == "embed_thumbnail") {
                             $thumbcid = uniqid('thumb');
                             ${$variable} = "<img style='border:1px solid #d1d1d1;' src='cid:{$thumbcid}' />";
                         } else {
                             if (substr($variable, 0, 15) == "img_storagedir_") {
                                 ${$variable} = "<img src='cid:" . basename(substr($variable, 15)) . "'/>";
                                 $images[] = dirname(__FILE__) . substr($variable, 15);
                             } else {
                                 if (substr($variable, 0, 4) == "img_") {
                                     $image_path = substr($variable, 4);
                                     if (substr($image_path, 0, 1) == "/") {
                                         // absolute paths
                                         $images[] = $image_path;
                                     } else {
                                         // relative paths
                                         $image_path = str_replace("../", "", $image_path);
                                         $images[] = dirname(__FILE__) . "/../" . $image_path;
                                     }
                                     ${$variable} = "<img src='cid:" . basename($image_path) . "'/>";
                                     $images[] = $image_path;
                                 } else {
                                     if (substr($variable, 0, 7) == "attach_") {
                                         ${$variable} = "";
                                         $attachments[] = substr($variable, 7);
                                     } else {
                                         if (substr($variable, 0, 5) == "text_") {
                                             ${$variable} = text(substr($variable, 5));
                                         } else {
                                             ${$variable} = getval($variable, "");
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 # avoid resetting templatevars that may have been passed here
                 if (!isset($templatevars[$variable])) {
                     $templatevars[$variable] = ${$variable};
                 }
             }
             if (isset($templatevars)) {
                 foreach ($templatevars as $key => $value) {
                     $template = str_replace("[" . $key . "]", nl2br($value), $template);
                 }
             }
             $body = $template;
         }
     }
     if (!isset($body)) {
         $body = $message;
     }
     global $use_smtp, $smtp_secure, $smtp_host, $smtp_port, $smtp_auth, $smtp_username, $smtp_password;
     $mail = new PHPMailer();
     // use an external SMTP server? (e.g. Gmail)
     if ($use_smtp) {
         $mail->IsSMTP();
         // enable SMTP
         $mail->SMTPDebug = 0;
         // debugging: 1 = errors and messages, 2 = messages only
         $mail->SMTPAuth = $smtp_auth;
         // authentication enabled/disabled
         $mail->SMTPSecure = $smtp_secure;
         // '', 'tls' or 'ssl'
         $mail->Host = $smtp_host;
         // hostname
         $mail->Port = $smtp_port;
         // port number
         $mail->Username = $smtp_username;
         // username
         $mail->Password = $smtp_password;
         // password
     }
     $reply_tos = explode(",", $reply_to);
     // only one from address is possible, so only use the first one:
     if (strstr($reply_tos[0], "<")) {
         $rtparts = explode("<", $reply_tos[0]);
         $mail->From = str_replace(">", "", $rtparts[1]);
         $mail->FromName = $rtparts[0];
     } else {
         $mail->From = $reply_tos[0];
         $mail->FromName = $from_name;
     }
     // if there are multiple addresses, that's what replyto handles.
     for ($n = 0; $n < count($reply_tos); $n++) {
         if (strstr($reply_tos[$n], "<")) {
             $rtparts = explode("<", $reply_tos[$n]);
             $mail->AddReplyto(str_replace(">", "", $rtparts[1]), $rtparts[0]);
         } else {
             $mail->AddReplyto($reply_tos[$n], $from_name);
         }
     }
     # modification to handle multiple comma delimited emails
     # such as for a multiple $email_notify
     $emails = $email;
     $emails = explode(',', $emails);
     $emails = array_map('trim', $emails);
     foreach ($emails as $email) {
         if (strstr($email, "<")) {
             $emparts = explode("<", $email);
             $mail->AddAddress(str_replace(">", "", $emparts[1]), $emparts[0]);
         } else {
             $mail->AddAddress($email);
         }
     }
     if ($cc != "") {
         # modification for multiple is also necessary here, though a broken cc seems to be simply removed by phpmailer rather than breaking it.
         $ccs = $cc;
         $ccs = explode(',', $ccs);
         $ccs = array_map('trim', $ccs);
         global $userfullname;
         foreach ($ccs as $cc) {
             if (strstr($cc, "<")) {
                 $ccparts = explode("<", $cc);
                 $mail->AddCC(str_replace(">", "", $ccparts[1]), $ccparts[0]);
             } else {
                 $mail->AddCC($cc, $userfullname);
             }
         }
     }
     if ($bcc != "") {
         # modification for multiple is also necessary here, though a broken cc seems to be simply removed by phpmailer rather than breaking it.
         $bccs = $bcc;
         $bccs = explode(',', $bccs);
         $bccs = array_map('trim', $bccs);
         global $userfullname;
         foreach ($bccs as $bccemail) {
             if (strstr($bccemail, "<")) {
                 $bccparts = explode("<", $bccemail);
                 $mail->AddBCC(str_replace(">", "", $bccparts[1]), $bccparts[0]);
             } else {
                 $mail->AddBCC($bccemail, $userfullname);
             }
         }
     }
     $mail->CharSet = "utf-8";
     if (is_html($body)) {
         $mail->IsHTML(true);
     } else {
         $mail->IsHTML(false);
     }
     $mail->Subject = $subject;
     $mail->Body = $body;
     if (isset($embed_thumbnail) && isset($templatevars['thumbnail'])) {
         $mail->AddEmbeddedImage($templatevars['thumbnail'], $thumbcid, $thumbcid, 'base64', 'image/jpeg');
     }
     if (isset($images)) {
         foreach ($images as $image) {
             $mail->AddEmbeddedImage($image, basename($image), basename($image), 'base64', 'image/gif');
         }
     }
     if (isset($attachments)) {
         foreach ($attachments as $attachment) {
             $mail->AddAttachment($attachment, basename($attachment));
         }
     }
     if (is_html($body)) {
         $h2t = new html2text($body);
         $text = $h2t->get_text();
         $mail->AltBody = $text;
     }
     if (!$mail->Send()) {
         echo "Message could not be sent. <p>";
         echo "Mailer Error: " . $mail->ErrorInfo;
         exit;
     }
     hook("aftersendmailphpmailer", "", $email);
 }