192 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 =
'')
194 global $conf, $dolibarr_main_data_root, $user;
196 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");
197 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
201 if (is_array($mimefilename_list)) {
202 foreach ($mimefilename_list as $key => $val) {
209 $this->sendcontext = $sendcontext;
212 $this->sendmode =
'';
213 if (!empty($this->sendcontext)) {
214 $smtpContextKey = strtoupper($this->sendcontext);
216 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
217 $this->sendmode = $smtpContextSendMode;
220 if (empty($this->sendmode)) {
230 $moreinheader .=
"Feedback-ID: ".$trackid.
':'.dol_getprefix(
'email').
":dolib\r\n";
236 $this->eol2 =
"\r\n";
240 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
244 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
247 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
250 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
252 if (empty($subject)) {
253 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
254 $this->error =
'ErrorSubjectIsRequired';
258 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
263 if ($msgishtml == -1) {
264 $this->msgishtml = 0;
266 $this->msgishtml = 1;
269 $this->msgishtml = $msgishtml;
272 global $dolibarr_main_url_root;
275 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
276 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
280 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
283 $this->msgishtml = 1;
285 dol_syslog(
"CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
288 if ($this->msgishtml) {
297 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
298 if ((
int) $conf->entity !== 1) {
299 $dolibarr_main_data_root_images.=
'/'.$conf->entity.
'/';
301 $findimg = $this->
findHtmlImages($dolibarr_main_data_root_images.
'/medias');
303 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
304 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
313 if ($resultImageData < 0) {
314 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
315 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
318 $findimg += $resultImageData;
323 foreach ($this->html_images as $i => $val) {
324 if ($this->html_images[$i]) {
325 $this->atleastoneimage = 1;
326 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
327 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
329 $posindice = count($filename_list);
330 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
331 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
332 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
334 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
337 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
339 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
348 if (is_array($filename_list)) {
349 foreach ($filename_list as $i => $val) {
350 if ($filename_list[$i]) {
351 $this->atleastonefile = 1;
352 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);
361 foreach ($listofemailstoadd as $key => $val) {
362 $emailtoadd = $listofemailstoadd[$key];
363 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
364 if (!empty($user) && !empty($user->email)) {
365 $emailtoadd = $user->email;
370 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
374 $listofemailstoadd[$key] = $emailtoadd;
376 unset($listofemailstoadd[$key]);
379 if (!empty($listofemailstoadd)) {
380 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
387 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
391 $tabto = explode(
",", $to);
392 foreach ($tabto as $key => $addrto) {
394 if (in_array($addrto[0], $listofemailstonotsendto)) {
402 $to = implode(
',', $tabto);
406 $tabcc = explode(
',', $addr_cc);
407 foreach ($tabcc as $key => $cc) {
409 if (in_array($cc[0], $listofemailstonotsendto)) {
417 $addr_cc = implode(
',', $tabcc);
421 $tabbcc = explode(
',', $addr_bcc);
422 foreach ($tabbcc as $key => $bcc) {
424 if (in_array($bcc[0], $listofemailstonotsendto)) {
425 unset($tabbcc[$key]);
432 $addr_bcc = implode(
',', $tabbcc);
436 if (empty($replyto)) {
444 $this->subject = $subject;
450 $this->deliveryreceipt = $deliveryreceipt;
453 $this->trackid = $trackid;
454 $this->in_reply_to = $in_reply_to;
455 $this->references = $references;
457 $this->filename_list = $filename_list;
458 $this->mimetype_list = $mimetype_list;
459 $this->mimefilename_list = $mimefilename_list;
460 $this->cid_list = $cid_list;
465 $this->addr_bcc =
'';
468 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
469 if (!empty($this->sendcontext)) {
470 $smtpContextKey = strtoupper($this->sendcontext);
472 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
473 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
477 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
481 if ($this->sendmode ==
'mail') {
492 if (!empty($moreinheader)) {
493 $smtp_headers .= $moreinheader;
499 if (!empty($this->html)) {
512 if (!empty($this->atleastonefile)) {
513 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
517 $this->headers = $smtp_headers.$mime_headers;
520 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
523 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
524 $this->message .= $text_body.$files_encoded;
525 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
526 } elseif ($this->sendmode ==
'smtps') {
529 $host = dol_getprefix(
'email');
531 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
532 $smtps =
new SMTPs();
533 $smtps->setCharSet($conf->file->character_set_client);
536 $subjecttouse = $this->subject;
541 $smtps->setSubject($subjecttouse);
546 $smtps->setTrackId($this->trackid);
548 if (!empty($this->in_reply_to)) {
549 $smtps->setInReplyTo($this->in_reply_to);
551 if (!empty($this->references)) {
552 $smtps->setReferences($this->references);
555 if (!empty($moreinheader)) {
556 $smtps->setMoreInHeader($moreinheader);
561 if (!empty($this->html)) {
571 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
573 if ($this->msgishtml) {
574 $smtps->setBodyContent($msg,
'html');
576 $smtps->setBodyContent($msg,
'plain');
579 if ($this->atleastoneimage) {
580 foreach ($this->images_encoded as $img) {
581 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
585 if (!empty($this->atleastonefile)) {
586 foreach ($filename_list as $i => $val) {
587 $content = file_get_contents($filename_list[$i]);
588 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
592 $smtps->setCC($this->addr_cc);
593 $smtps->setBCC($this->addr_bcc);
594 $smtps->setErrorsTo($this->errors_to);
595 $smtps->setDeliveryReceipt($this->deliveryreceipt);
597 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
600 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
602 $this->smtps = $smtps;
603 } elseif ($this->sendmode ==
'swiftmailer') {
606 $host = dol_getprefix(
'email');
608 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
611 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
613 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
617 $this->message =
new Swift_Message();
620 $headers = $this->message->getHeaders();
622 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
623 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
624 $headerID = $this->msgid;
625 $msgid = $headers->get(
'Message-ID');
626 $msgid->setId($headerID);
629 if (!empty($this->in_reply_to)) {
630 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
633 if (!empty($this->references)) {
634 $headers->addIdHeader(
'References', $this->references);
637 if (!empty($moreinheader)) {
638 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
639 foreach ($moreinheaderarray as $moreinheaderval) {
640 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
641 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
642 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
649 $this->message->setSubject($this->subject);
651 $this->errors[] = $e->getMessage();
656 if (!empty($this->addr_from)) {
660 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
661 $adressEmailFrom = array();
662 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
663 $adressEmailFrom = reset($adressEmailFrom);
664 if ($emailMatchs !==
false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
665 $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
673 $this->errors[] = $e->getMessage();
678 if (!empty($this->addr_to)) {
682 $this->errors[] = $e->getMessage();
686 if (!empty($this->reply_to)) {
690 $this->errors[] = $e->getMessage();
694 if (!empty($this->errors_to)) {
696 $headers->addTextHeader(
'Errors-To', $this->
getValidAddress($this->errors_to, 0));
698 $this->errors[] = $e->getMessage();
703 $this->message->setCharSet($conf->file->character_set_client);
705 $this->errors[] = $e->getMessage();
708 if (!empty($this->html)) {
717 if ($this->atleastoneimage) {
718 foreach ($this->html_images as $img) {
720 $attachment = Swift_Image::fromPath($img[
'fullpath']);
722 $imgcid = $this->message->embed($attachment);
724 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
726 foreach ($this->images_encoded as $img) {
728 $attachment = Swift_Image::fromPath($img[
'fullpath']);
730 $imgcid = $this->message->embed($attachment);
732 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
736 if ($this->msgishtml) {
737 $this->message->setBody($msg,
'text/html');
739 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
741 $this->message->setBody($msg,
'text/plain');
743 $this->message->addPart(
dol_nl2br($msg),
'text/html');
746 if (!empty($this->atleastonefile)) {
747 foreach ($filename_list as $i => $val) {
749 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
750 if (!empty($mimefilename_list[$i])) {
751 $attachment->setFilename($mimefilename_list[$i]);
753 $this->message->attach($attachment);
757 if (!empty($this->addr_cc)) {
761 $this->errors[] = $e->getMessage();
764 if (!empty($this->addr_bcc)) {
768 $this->errors[] = $e->getMessage();
771 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
773 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
775 $this->errors[] = $e->getMessage();
781 $this->error =
'Bad value for sendmode';
794 global $conf, $db, $langs, $hookmanager;
796 $errorlevel = error_reporting();
802 if (!is_object($hookmanager)) {
803 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
806 $hookmanager->initHooks(array(
'mail'));
808 $parameters = array();
810 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
812 $this->error =
"Error in hook maildao sendMail ".$reshook;
813 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
821 $sendingmode = $this->sendmode;
822 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
824 $listofmethods = array();
825 $listofmethods[
'mail'] =
'PHP mail function';
827 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
831 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
832 $linktoadminemailend =
'</a>';
833 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
834 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
835 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
836 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
838 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
839 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
842 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
848 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
850 $tmparray1 = explode(
',', $this->addr_to);
851 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
852 $this->error =
'Too much recipients in to:';
853 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
857 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
859 $tmparray2 = explode(
',', $this->addr_cc);
860 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
861 $this->error =
'Too much recipients in cc:';
862 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
866 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
868 $tmparray3 = explode(
',', $this->addr_bcc);
869 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
870 $this->error =
'Too much recipients in bcc:';
871 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
875 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
877 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
878 $this->error =
'Too much recipients in to:, cc:, bcc:';
879 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
883 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
884 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
885 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
886 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
887 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
888 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
889 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
890 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
891 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
892 if (!empty($this->sendcontext)) {
893 $smtpContextKey = strtoupper($this->sendcontext);
895 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
896 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
897 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
898 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
899 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
900 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
901 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
902 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
903 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
904 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
909 if ($this->sendmode ==
'mail') {
912 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
917 if (isset($_SERVER[
"WINDIR"])) {
918 if (empty($this->addr_from)) {
919 $this->addr_from =
'robot@example.com';
921 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
934 if ($res && !$this->subject) {
935 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
936 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
940 if ($res && !$dest) {
941 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
942 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
947 $additionnalparam =
'';
954 $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) :
''));
957 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
961 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
965 if (preg_match(
'/^win/i', PHP_OS)) {
968 if (preg_match(
'/^mac/i', PHP_OS)) {
972 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
974 $this->message = stripslashes($this->message);
981 $subjecttouse = $this->subject;
986 if (!empty($additionnalparam)) {
987 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
989 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
993 $langs->load(
"errors");
994 $this->error =
"Failed to send mail with php mail";
996 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
998 $this->error .=
".<br>";
999 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1000 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1006 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1010 if (isset($_SERVER[
"WINDIR"])) {
1011 @ini_restore(
'sendmail_from');
1016 ini_restore(
'SMTP');
1019 ini_restore(
'smtp_port');
1021 } elseif ($this->sendmode ==
'smtps') {
1022 if (!is_object($this->smtps)) {
1023 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1024 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1030 $this->smtps->setTransportType(0);
1033 if (empty($conf->global->$keyforsmtpserver)) {
1034 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1036 if (empty($conf->global->$keyforsmtpport)) {
1037 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1043 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1046 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1049 $server = ($secure ? $secure.
'://' :
'').$server;
1053 $this->smtps->setHost($server);
1054 $this->smtps->setPort($port);
1058 if (!empty($conf->global->$keyforsmtpid)) {
1060 $this->smtps->setID($loginid);
1062 if (!empty($conf->global->$keyforsmtppw)) {
1064 $this->smtps->setPW($loginpass);
1068 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1073 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1074 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1076 $keyforprovider =
'';
1078 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1079 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1081 if (!empty($supportedoauth2array)) {
1082 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1083 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1084 $OAUTH_SERVICENAME = $nameofservice;
1086 $OAUTH_SERVICENAME =
'Unknown';
1089 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1091 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1093 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
1095 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1099 if (is_object($tokenobj)) {
1100 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1103 if (is_object($tokenobj) && $expire) {
1104 $credentials =
new Credentials(
1109 $serviceFactory = new \OAuth\ServiceFactory();
1110 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1112 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1114 $refreshtoken = $tokenobj->getRefreshToken();
1115 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1116 $tokenobj->setRefreshToken($refreshtoken);
1117 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1119 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1122 if (is_object($tokenobj)) {
1123 $this->smtps->setToken($tokenobj->getAccessToken());
1125 $this->error =
"Token not found";
1129 $this->error = $e->getMessage();
1130 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1135 $from = $this->smtps->getFrom(
'org');
1136 if ($res && !$from) {
1137 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1138 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1141 $dest = $this->smtps->getTo();
1142 if ($res && !$dest) {
1143 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1144 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1152 $this->smtps->setDebug(
true);
1155 $result = $this->smtps->sendMsg();
1163 $smtperrorcode = $this->smtps->lastretval;
1164 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1166 if ($smtperrorcode ==
'421') {
1181 $result = $this->smtps->getErrors();
1182 if (empty($this->error) && empty($result)) {
1183 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1186 if (empty($this->error)) {
1187 $this->error = $result;
1189 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1197 } elseif ($this->sendmode ==
'swiftmailer') {
1200 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1203 if (empty($conf->global->$keyforsmtpserver)) {
1204 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1206 if (empty($conf->global->$keyforsmtpport)) {
1207 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1213 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1216 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1220 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalString($keyforsmtpport), $secure);
1222 if (!empty($conf->global->$keyforsmtpid)) {
1225 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1229 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1234 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1235 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1237 $keyforprovider =
'';
1239 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1240 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1242 if (!empty($supportedoauth2array)) {
1243 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1244 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1245 $OAUTH_SERVICENAME = $nameofservice;
1247 $OAUTH_SERVICENAME =
'Unknown';
1250 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1252 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1254 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
1257 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1261 if (is_object($tokenobj)) {
1262 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1265 if (is_object($tokenobj) && $expire) {
1266 $credentials =
new Credentials(
1271 $serviceFactory = new \OAuth\ServiceFactory();
1272 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1274 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1276 $refreshtoken = $tokenobj->getRefreshToken();
1277 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1278 $tokenobj->setRefreshToken($refreshtoken);
1279 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1281 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1284 if (is_object($tokenobj)) {
1285 $this->transport->setAuthMode(
'XOAUTH2');
1286 $this->transport->setPassword($tokenobj->getAccessToken());
1288 $this->errors[] =
"Token not found";
1292 $this->errors[] = $e->getMessage();
1293 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1297 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1302 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1303 $this->message->setEncoder($contentEncoderBase64);
1306 $this->mailer =
new Swift_Mailer($this->transport);
1313 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1314 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1319 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1322 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1328 $failedRecipients = array();
1330 $result = $this->mailer->send($this->message, $failedRecipients);
1332 $this->errors[] = $e->getMessage();
1339 if (!empty($this->error) || !empty($this->errors) || !$result) {
1340 if (!empty($failedRecipients)) {
1341 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1342 $this->errors[] = $this->error;
1344 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1351 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1357 $this->error =
'Bad value for sendmode';
1362 foreach ($this->html_images as $val) {
1363 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1368 $parameters = array(
'sent' => $res);
1370 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1372 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1373 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1378 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1379 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1382 error_reporting($errorlevel);