228 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 =
'')
230 global
$conf, $dolibarr_main_data_root, $user;
232 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");
233 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
237 if (is_array($mimefilename_list)) {
238 foreach ($mimefilename_list as $key => $val) {
245 $this->sendcontext = $sendcontext;
248 $this->sendmode =
'';
249 if (!empty($this->sendcontext)) {
250 $smtpContextKey = strtoupper($this->sendcontext);
252 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
253 $this->sendmode = $smtpContextSendMode;
256 if (empty($this->sendmode)) {
266 $moreinheader .=
"Feedback-ID: ".$trackid.
':'.dol_getprefix(
'email').
":dolib\r\n";
272 $this->eol2 =
"\r\n";
276 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
280 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
283 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"),
'3');
286 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"),
'3');
288 if (empty($subject)) {
289 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
290 $this->error =
'ErrorSubjectIsRequired';
294 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
299 if ($msgishtml == -1) {
300 $this->msgishtml = 0;
302 $this->msgishtml = 1;
305 $this->msgishtml = $msgishtml;
308 global $dolibarr_main_url_root;
311 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
312 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
316 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
319 $this->msgishtml = 1;
321 dol_syslog(
"CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
324 if ($this->msgishtml) {
333 $findimg = $this->
findHtmlImages($dolibarr_main_data_root.
'/medias');
335 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
336 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
345 if ($resultImageData < 0) {
346 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
347 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
350 $findimg += $resultImageData;
355 foreach ($this->html_images as $i => $val) {
356 if ($this->html_images[$i]) {
357 $this->atleastoneimage = 1;
358 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
359 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
361 $posindice = count($filename_list);
362 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
363 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
364 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
366 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
369 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
371 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
380 if (is_array($filename_list)) {
381 foreach ($filename_list as $i => $val) {
382 if ($filename_list[$i]) {
383 $this->atleastonefile = 1;
384 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);
393 foreach ($listofemailstoadd as $key => $val) {
394 $emailtoadd = $listofemailstoadd[$key];
395 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
396 if (!empty($user) && !empty($user->email)) {
397 $emailtoadd = $user->email;
402 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
406 $listofemailstoadd[$key] = $emailtoadd;
408 unset($listofemailstoadd[$key]);
411 if (!empty($listofemailstoadd)) {
412 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
420 $tabto = explode(
",", $to);
421 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
422 foreach ($tabto as $key => $addrto) {
424 if (in_array($addrto[0], $listofemailstonotsendto)) {
432 $to = implode(
',', $tabto);
436 $tabcc = explode(
',', $addr_cc);
437 foreach ($tabcc as $key => $cc) {
439 if (in_array($cc[0], $listofemailstonotsendto)) {
447 $addr_cc = implode(
',', $tabcc);
451 $tabbcc = explode(
',', $addr_bcc);
452 foreach ($tabbcc as $key => $bcc) {
454 if (in_array($bcc[0], $listofemailstonotsendto)) {
455 unset($tabbcc[$key]);
462 $addr_bcc = implode(
',', $tabbcc);
466 if (empty($replyto)) {
474 $this->subject = $subject;
480 $this->deliveryreceipt = $deliveryreceipt;
483 $this->trackid = $trackid;
484 $this->in_reply_to = $in_reply_to;
485 $this->references = $references;
487 $this->filename_list = $filename_list;
488 $this->mimetype_list = $mimetype_list;
489 $this->mimefilename_list = $mimefilename_list;
490 $this->cid_list = $cid_list;
495 $this->addr_bcc =
'';
498 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
499 if (!empty($this->sendcontext)) {
500 $smtpContextKey = strtoupper($this->sendcontext);
502 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
503 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
507 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
511 if ($this->sendmode ==
'mail') {
522 if (!empty($moreinheader)) {
523 $smtp_headers .= $moreinheader;
529 if (!empty($this->html)) {
542 if (!empty($this->atleastonefile)) {
543 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
547 $this->headers = $smtp_headers.$mime_headers;
550 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
553 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
554 $this->message .= $text_body.$files_encoded;
555 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
556 } elseif ($this->sendmode ==
'smtps') {
559 $host = dol_getprefix(
'email');
561 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
562 $smtps =
new SMTPs();
563 $smtps->setCharSet(
$conf->file->character_set_client);
566 $subjecttouse = $this->subject;
571 $smtps->setSubject($subjecttouse);
576 $smtps->setTrackId($this->trackid);
578 if (!empty($this->in_reply_to)) {
579 $smtps->setInReplyTo($this->in_reply_to);
581 if (!empty($this->references)) {
582 $smtps->setReferences($this->references);
585 if (!empty($moreinheader)) {
586 $smtps->setMoreInHeader($moreinheader);
591 if (!empty($this->html)) {
604 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
606 if ($this->msgishtml) {
607 $smtps->setBodyContent($msg,
'html');
609 $smtps->setBodyContent($msg,
'plain');
612 if ($this->atleastoneimage) {
613 foreach ($this->images_encoded as $img) {
614 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
618 if (!empty($this->atleastonefile)) {
619 foreach ($filename_list as $i => $val) {
620 $content = file_get_contents($filename_list[$i]);
621 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
625 $smtps->setCC($this->addr_cc);
626 $smtps->setBCC($this->addr_bcc);
627 $smtps->setErrorsTo($this->errors_to);
628 $smtps->setDeliveryReceipt($this->deliveryreceipt);
630 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
633 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
635 $this->smtps = $smtps;
636 } elseif ($this->sendmode ==
'swiftmailer') {
639 $host = dol_getprefix(
'email');
641 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
644 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
646 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
650 $this->message =
new Swift_Message();
653 $headers = $this->message->getHeaders();
655 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
656 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
657 $headerID = $this->msgid;
658 $msgid = $headers->get(
'Message-ID');
659 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
660 $msgid->setId($headerID);
664 if (!empty($this->in_reply_to)) {
665 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
668 if (!empty($this->references)) {
669 $headers->addIdHeader(
'References', $this->references);
672 if (!empty($moreinheader)) {
673 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
674 foreach ($moreinheaderarray as $moreinheaderval) {
675 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
676 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
677 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
684 $this->message->setSubject($this->subject);
686 $this->errors[] = $e->getMessage();
691 if (!empty($this->addr_from)) {
695 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
696 $adressEmailFrom = array();
697 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
698 $adressEmailFrom = reset($adressEmailFrom);
699 if ($emailMatchs !==
false && filter_var(
$conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) &&
$conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
700 $this->message->setFrom(
$conf->global->MAIN_MAIL_SMTPS_ID);
708 $this->errors[] = $e->getMessage();
713 if (!empty($this->addr_to)) {
717 $this->errors[] = $e->getMessage();
721 if (!empty($this->reply_to)) {
725 $this->errors[] = $e->getMessage();
729 if (!empty($this->errors_to)) {
731 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
733 $this->errors[] = $e->getMessage();
738 $this->message->setCharSet(
$conf->file->character_set_client);
740 $this->errors[] = $e->getMessage();
743 if (!empty($this->html)) {
752 if ($this->atleastoneimage) {
753 foreach ($this->html_images as $img) {
755 $attachment = Swift_Image::fromPath($img[
'fullpath']);
757 $imgcid = $this->message->embed($attachment);
759 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
761 foreach ($this->images_encoded as $img) {
763 $attachment = Swift_Image::fromPath($img[
'fullpath']);
765 $imgcid = $this->message->embed($attachment);
767 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
771 if ($this->msgishtml) {
772 $this->message->setBody($msg,
'text/html');
774 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
776 $this->message->setBody($msg,
'text/plain');
778 $this->message->addPart(
dol_nl2br($msg),
'text/html');
781 if (!empty($this->atleastonefile)) {
782 foreach ($filename_list as $i => $val) {
784 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
785 if (!empty($mimefilename_list[$i])) {
786 $attachment->setFilename($mimefilename_list[$i]);
788 $this->message->attach($attachment);
792 if (!empty($this->addr_cc)) {
796 $this->errors[] = $e->getMessage();
799 if (!empty($this->addr_bcc)) {
803 $this->errors[] = $e->getMessage();
806 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
808 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
810 $this->errors[] = $e->getMessage();
816 $this->error =
'Bad value for sendmode';
829 global
$conf, $db, $langs, $hookmanager;
831 $errorlevel = error_reporting();
837 if (!is_object($hookmanager)) {
838 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
841 $hookmanager->initHooks(array(
'mail'));
843 $parameters = array();
845 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
847 $this->error =
"Error in hook maildao sendMail ".$reshook;
848 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
853 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
857 $sendingmode = $this->sendmode;
858 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
860 $listofmethods = array();
861 $listofmethods[
'mail'] =
'PHP mail function';
863 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
867 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
868 $linktoadminemailend =
'</a>';
869 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
870 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
871 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
872 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
874 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
875 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
878 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
884 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
886 $tmparray1 = explode(
',', $this->addr_to);
887 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
888 $this->error =
'Too much recipients in to:';
889 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
893 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
895 $tmparray2 = explode(
',', $this->addr_cc);
896 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
897 $this->error =
'Too much recipients in cc:';
898 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
902 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
904 $tmparray3 = explode(
',', $this->addr_bcc);
905 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
906 $this->error =
'Too much recipients in bcc:';
907 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
911 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
913 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
914 $this->error =
'Too much recipients in to:, cc:, bcc:';
915 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
919 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
920 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
921 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
922 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
923 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
924 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
925 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
926 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
927 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
928 if (!empty($this->sendcontext)) {
929 $smtpContextKey = strtoupper($this->sendcontext);
931 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
932 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
933 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
934 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
935 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
936 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
937 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
938 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
939 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
940 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
945 if ($this->sendmode ==
'mail') {
948 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
953 if (isset($_SERVER[
"WINDIR"])) {
954 if (empty($this->addr_from)) {
955 $this->addr_from =
'robot@example.com';
957 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
970 if ($res && !$this->subject) {
971 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
972 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
976 if ($res && !$dest) {
977 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
978 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
983 $additionnalparam =
'';
990 $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) :
''));
993 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
997 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1001 if (preg_match(
'/^win/i', PHP_OS)) {
1004 if (preg_match(
'/^mac/i', PHP_OS)) {
1008 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1010 $this->message = stripslashes($this->message);
1017 $subjecttouse = $this->subject;
1022 if (!empty($additionnalparam)) {
1023 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1025 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1029 $langs->load(
"errors");
1030 $this->error =
"Failed to send mail with php mail";
1032 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1034 $this->error .=
".<br>";
1035 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1036 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1042 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1046 if (isset($_SERVER[
"WINDIR"])) {
1047 @ini_restore(
'sendmail_from');
1052 ini_restore(
'SMTP');
1055 ini_restore(
'smtp_port');
1057 } elseif ($this->sendmode ==
'smtps') {
1058 if (!is_object($this->smtps)) {
1059 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1060 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1066 $this->smtps->setTransportType(0);
1069 if (empty(
$conf->global->$keyforsmtpserver)) {
1070 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1072 if (empty(
$conf->global->$keyforsmtpport)) {
1073 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1085 $server = ($secure ? $secure.
'://' :
'').$server;
1089 $this->smtps->setHost($server);
1090 $this->smtps->setPort($port);
1096 $this->smtps->setID($loginid);
1100 $this->smtps->setPW($loginpass);
1104 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1109 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1110 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1112 $keyforprovider =
'';
1114 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1115 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1117 if (!empty($supportedoauth2array)) {
1118 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1119 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1120 $OAUTH_SERVICENAME = $nameofservice;
1122 $OAUTH_SERVICENAME =
'Unknown';
1125 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1127 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1131 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1135 if (is_object($tokenobj)) {
1136 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1139 if (is_object($tokenobj) && $expire) {
1140 $credentials =
new Credentials(
1145 $serviceFactory = new \OAuth\ServiceFactory();
1146 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1148 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1151 $refreshtoken = $tokenobj->getRefreshToken();
1153 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1155 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1156 $tokenobj->setRefreshToken($refreshtoken);
1157 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1160 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1163 if (is_object($tokenobj)) {
1164 $this->smtps->setToken($tokenobj->getAccessToken());
1166 $this->error =
"Token not found";
1170 $this->error = $e->getMessage();
1171 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1176 $from = $this->smtps->getFrom(
'org');
1177 if ($res && !$from) {
1178 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1179 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1182 $dest = $this->smtps->getTo();
1183 if ($res && !$dest) {
1184 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1185 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1193 $this->smtps->setDebug(
true);
1196 $result = $this->smtps->sendMsg();
1204 $smtperrorcode = $this->smtps->lastretval;
1205 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1207 if ($smtperrorcode ==
'421') {
1222 $result = $this->smtps->getErrors();
1223 if (empty($this->error) && empty($result)) {
1224 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1227 if (empty($this->error)) {
1228 $this->error = $result;
1230 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1238 } elseif ($this->sendmode ==
'swiftmailer') {
1241 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1244 if (empty(
$conf->global->$keyforsmtpserver)) {
1245 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1247 if (empty(
$conf->global->$keyforsmtpport)) {
1248 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1261 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1270 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1275 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1276 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1278 $keyforprovider =
'';
1280 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1281 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1283 if (!empty($supportedoauth2array)) {
1284 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1285 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1286 $OAUTH_SERVICENAME = $nameofservice;
1288 $OAUTH_SERVICENAME =
'Unknown';
1291 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1293 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1298 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1302 if (is_object($tokenobj)) {
1303 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1306 if (is_object($tokenobj) && $expire) {
1307 $credentials =
new Credentials(
1312 $serviceFactory = new \OAuth\ServiceFactory();
1313 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1315 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1316 $refreshtoken = $tokenobj->getRefreshToken();
1318 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1321 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1322 $tokenobj->setRefreshToken($refreshtoken);
1323 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1325 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1329 if (is_object($tokenobj)) {
1330 $this->transport->setAuthMode(
'XOAUTH2');
1331 $this->transport->setPassword($tokenobj->getAccessToken());
1333 $this->errors[] =
"Token not found";
1337 $this->errors[] = $e->getMessage();
1338 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1342 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1347 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1348 $this->message->setEncoder($contentEncoderBase64);
1351 $this->mailer =
new Swift_Mailer($this->transport);
1358 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1359 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1364 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1367 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1373 $failedRecipients = array();
1375 $result = $this->mailer->send($this->message, $failedRecipients);
1377 $this->errors[] = $e->getMessage();
1384 if (!empty($this->error) || !empty($this->errors) || !$result) {
1385 if (!empty($failedRecipients)) {
1386 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1387 $this->errors[] = $this->error;
1389 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1396 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1402 $this->error =
'Bad value for sendmode';
1415 $parameters = array(
'sent' => $res);
1417 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1419 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1420 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1425 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1426 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1429 error_reporting($errorlevel);