diff --git a/main/admin/group_add.php b/main/admin/group_add.php index b0ad03be6c..2468608821 100755 --- a/main/admin/group_add.php +++ b/main/admin/group_add.php @@ -141,7 +141,7 @@ if( $form->validate()) { else { $emailbody = get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ".api_get_setting('siteName') ." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ". $username ."\n". get_lang('Pass')." : ".stripslashes($password)."\n\n" .get_lang('Address') ." ". api_get_setting('siteName') ." ". get_lang('Is') ." : ". $_configuration['root_web'] ."\n\n". get_lang('Problem'). "\n\n". get_lang('SignatureFormula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n". get_lang('Manager'). " ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".api_get_setting('emailAdministrator'); } - @api_mail($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin); + @api_mail_html($recipient_name, $email, $emailsubject, $emailbody, $sender_name, $email_admin); }*/ Security::clear_token(); diff --git a/main/inc/ajax/user_manager.ajax.php b/main/inc/ajax/user_manager.ajax.php index f7f22a7e18..1c82b160db 100755 --- a/main/inc/ajax/user_manager.ajax.php +++ b/main/inc/ajax/user_manager.ajax.php @@ -101,14 +101,15 @@ switch ($action) { 'userId' => $user_id ); - $result = api_mail( + $result = api_mail_html( $recipient_name, $user_info['mail'], $emailsubject, $emailbody, $sender_name, $email_admin, - '', + null, + null, $additionalParameters ); event_system(LOG_USER_ENABLE, LOG_USER_ID, $user_id); diff --git a/main/inc/global.inc.php b/main/inc/global.inc.php index f7759e2370..a7b822b27d 100755 --- a/main/inc/global.inc.php +++ b/main/inc/global.inc.php @@ -109,7 +109,6 @@ require_once $lib_path.'banner.lib.php'; require_once $lib_path.'fileManage.lib.php'; require_once $lib_path.'fileUpload.lib.php'; require_once $lib_path.'fileDisplay.lib.php'; -require_once $lib_path.'mail.lib.inc.php'; require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.lib.php'; require_once $lib_path.'course_category.lib.php'; diff --git a/main/inc/lib/api.lib.php b/main/inc/lib/api.lib.php index 28c0e47700..54c2cd7c4f 100644 --- a/main/inc/lib/api.lib.php +++ b/main/inc/lib/api.lib.php @@ -7782,3 +7782,192 @@ function api_create_protected_dir($name, $parentDirectory) return $isCreated; } + + +/** + * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully) + * Sender name and email can be specified, if not specified + * name and email of the platform admin are used + * + * @author Bert Vanderkimpen ICT&O UGent + * @author Yannick Warnier + * + * @param string name of recipient + * @param string email of recipient + * @param string email subject + * @param string email body + * @param string sender name + * @param string sender e-mail + * @param array extra headers in form $headers = array($name => $value) to allow parsing + * @param array data file (path and filename) + * @param array data to attach a file (optional) + * @param bool True for attaching a embedded file inside content html (optional) + * @return returns true if mail was sent + * @see class.phpmailer.php + */ +function api_mail_html( + $recipient_name, + $recipient_email, + $subject, + $message, + $senderName = '', + $senderEmail = '', + $extra_headers = array(), + $data_file = array(), + $embedded_image = false, + $additionalParameters = array() +) { + global $platform_email; + + $mail = new PHPMailer(); + $mail->Mailer = $platform_email['SMTP_MAILER']; + $mail->Host = $platform_email['SMTP_HOST']; + $mail->Port = $platform_email['SMTP_PORT']; + $mail->CharSet = $platform_email['SMTP_CHARSET']; + // Stay far below SMTP protocol 980 chars limit. + $mail->WordWrap = 200; + + if ($platform_email['SMTP_AUTH']) { + $mail->SMTPAuth = 1; + $mail->Username = $platform_email['SMTP_USER']; + $mail->Password = $platform_email['SMTP_PASS']; + } + + // 5 = low, 1 = high + $mail->Priority = 3; + $mail->SMTPKeepAlive = true; + + // Default values + $notification = new Notification(); + $defaultEmail = $notification->getDefaultPlatformSenderEmail(); + $defaultName = $notification->getDefaultPlatformSenderName(); + + // Error to admin. + $mail->AddCustomHeader('Errors-To: '.$defaultEmail); + + // If the parameter is set don't use the admin. + $senderName = !empty($senderName) ? $senderName : $defaultEmail; + $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultName; + + // Reply to first + if (isset($extra_headers['reply_to'])) { + $mail->AddReplyTo( + $extra_headers['reply_to']['mail'], + $extra_headers['reply_to']['name'] + ); + $mail->Sender = $extra_headers['reply_to']['mail']; + unset($extra_headers['reply_to']); + } + + $mail->SetFrom($senderEmail, $senderName); + + $mail->Subject = $subject; + $mail->AltBody = strip_tags( + str_replace('
', "\n", api_html_entity_decode($message)) + ); + + // Send embedded image. + if ($embedded_image) { + // Get all images html inside content. + preg_match_all("/]*)[\"\']?[^>]*>/i", $message, $m); + // Prepare new tag images. + $new_images_html = array(); + $i = 1; + if (!empty($m[1])) { + foreach ($m[1] as $image_path) { + $real_path = realpath($image_path); + $filename = basename($image_path); + $image_cid = $filename.'_'.$i; + $encoding = 'base64'; + $image_type = mime_content_type($real_path); + $mail->AddEmbeddedImage( + $real_path, + $image_cid, + $filename, + $encoding, + $image_type + ); + $new_images_html[] = ''; + $i++; + } + } + + // Replace origin image for new embedded image html. + $x = 0; + if (!empty($m[0])) { + foreach ($m[0] as $orig_img) { + $message = str_replace($orig_img, $new_images_html[$x], $message); + $x++; + } + } + } + $message = str_replace(array("\n\r", "\n", "\r"), '
', $message); + $mail->Body = ''.$message.''; + + // Attachment ... + if (!empty($data_file)) { + $mail->AddAttachment($data_file['path'], $data_file['filename']); + } + + // Only valid addresses are accepted. + if (is_array($recipient_email)) { + foreach ($recipient_email as $dest) { + if (api_valid_email($dest)) { + $mail->AddAddress($dest, $recipient_name); + } + } + } else { + if (api_valid_email($recipient_email)) { + $mail->AddAddress($recipient_email, $recipient_name); + } else { + return 0; + } + } + + if (is_array($extra_headers) && count($extra_headers) > 0) { + foreach ($extra_headers as $key => $value) { + switch (strtolower($key)) { + case 'encoding': + case 'content-transfer-encoding': + $mail->Encoding = $value; + break; + case 'charset': + $mail->Charset = $value; + break; + case 'contenttype': + case 'content-type': + $mail->ContentType = $value; + break; + default: + $mail->AddCustomHeader($key.':'.$value); + break; + } + } + } else { + if (!empty($extra_headers)) { + $mail->AddCustomHeader($extra_headers); + } + } + + // WordWrap the html body (phpMailer only fixes AltBody) FS#2988 + $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap); + // Send the mail message. + if (!$mail->Send()) { + error_log('ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'
'); + return 0; + } + + $plugin = new AppPlugin(); + $installedPluginsList = $plugin->getInstalledPluginListObject(); + foreach ($installedPluginsList as $installedPlugin) { + if ($installedPlugin->isMailPlugin and array_key_exists("smsType", $additionalParameters)) { + $className = str_replace("Plugin", "", get_class($installedPlugin)); + $smsObject = new $className; + $smsObject->send($additionalParameters); + } + } + + // Clear all the addresses. + $mail->ClearAddresses(); + return 1; +} diff --git a/main/inc/lib/events_email.class.php b/main/inc/lib/events_email.class.php index 9cb24455d5..2d6769b692 100755 --- a/main/inc/lib/events_email.class.php +++ b/main/inc/lib/events_email.class.php @@ -93,14 +93,11 @@ class EventsMail $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname']); // checks if there's a file we need to join to the mail - if (isset($values["certificate_pdf_file"])) - { + if (isset($values["certificate_pdf_file"])) { $message = str_replace("\n", "
", $message); - @api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, array($values['certificate_pdf_file'])); - } - else - { - @api_mail($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin); + api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin, null, array($values['certificate_pdf_file'])); + } else { + api_mail_html($recipient_name, $user_info["mail"], $subject, $message, $sender_name, $email_admin); } // If the mail only need to be send once (we know that thanks to the events.conf), we log it in the table @@ -150,7 +147,7 @@ class EventsMail // we send the mail $recipient_name = api_get_person_name($value['firstname'], $value['lastname']); - @api_mail($recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin); + api_mail_html($recipient_name, $value["email"], $subject, $message, $sender_name, $email_admin); // If the mail only need to be send once (we know that thanks to the events.conf, we log it in the table if ($event_config[$event_name]["sending_mail_once"]) diff --git a/main/inc/lib/mail.lib.inc.php b/main/inc/lib/mail.lib.inc.php deleted file mode 100755 index a972292686..0000000000 --- a/main/inc/lib/mail.lib.inc.php +++ /dev/null @@ -1,239 +0,0 @@ - - * - * @param string name of recipient - * @param string email of recipient - * @param string email subject - * @param string email body - * @param string sender name - * @param string sender e-mail - * @param array extra headers in form $headers = array($name => $value) to allow parsing - * @param array data file (path and filename) - * @param array data to attach a file (optional) - * @param bool True for attaching a embedded file inside content html (optional) - * @return returns true if mail was sent - * @see class.phpmailer.php - */ -function api_mail_html( - $recipient_name, - $recipient_email, - $subject, - $message, - $senderName = '', - $senderEmail = '', - $extra_headers = array(), - $data_file = array(), - $embedded_image = false, - $additionalParameters = array() -) { - global $platform_email; - - $mail = new PHPMailer(); - $mail->Mailer = $platform_email['SMTP_MAILER']; - $mail->Host = $platform_email['SMTP_HOST']; - $mail->Port = $platform_email['SMTP_PORT']; - $mail->CharSet = $platform_email['SMTP_CHARSET']; - // Stay far below SMTP protocol 980 chars limit. - $mail->WordWrap = 200; - - if ($platform_email['SMTP_AUTH']) { - $mail->SMTPAuth = 1; - $mail->Username = $platform_email['SMTP_USER']; - $mail->Password = $platform_email['SMTP_PASS']; - } - - // 5 = low, 1 = high - $mail->Priority = 3; - $mail->SMTPKeepAlive = true; - - // Default values - $notification = new Notification(); - $defaultEmail = $notification->getDefaultPlatformSenderEmail(); - $defaultName = $notification->getDefaultPlatformSenderName(); - - // Error to admin. - $mail->AddCustomHeader('Errors-To: '.$defaultEmail); - - // If the parameter is set don't use the admin. - $senderName = !empty($senderName) ? $senderName : $defaultEmail; - $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultName; - - // Reply to first - if (isset($extra_headers['reply_to'])) { - $mail->AddReplyTo( - $extra_headers['reply_to']['mail'], - $extra_headers['reply_to']['name'] - ); - $mail->Sender = $extra_headers['reply_to']['mail']; - unset($extra_headers['reply_to']); - } - - $mail->SetFrom($senderEmail, $senderName); - - $mail->Subject = $subject; - $mail->AltBody = strip_tags( - str_replace('
', "\n", api_html_entity_decode($message)) - ); - - // Send embedded image. - if ($embedded_image) { - // Get all images html inside content. - preg_match_all("/]*)[\"\']?[^>]*>/i", $message, $m); - // Prepare new tag images. - $new_images_html = array(); - $i = 1; - if (!empty($m[1])) { - foreach ($m[1] as $image_path) { - $real_path = realpath($image_path); - $filename = basename($image_path); - $image_cid = $filename.'_'.$i; - $encoding = 'base64'; - $image_type = mime_content_type($real_path); - $mail->AddEmbeddedImage( - $real_path, - $image_cid, - $filename, - $encoding, - $image_type - ); - $new_images_html[] = ''; - $i++; - } - } - - // Replace origin image for new embedded image html. - $x = 0; - if (!empty($m[0])) { - foreach ($m[0] as $orig_img) { - $message = str_replace($orig_img, $new_images_html[$x], $message); - $x++; - } - } - } - $message = str_replace(array("\n\r", "\n", "\r"), '
', $message); - $mail->Body = ''.$message.''; - - // Attachment ... - if (!empty($data_file)) { - $mail->AddAttachment($data_file['path'], $data_file['filename']); - } - - // Only valid addresses are accepted. - if (is_array($recipient_email)) { - foreach ($recipient_email as $dest) { - if (api_valid_email($dest)) { - $mail->AddAddress($dest, $recipient_name); - } - } - } else { - if (api_valid_email($recipient_email)) { - $mail->AddAddress($recipient_email, $recipient_name); - } else { - return 0; - } - } - - if (is_array($extra_headers) && count($extra_headers) > 0) { - foreach ($extra_headers as $key => $value) { - switch (strtolower($key)) { - case 'encoding': - case 'content-transfer-encoding': - $mail->Encoding = $value; - break; - case 'charset': - $mail->Charset = $value; - break; - case 'contenttype': - case 'content-type': - $mail->ContentType = $value; - break; - default: - $mail->AddCustomHeader($key.':'.$value); - break; - } - } - } else { - if (!empty($extra_headers)) { - $mail->AddCustomHeader($extra_headers); - } - } - - // WordWrap the html body (phpMailer only fixes AltBody) FS#2988 - $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap); - // Send the mail message. - if (!$mail->Send()) { - error_log('ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'
'); - return 0; - } - - $plugin = new AppPlugin(); - $installedPluginsList = $plugin->getInstalledPluginListObject(); - foreach ($installedPluginsList as $installedPlugin) { - if ($installedPlugin->isMailPlugin and array_key_exists("smsType", $additionalParameters)) { - $className = str_replace("Plugin", "", get_class($installedPlugin)); - $smsObject = new $className; - $smsObject->send($additionalParameters); - } - } - - // Clear all the addresses. - $mail->ClearAddresses(); - return 1; -} diff --git a/plugin/buycourses/src/process_confirm.php b/plugin/buycourses/src/process_confirm.php index 18d5f917fd..db6b8043b2 100644 --- a/plugin/buycourses/src/process_confirm.php +++ b/plugin/buycourses/src/process_confirm.php @@ -72,7 +72,7 @@ if (isset($_POST['Confirm'])) { $message = str_replace("{{reference}}", $reference, $message); $message .= $text; - api_mail($name, $email, $asunto, $message); + api_mail_html($name, $email, $asunto, $message); // Return to course list header('Location:list.php'); } diff --git a/tests/main/inc/lib/login.lib.test.php b/tests/main/inc/lib/login.lib.test.php index 3d9350aef8..4cea8fdd10 100755 --- a/tests/main/inc/lib/login.lib.test.php +++ b/tests/main/inc/lib/login.lib.test.php @@ -41,7 +41,6 @@ class TestLogin extends UnitTestCase { } function testhandle_encrypted_password() { - require_once api_get_path (LIBRARY_PATH).'mail.lib.inc.php'; global $charset; global $_configuration; ob_start(); diff --git a/tests/main/inc/lib/mail.lib.inc.test.php b/tests/main/inc/lib/mail.lib.inc.test.php index 28f948dadd..abf3112911 100755 --- a/tests/main/inc/lib/mail.lib.inc.test.php +++ b/tests/main/inc/lib/mail.lib.inc.test.php @@ -1,5 +1,4 @@