193 public function __construct($subject, $to, $from, $msg, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $addr_cc =
"", $addr_bcc =
"", $deliveryreceipt = 0, $msgishtml = 0, $errors_to =
'', $css =
'', $trackid =
'', $moreinheader =
'', $sendcontext =
'standard', $replyto =
'', $upload_dir_tmp =
'', $in_reply_to =
'', $references =
'')
195 global $conf, $dolibarr_main_data_root, $user;
197 dol_syslog(
"CMailFile::CMailfile: charset=".$conf->file->character_set_client.
" from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to, replyto=$replyto trackid=$trackid sendcontext=$sendcontext");
198 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
202 if (is_array($mimefilename_list)) {
203 foreach ($mimefilename_list as $key => $val) {
210 $this->sendcontext = $sendcontext;
213 $this->sendmode =
'';
214 if (!empty($this->sendcontext)) {
215 $smtpContextKey = strtoupper($this->sendcontext);
217 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
218 $this->sendmode = $smtpContextSendMode;
221 if (empty($this->sendmode)) {
231 $moreinheader .=
"Feedback-ID: ".$trackid.
':'.dol_getprefix(
'email').
":dolib\r\n";
237 $this->eol2 =
"\r\n";
241 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
245 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
248 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
251 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
253 if (empty($subject)) {
254 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
255 $this->error =
'ErrorSubjectIsRequired';
259 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
264 if ($msgishtml == -1) {
265 $this->msgishtml = 0;
267 $this->msgishtml = 1;
270 $this->msgishtml = $msgishtml;
273 global $dolibarr_main_url_root;
276 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
277 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
281 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
284 $this->msgishtml = 1;
286 dol_syslog(
"CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
289 if ($this->msgishtml) {
298 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
299 if ((
int) $conf->entity !== 1) {
300 $dolibarr_main_data_root_images.=
'/'.$conf->entity.
'/';
302 $findimg = $this->
findHtmlImages($dolibarr_main_data_root_images.
'/medias');
304 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
305 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
314 if ($resultImageData < 0) {
315 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
316 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
319 $findimg += $resultImageData;
324 foreach ($this->html_images as $i => $val) {
325 if ($this->html_images[$i]) {
326 $this->atleastoneimage = 1;
327 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
328 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
330 $posindice = count($filename_list);
331 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
332 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
333 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
335 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
338 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
340 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
349 if (is_array($filename_list)) {
350 foreach ($filename_list as $i => $val) {
351 if ($filename_list[$i]) {
352 $this->atleastonefile = 1;
353 dol_syslog(
"CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].
", mimetype_list[$i]=".$mimetype_list[$i].
" mimefilename_list[$i]=".$mimefilename_list[$i].
" cid_list[$i]=".(empty($cid_list[$i]) ?
'' : $cid_list[$i]), LOG_DEBUG);
362 foreach ($listofemailstoadd as $key => $val) {
363 $emailtoadd = $listofemailstoadd[$key];
364 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
365 if (!empty($user) && !empty($user->email)) {
366 $emailtoadd = $user->email;
371 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
375 $listofemailstoadd[$key] = $emailtoadd;
377 unset($listofemailstoadd[$key]);
380 if (!empty($listofemailstoadd)) {
381 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
388 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
392 $tabto = explode(
",", $to);
393 foreach ($tabto as $key => $addrto) {
395 if (in_array($addrto[0], $listofemailstonotsendto)) {
403 $to = implode(
',', $tabto);
407 $tabcc = explode(
',', $addr_cc);
408 foreach ($tabcc as $key => $cc) {
410 if (in_array($cc[0], $listofemailstonotsendto)) {
418 $addr_cc = implode(
',', $tabcc);
422 $tabbcc = explode(
',', $addr_bcc);
423 foreach ($tabbcc as $key => $bcc) {
425 if (in_array($bcc[0], $listofemailstonotsendto)) {
426 unset($tabbcc[$key]);
433 $addr_bcc = implode(
',', $tabbcc);
437 if (empty($replyto)) {
445 $this->subject = $subject;
451 $this->deliveryreceipt = $deliveryreceipt;
454 $this->trackid = $trackid;
455 $this->in_reply_to = $in_reply_to;
456 $this->references = $references;
458 $this->filename_list = $filename_list;
459 $this->mimetype_list = $mimetype_list;
460 $this->mimefilename_list = $mimefilename_list;
461 $this->cid_list = $cid_list;
466 $this->addr_bcc =
'';
469 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
470 if (!empty($this->sendcontext)) {
471 $smtpContextKey = strtoupper($this->sendcontext);
473 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
474 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
478 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
482 if ($this->sendmode ==
'mail') {
493 if (!empty($moreinheader)) {
494 $smtp_headers .= $moreinheader;
500 if (!empty($this->html)) {
513 if (!empty($this->atleastonefile)) {
514 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
518 $this->headers = $smtp_headers.$mime_headers;
521 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
524 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
525 $this->message .= $text_body.$files_encoded;
526 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
527 } elseif ($this->sendmode ==
'smtps') {
530 $host = dol_getprefix(
'email');
532 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
533 $smtps =
new SMTPs();
534 $smtps->setCharSet($conf->file->character_set_client);
537 $subjecttouse = $this->subject;
542 $smtps->setSubject($subjecttouse);
547 $smtps->setTrackId($this->trackid);
549 if (!empty($this->in_reply_to)) {
550 $smtps->setInReplyTo($this->in_reply_to);
552 if (!empty($this->references)) {
553 $smtps->setReferences($this->references);
556 if (!empty($moreinheader)) {
557 $smtps->setMoreInHeader($moreinheader);
562 if (!empty($this->html)) {
572 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
574 if ($this->msgishtml) {
575 $smtps->setBodyContent($msg,
'html');
577 $smtps->setBodyContent($msg,
'plain');
580 if ($this->atleastoneimage) {
581 foreach ($this->images_encoded as $img) {
582 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
586 if (!empty($this->atleastonefile)) {
587 foreach ($filename_list as $i => $val) {
588 $content = file_get_contents($filename_list[$i]);
589 if (empty($cid_list[$i])) {
590 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
595 $smtps->setCC($this->addr_cc);
596 $smtps->setBCC($this->addr_bcc);
597 $smtps->setErrorsTo($this->errors_to);
598 $smtps->setDeliveryReceipt($this->deliveryreceipt);
600 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
603 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
605 $this->smtps = $smtps;
606 } elseif ($this->sendmode ==
'swiftmailer') {
609 $host = dol_getprefix(
'email');
611 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
614 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
616 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
620 $this->message =
new Swift_Message();
623 $headers = $this->message->getHeaders();
625 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
626 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
627 $headerID = $this->msgid;
628 $msgid = $headers->get(
'Message-ID');
629 $msgid->setId($headerID);
632 if (!empty($this->in_reply_to)) {
633 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
636 if (!empty($this->references)) {
637 $headers->addIdHeader(
'References', $this->references);
640 if (!empty($moreinheader)) {
641 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
642 foreach ($moreinheaderarray as $moreinheaderval) {
643 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
644 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
645 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
652 $this->message->setSubject($this->subject);
654 $this->errors[] = $e->getMessage();
659 if (!empty($this->addr_from)) {
663 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
664 $adressEmailFrom = array();
665 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
666 $adressEmailFrom = reset($adressEmailFrom);
667 if ($emailMatchs !==
false && filter_var(
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID'), FILTER_VALIDATE_EMAIL) &&
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID') !== $adressEmailFrom[0]) {
676 $this->errors[] = $e->getMessage();
681 if (!empty($this->addr_to)) {
685 $this->errors[] = $e->getMessage();
689 if (!empty($this->reply_to)) {
693 $this->errors[] = $e->getMessage();
697 if (!empty($this->errors_to)) {
699 $headers->addTextHeader(
'Errors-To', $this->
getValidAddress($this->errors_to, 0));
701 $this->errors[] = $e->getMessage();
706 $this->message->setCharSet($conf->file->character_set_client);
708 $this->errors[] = $e->getMessage();
711 if (!empty($this->html)) {
720 if ($this->atleastoneimage) {
721 foreach ($this->html_images as $img) {
723 $attachment = Swift_Image::fromPath($img[
'fullpath']);
725 $imgcid = $this->message->embed($attachment);
727 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
729 foreach ($this->images_encoded as $img) {
731 $attachment = Swift_Image::fromPath($img[
'fullpath']);
733 $imgcid = $this->message->embed($attachment);
735 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
739 if ($this->msgishtml) {
740 $this->message->setBody($msg,
'text/html');
742 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
744 $this->message->setBody($msg,
'text/plain');
746 $this->message->addPart(
dol_nl2br($msg),
'text/html');
749 if (!empty($this->atleastonefile)) {
750 foreach ($filename_list as $i => $val) {
752 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
753 if (!empty($mimefilename_list[$i])) {
754 $attachment->setFilename($mimefilename_list[$i]);
756 $this->message->attach($attachment);
760 if (!empty($this->addr_cc)) {
764 $this->errors[] = $e->getMessage();
767 if (!empty($this->addr_bcc)) {
771 $this->errors[] = $e->getMessage();
774 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
776 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
778 $this->errors[] = $e->getMessage();
784 $this->error =
'Bad value for sendmode';
797 global $conf, $db, $langs, $hookmanager;
799 $errorlevel = error_reporting();
805 if (!is_object($hookmanager)) {
806 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
809 $hookmanager->initHooks(array(
'mail'));
811 $parameters = array();
813 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
815 $this->error =
"Error in hook maildao sendMail ".$reshook;
816 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
824 $sendingmode = $this->sendmode;
825 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
827 $listofmethods = array();
828 $listofmethods[
'mail'] =
'PHP mail function';
830 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
834 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
835 $linktoadminemailend =
'</a>';
836 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
837 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
838 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
839 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
841 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
842 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
845 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
851 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
853 $tmparray1 = explode(
',', $this->addr_to);
854 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
855 $this->error =
'Too much recipients in to:';
856 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
860 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
862 $tmparray2 = explode(
',', $this->addr_cc);
863 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
864 $this->error =
'Too much recipients in cc:';
865 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
869 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
871 $tmparray3 = explode(
',', $this->addr_bcc);
872 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
873 $this->error =
'Too much recipients in bcc:';
874 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
878 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
880 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
881 $this->error =
'Too much recipients in to:, cc:, bcc:';
882 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
886 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
887 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
888 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
889 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
890 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
891 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
892 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
893 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
894 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
895 if (!empty($this->sendcontext)) {
896 $smtpContextKey = strtoupper($this->sendcontext);
898 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
899 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
900 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
901 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
902 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
903 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
904 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
905 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
906 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
907 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
912 if ($this->sendmode ==
'mail') {
915 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
920 if (isset($_SERVER[
"WINDIR"])) {
921 if (empty($this->addr_from)) {
922 $this->addr_from =
'robot@example.com';
924 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
937 if ($res && !$this->subject) {
938 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
939 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
943 if ($res && !$dest) {
944 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
945 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
950 $additionnalparam =
'';
957 $additionnalparam .= ($additionnalparam ?
' ' :
'').(
getDolGlobalString(
'MAIN_MAIL_ERRORS_TO') ?
'-f'.$this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO, 2) : ($this->addr_from !=
'' ?
'-f'.$this->getValidAddress($this->addr_from, 2) :
''));
960 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
964 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
968 if (preg_match(
'/^win/i', PHP_OS)) {
971 if (preg_match(
'/^mac/i', PHP_OS)) {
975 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
977 $this->message = stripslashes($this->message);
984 $subjecttouse = $this->subject;
989 if (!empty($additionnalparam)) {
990 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
992 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
996 $langs->load(
"errors");
997 $this->error =
"Failed to send mail with php mail";
999 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1001 $this->error .=
".<br>";
1002 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1003 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1009 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1013 if (isset($_SERVER[
"WINDIR"])) {
1014 @ini_restore(
'sendmail_from');
1019 ini_restore(
'SMTP');
1022 ini_restore(
'smtp_port');
1024 } elseif ($this->sendmode ==
'smtps') {
1025 if (!is_object($this->smtps)) {
1026 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1027 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1033 $this->smtps->setTransportType(0);
1036 if (empty($conf->global->$keyforsmtpserver)) {
1037 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1039 if (empty($conf->global->$keyforsmtpport)) {
1040 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1046 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1049 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1052 $server = ($secure ? $secure.
'://' :
'').$server;
1056 $this->smtps->setHost($server);
1057 $this->smtps->setPort($port);
1061 if (!empty($conf->global->$keyforsmtpid)) {
1063 $this->smtps->setID($loginid);
1065 if (!empty($conf->global->$keyforsmtppw)) {
1067 $this->smtps->setPW($loginpass);
1071 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1076 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1077 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1079 $keyforprovider =
'';
1081 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1082 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1084 if (!empty($supportedoauth2array)) {
1085 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1086 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1087 $OAUTH_SERVICENAME = $nameofservice;
1089 $OAUTH_SERVICENAME =
'Unknown';
1092 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1094 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1096 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
1098 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1102 if (is_object($tokenobj)) {
1103 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1106 if (is_object($tokenobj) && $expire) {
1107 $credentials =
new Credentials(
1112 $serviceFactory = new \OAuth\ServiceFactory();
1113 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1115 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1117 $refreshtoken = $tokenobj->getRefreshToken();
1118 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1119 $tokenobj->setRefreshToken($refreshtoken);
1120 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1122 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1125 if (is_object($tokenobj)) {
1126 $this->smtps->setToken($tokenobj->getAccessToken());
1128 $this->error =
"Token not found";
1132 $this->error = $e->getMessage();
1133 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1138 $from = $this->smtps->getFrom(
'org');
1139 if ($res && !$from) {
1140 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1141 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1144 $dest = $this->smtps->getTo();
1145 if ($res && !$dest) {
1146 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1147 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1155 $this->smtps->setDebug(
true);
1158 $result = $this->smtps->sendMsg();
1166 $smtperrorcode = $this->smtps->lastretval;
1167 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1169 if ($smtperrorcode ==
'421') {
1184 $result = $this->smtps->getErrors();
1185 if (empty($this->error) && empty($result)) {
1186 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1189 if (empty($this->error)) {
1190 $this->error = $result;
1192 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1200 } elseif ($this->sendmode ==
'swiftmailer') {
1203 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1206 if (empty($conf->global->$keyforsmtpserver)) {
1207 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1209 if (empty($conf->global->$keyforsmtpport)) {
1210 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1216 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1219 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1223 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalString($keyforsmtpport), $secure);
1225 if (!empty($conf->global->$keyforsmtpid)) {
1228 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1232 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1237 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1238 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1240 $keyforprovider =
'';
1242 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1243 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1245 if (!empty($supportedoauth2array)) {
1246 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1247 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1248 $OAUTH_SERVICENAME = $nameofservice;
1250 $OAUTH_SERVICENAME =
'Unknown';
1253 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1255 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1257 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
1260 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1264 if (is_object($tokenobj)) {
1265 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1268 if (is_object($tokenobj) && $expire) {
1269 $credentials =
new Credentials(
1274 $serviceFactory = new \OAuth\ServiceFactory();
1275 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1277 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1279 $refreshtoken = $tokenobj->getRefreshToken();
1280 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1281 $tokenobj->setRefreshToken($refreshtoken);
1282 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1284 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1287 if (is_object($tokenobj)) {
1288 $this->transport->setAuthMode(
'XOAUTH2');
1289 $this->transport->setPassword($tokenobj->getAccessToken());
1291 $this->errors[] =
"Token not found";
1295 $this->errors[] = $e->getMessage();
1296 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1300 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1305 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1306 $this->message->setEncoder($contentEncoderBase64);
1309 $this->mailer =
new Swift_Mailer($this->transport);
1316 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1317 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1322 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1325 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1331 $failedRecipients = array();
1333 $result = $this->mailer->send($this->message, $failedRecipients);
1335 $this->errors[] = $e->getMessage();
1342 if (!empty($this->error) || !empty($this->errors) || !$result) {
1343 if (!empty($failedRecipients)) {
1344 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1345 $this->errors[] = $this->error;
1347 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1354 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1360 $this->error =
'Bad value for sendmode';
1365 foreach ($this->html_images as $val) {
1366 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1371 $parameters = array(
'sent' => $res);
1373 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1375 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1376 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1381 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1382 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1385 error_reporting($errorlevel);