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);
419 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
423 $tabto = explode(
",", $to);
424 foreach ($tabto as $key => $addrto) {
426 if (in_array($addrto[0], $listofemailstonotsendto)) {
434 $to = implode(
',', $tabto);
438 $tabcc = explode(
',', $addr_cc);
439 foreach ($tabcc as $key => $cc) {
441 if (in_array($cc[0], $listofemailstonotsendto)) {
449 $addr_cc = implode(
',', $tabcc);
453 $tabbcc = explode(
',', $addr_bcc);
454 foreach ($tabbcc as $key => $bcc) {
456 if (in_array($bcc[0], $listofemailstonotsendto)) {
457 unset($tabbcc[$key]);
464 $addr_bcc = implode(
',', $tabbcc);
468 if (empty($replyto)) {
476 $this->subject = $subject;
482 $this->deliveryreceipt = $deliveryreceipt;
485 $this->trackid = $trackid;
486 $this->in_reply_to = $in_reply_to;
487 $this->references = $references;
489 $this->filename_list = $filename_list;
490 $this->mimetype_list = $mimetype_list;
491 $this->mimefilename_list = $mimefilename_list;
492 $this->cid_list = $cid_list;
497 $this->addr_bcc =
'';
500 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
501 if (!empty($this->sendcontext)) {
502 $smtpContextKey = strtoupper($this->sendcontext);
504 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
505 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
509 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
513 if ($this->sendmode ==
'mail') {
524 if (!empty($moreinheader)) {
525 $smtp_headers .= $moreinheader;
531 if (!empty($this->html)) {
544 if (!empty($this->atleastonefile)) {
545 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
549 $this->headers = $smtp_headers.$mime_headers;
552 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
555 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
556 $this->message .= $text_body.$files_encoded;
557 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
558 } elseif ($this->sendmode ==
'smtps') {
561 $host = dol_getprefix(
'email');
563 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
564 $smtps =
new SMTPs();
565 $smtps->setCharSet(
$conf->file->character_set_client);
568 $subjecttouse = $this->subject;
573 $smtps->setSubject($subjecttouse);
578 $smtps->setTrackId($this->trackid);
580 if (!empty($this->in_reply_to)) {
581 $smtps->setInReplyTo($this->in_reply_to);
583 if (!empty($this->references)) {
584 $smtps->setReferences($this->references);
587 if (!empty($moreinheader)) {
588 $smtps->setMoreInHeader($moreinheader);
593 if (!empty($this->html)) {
606 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
608 if ($this->msgishtml) {
609 $smtps->setBodyContent($msg,
'html');
611 $smtps->setBodyContent($msg,
'plain');
614 if ($this->atleastoneimage) {
615 foreach ($this->images_encoded as $img) {
616 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
620 if (!empty($this->atleastonefile)) {
621 foreach ($filename_list as $i => $val) {
622 $content = file_get_contents($filename_list[$i]);
623 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
627 $smtps->setCC($this->addr_cc);
628 $smtps->setBCC($this->addr_bcc);
629 $smtps->setErrorsTo($this->errors_to);
630 $smtps->setDeliveryReceipt($this->deliveryreceipt);
634 $options = array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true));
637 $options = array_merge($options, array(
'ssl' => array(
'capture_peer_cert' =>
true,
'capture_peer_cert_chain' =>
true)));
647 $options = array_merge($options, array(
'socket' => [
'bindto' =>
'0.0.0.0:0']));
649 if (!empty($options)) {
650 $smtps->setOptions($options);
653 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
655 $this->smtps = $smtps;
656 } elseif ($this->sendmode ==
'swiftmailer') {
659 $host = dol_getprefix(
'email');
661 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
664 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
666 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
670 $this->message =
new Swift_Message();
673 $headers = $this->message->getHeaders();
675 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
676 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
677 $headerID = $this->msgid;
678 $msgid = $headers->get(
'Message-ID');
679 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
680 $msgid->setId($headerID);
684 if (!empty($this->in_reply_to)) {
685 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
688 if (!empty($this->references)) {
689 $headers->addIdHeader(
'References', $this->references);
692 if (!empty($moreinheader)) {
693 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
694 foreach ($moreinheaderarray as $moreinheaderval) {
695 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
696 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
697 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
704 $this->message->setSubject($this->subject);
706 $this->errors[] = $e->getMessage();
711 if (!empty($this->addr_from)) {
715 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
716 $adressEmailFrom = array();
717 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
718 $adressEmailFrom = reset($adressEmailFrom);
719 if ($emailMatchs !==
false && filter_var(
$conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) &&
$conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
720 $this->message->setFrom(
$conf->global->MAIN_MAIL_SMTPS_ID);
728 $this->errors[] = $e->getMessage();
733 if (!empty($this->addr_to)) {
737 $this->errors[] = $e->getMessage();
741 if (!empty($this->reply_to)) {
745 $this->errors[] = $e->getMessage();
749 if (!empty($this->errors_to)) {
751 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
753 $this->errors[] = $e->getMessage();
758 $this->message->setCharSet(
$conf->file->character_set_client);
760 $this->errors[] = $e->getMessage();
763 if (!empty($this->html)) {
772 if ($this->atleastoneimage) {
773 foreach ($this->html_images as $img) {
775 $attachment = Swift_Image::fromPath($img[
'fullpath']);
777 $imgcid = $this->message->embed($attachment);
779 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
781 foreach ($this->images_encoded as $img) {
783 $attachment = Swift_Image::fromPath($img[
'fullpath']);
785 $imgcid = $this->message->embed($attachment);
787 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
791 if ($this->msgishtml) {
792 $this->message->setBody($msg,
'text/html');
794 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
796 $this->message->setBody($msg,
'text/plain');
798 $this->message->addPart(
dol_nl2br($msg),
'text/html');
801 if (!empty($this->atleastonefile)) {
802 foreach ($filename_list as $i => $val) {
804 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
805 if (!empty($mimefilename_list[$i])) {
806 $attachment->setFilename($mimefilename_list[$i]);
808 $this->message->attach($attachment);
812 if (!empty($this->addr_cc)) {
816 $this->errors[] = $e->getMessage();
819 if (!empty($this->addr_bcc)) {
823 $this->errors[] = $e->getMessage();
826 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
828 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
830 $this->errors[] = $e->getMessage();
836 $this->error =
'Bad value for sendmode';
849 global
$conf, $db, $langs, $hookmanager;
851 $errorlevel = error_reporting();
857 if (!is_object($hookmanager)) {
858 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
861 $hookmanager->initHooks(array(
'mail'));
863 $parameters = array();
865 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
867 $this->error =
"Error in hook maildao sendMail ".$reshook;
868 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
873 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
877 $sendingmode = $this->sendmode;
878 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
880 $listofmethods = array();
881 $listofmethods[
'mail'] =
'PHP mail function';
883 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
887 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
888 $linktoadminemailend =
'</a>';
889 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
890 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
891 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
892 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
894 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
895 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
898 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
904 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
906 $tmparray1 = explode(
',', $this->addr_to);
907 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
908 $this->error =
'Too much recipients in to:';
909 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
913 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
915 $tmparray2 = explode(
',', $this->addr_cc);
916 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
917 $this->error =
'Too much recipients in cc:';
918 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
922 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
924 $tmparray3 = explode(
',', $this->addr_bcc);
925 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
926 $this->error =
'Too much recipients in bcc:';
927 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
931 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
933 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
934 $this->error =
'Too much recipients in to:, cc:, bcc:';
935 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
939 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
940 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
941 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
942 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
943 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
944 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
945 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
946 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
947 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
948 if (!empty($this->sendcontext)) {
949 $smtpContextKey = strtoupper($this->sendcontext);
951 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
952 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
953 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
954 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
955 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
956 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
957 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
958 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
959 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
960 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
965 if ($this->sendmode ==
'mail') {
968 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
973 if (isset($_SERVER[
"WINDIR"])) {
974 if (empty($this->addr_from)) {
975 $this->addr_from =
'robot@example.com';
977 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
990 if ($res && !$this->subject) {
991 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
992 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
996 if ($res && !$dest) {
997 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
998 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1003 $additionnalparam =
'';
1010 $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) :
''));
1013 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
1017 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1021 if (preg_match(
'/^win/i', PHP_OS)) {
1024 if (preg_match(
'/^mac/i', PHP_OS)) {
1028 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1030 $this->message = stripslashes($this->message);
1037 $subjecttouse = $this->subject;
1042 if (!empty($additionnalparam)) {
1043 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1045 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1049 $langs->load(
"errors");
1050 $this->error =
"Failed to send mail with php mail";
1052 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1054 $this->error .=
".<br>";
1055 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1056 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1062 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1066 if (isset($_SERVER[
"WINDIR"])) {
1067 @ini_restore(
'sendmail_from');
1072 ini_restore(
'SMTP');
1075 ini_restore(
'smtp_port');
1077 } elseif ($this->sendmode ==
'smtps') {
1078 if (!is_object($this->smtps)) {
1079 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1080 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1086 $this->smtps->setTransportType(0);
1089 if (empty(
$conf->global->$keyforsmtpserver)) {
1090 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1092 if (empty(
$conf->global->$keyforsmtpport)) {
1093 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1105 $server = ($secure ? $secure.
'://' :
'').$server;
1109 $this->smtps->setHost($server);
1110 $this->smtps->setPort($port);
1116 $this->smtps->setID($loginid);
1120 $this->smtps->setPW($loginpass);
1124 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1129 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1130 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1132 $keyforprovider =
'';
1134 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1135 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1137 if (!empty($supportedoauth2array)) {
1138 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1139 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1140 $OAUTH_SERVICENAME = $nameofservice;
1142 $OAUTH_SERVICENAME =
'Unknown';
1145 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1147 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1151 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1155 if (is_object($tokenobj)) {
1156 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1159 if (is_object($tokenobj) && $expire) {
1160 $credentials =
new Credentials(
1165 $serviceFactory = new \OAuth\ServiceFactory();
1166 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1168 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1171 $refreshtoken = $tokenobj->getRefreshToken();
1173 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1175 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1176 $tokenobj->setRefreshToken($refreshtoken);
1177 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1180 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1183 if (is_object($tokenobj)) {
1184 $this->smtps->setToken($tokenobj->getAccessToken());
1186 $this->error =
"Token not found";
1190 $this->error = $e->getMessage();
1191 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1196 $from = $this->smtps->getFrom(
'org');
1197 if ($res && !$from) {
1198 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1199 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1202 $dest = $this->smtps->getTo();
1203 if ($res && !$dest) {
1204 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1205 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1213 $this->smtps->setDebug(
true);
1216 $result = $this->smtps->sendMsg();
1224 $smtperrorcode = $this->smtps->lastretval;
1225 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1227 if ($smtperrorcode ==
'421') {
1242 $result = $this->smtps->getErrors();
1243 if (empty($this->error) && empty($result)) {
1244 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1247 if (empty($this->error)) {
1248 $this->error = $result;
1250 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1258 } elseif ($this->sendmode ==
'swiftmailer') {
1261 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1264 if (empty(
$conf->global->$keyforsmtpserver)) {
1265 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1267 if (empty(
$conf->global->$keyforsmtpport)) {
1268 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1281 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1290 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1295 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1296 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1298 $keyforprovider =
'';
1300 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1301 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1303 if (!empty($supportedoauth2array)) {
1304 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1305 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1306 $OAUTH_SERVICENAME = $nameofservice;
1308 $OAUTH_SERVICENAME =
'Unknown';
1311 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1313 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1318 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1322 if (is_object($tokenobj)) {
1323 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1326 if (is_object($tokenobj) && $expire) {
1327 $credentials =
new Credentials(
1332 $serviceFactory = new \OAuth\ServiceFactory();
1333 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1335 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1336 $refreshtoken = $tokenobj->getRefreshToken();
1338 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1341 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1342 $tokenobj->setRefreshToken($refreshtoken);
1343 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1345 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1349 if (is_object($tokenobj)) {
1350 $this->transport->setAuthMode(
'XOAUTH2');
1351 $this->transport->setPassword($tokenobj->getAccessToken());
1353 $this->errors[] =
"Token not found";
1357 $this->errors[] = $e->getMessage();
1358 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1362 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1367 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1368 $this->message->setEncoder($contentEncoderBase64);
1371 $this->mailer =
new Swift_Mailer($this->transport);
1378 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1379 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1384 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1387 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1393 $failedRecipients = array();
1395 $result = $this->mailer->send($this->message, $failedRecipients);
1397 $this->errors[] = $e->getMessage();
1404 if (!empty($this->error) || !empty($this->errors) || !$result) {
1405 if (!empty($failedRecipients)) {
1406 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1407 $this->errors[] = $this->error;
1409 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1416 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1422 $this->error =
'Bad value for sendmode';
1435 $parameters = array(
'sent' => $res);
1437 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1439 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1440 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1445 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1446 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1449 error_reporting($errorlevel);