229 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 =
'')
231 global
$conf, $dolibarr_main_data_root, $user;
233 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");
234 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
238 if (is_array($mimefilename_list)) {
239 foreach ($mimefilename_list as $key => $val) {
246 $this->sendcontext = $sendcontext;
249 $this->sendmode =
'';
250 if (!empty($this->sendcontext)) {
251 $smtpContextKey = strtoupper($this->sendcontext);
253 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
254 $this->sendmode = $smtpContextSendMode;
257 if (empty($this->sendmode)) {
267 $moreinheader .=
"Feedback-ID: ".$trackid.
':'.dol_getprefix(
'email').
":dolib\r\n";
273 $this->eol2 =
"\r\n";
277 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
281 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
284 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"),
'3');
287 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"),
'3');
289 if (empty($subject)) {
290 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
291 $this->error =
'ErrorSubjectIsRequired';
295 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
300 if ($msgishtml == -1) {
301 $this->msgishtml = 0;
303 $this->msgishtml = 1;
306 $this->msgishtml = $msgishtml;
309 global $dolibarr_main_url_root;
312 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
313 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
317 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
320 $this->msgishtml = 1;
322 dol_syslog(
"CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
325 if ($this->msgishtml) {
334 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
335 if ((
int)
$conf->entity !== 1) {
336 $dolibarr_main_data_root_images.=
'/'.$conf->entity.
'/';
338 $findimg = $this->
findHtmlImages($dolibarr_main_data_root_images.
'/medias');
340 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
341 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
350 if ($resultImageData < 0) {
351 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
352 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
355 $findimg += $resultImageData;
360 foreach ($this->html_images as $i => $val) {
361 if ($this->html_images[$i]) {
362 $this->atleastoneimage = 1;
363 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
364 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
366 $posindice = count($filename_list);
367 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
368 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
369 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
371 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
374 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
376 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
385 if (is_array($filename_list)) {
386 foreach ($filename_list as $i => $val) {
387 if ($filename_list[$i]) {
388 $this->atleastonefile = 1;
389 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);
398 foreach ($listofemailstoadd as $key => $val) {
399 $emailtoadd = $listofemailstoadd[$key];
400 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
401 if (!empty($user) && !empty($user->email)) {
402 $emailtoadd = $user->email;
407 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
411 $listofemailstoadd[$key] = $emailtoadd;
413 unset($listofemailstoadd[$key]);
416 if (!empty($listofemailstoadd)) {
417 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
424 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
428 $tabto = explode(
",", $to);
429 foreach ($tabto as $key => $addrto) {
431 if (in_array($addrto[0], $listofemailstonotsendto)) {
439 $to = implode(
',', $tabto);
443 $tabcc = explode(
',', $addr_cc);
444 foreach ($tabcc as $key => $cc) {
446 if (in_array($cc[0], $listofemailstonotsendto)) {
454 $addr_cc = implode(
',', $tabcc);
458 $tabbcc = explode(
',', $addr_bcc);
459 foreach ($tabbcc as $key => $bcc) {
461 if (in_array($bcc[0], $listofemailstonotsendto)) {
462 unset($tabbcc[$key]);
469 $addr_bcc = implode(
',', $tabbcc);
473 if (empty($replyto)) {
481 $this->subject = $subject;
487 $this->deliveryreceipt = $deliveryreceipt;
490 $this->trackid = $trackid;
491 $this->in_reply_to = $in_reply_to;
492 $this->references = $references;
494 $this->filename_list = $filename_list;
495 $this->mimetype_list = $mimetype_list;
496 $this->mimefilename_list = $mimefilename_list;
497 $this->cid_list = $cid_list;
502 $this->addr_bcc =
'';
505 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
506 if (!empty($this->sendcontext)) {
507 $smtpContextKey = strtoupper($this->sendcontext);
509 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
510 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
514 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
518 if ($this->sendmode ==
'mail') {
529 if (!empty($moreinheader)) {
530 $smtp_headers .= $moreinheader;
536 if (!empty($this->html)) {
549 if (!empty($this->atleastonefile)) {
550 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
554 $this->headers = $smtp_headers.$mime_headers;
557 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
560 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
561 $this->message .= $text_body.$files_encoded;
562 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
563 } elseif ($this->sendmode ==
'smtps') {
566 $host = dol_getprefix(
'email');
568 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
569 $smtps =
new SMTPs();
570 $smtps->setCharSet(
$conf->file->character_set_client);
573 $subjecttouse = $this->subject;
578 $smtps->setSubject($subjecttouse);
583 $smtps->setTrackId($this->trackid);
585 if (!empty($this->in_reply_to)) {
586 $smtps->setInReplyTo($this->in_reply_to);
588 if (!empty($this->references)) {
589 $smtps->setReferences($this->references);
592 if (!empty($moreinheader)) {
593 $smtps->setMoreInHeader($moreinheader);
598 if (!empty($this->html)) {
611 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
613 if ($this->msgishtml) {
614 $smtps->setBodyContent($msg,
'html');
616 $smtps->setBodyContent($msg,
'plain');
619 if ($this->atleastoneimage) {
620 foreach ($this->images_encoded as $img) {
621 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
625 if (!empty($this->atleastonefile)) {
626 foreach ($filename_list as $i => $val) {
627 $content = file_get_contents($filename_list[$i]);
628 if (empty($cid_list[$i])) {
629 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
634 $smtps->setCC($this->addr_cc);
635 $smtps->setBCC($this->addr_bcc);
636 $smtps->setErrorsTo($this->errors_to);
637 $smtps->setDeliveryReceipt($this->deliveryreceipt);
641 $options = array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true));
644 $options = array_merge($options, array(
'ssl' => array(
'capture_peer_cert' =>
true,
'capture_peer_cert_chain' =>
true)));
654 $options = array_merge($options, array(
'socket' => [
'bindto' =>
'0.0.0.0:0']));
656 if (!empty($options)) {
657 $smtps->setOptions($options);
660 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
662 $this->smtps = $smtps;
663 } elseif ($this->sendmode ==
'swiftmailer') {
666 $host = dol_getprefix(
'email');
668 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
671 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
673 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
677 $this->message =
new Swift_Message();
680 $headers = $this->message->getHeaders();
682 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
683 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
684 $headerID = $this->msgid;
685 $msgid = $headers->get(
'Message-ID');
686 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
687 $msgid->setId($headerID);
691 if (!empty($this->in_reply_to)) {
692 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
695 if (!empty($this->references)) {
696 $headers->addIdHeader(
'References', $this->references);
699 if (!empty($moreinheader)) {
700 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
701 foreach ($moreinheaderarray as $moreinheaderval) {
702 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
703 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
704 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
711 $this->message->setSubject($this->subject);
713 $this->errors[] = $e->getMessage();
718 if (!empty($this->addr_from)) {
722 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
723 $adressEmailFrom = array();
724 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
725 $adressEmailFrom = reset($adressEmailFrom);
726 if ($emailMatchs !==
false && filter_var(
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID'), FILTER_VALIDATE_EMAIL) &&
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID') !== $adressEmailFrom[0]) {
735 $this->errors[] = $e->getMessage();
740 if (!empty($this->addr_to)) {
744 $this->errors[] = $e->getMessage();
748 if (!empty($this->reply_to)) {
752 $this->errors[] = $e->getMessage();
756 if (!empty($this->errors_to)) {
758 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
760 $this->errors[] = $e->getMessage();
765 $this->message->setCharSet(
$conf->file->character_set_client);
767 $this->errors[] = $e->getMessage();
770 if (!empty($this->html)) {
779 if ($this->atleastoneimage) {
780 foreach ($this->html_images as $img) {
782 $attachment = Swift_Image::fromPath($img[
'fullpath']);
784 $imgcid = $this->message->embed($attachment);
786 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
788 foreach ($this->images_encoded as $img) {
790 $attachment = Swift_Image::fromPath($img[
'fullpath']);
792 $imgcid = $this->message->embed($attachment);
794 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
798 if ($this->msgishtml) {
799 $this->message->setBody($msg,
'text/html');
801 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
803 $this->message->setBody($msg,
'text/plain');
805 $this->message->addPart(
dol_nl2br($msg),
'text/html');
808 if (!empty($this->atleastonefile)) {
809 foreach ($filename_list as $i => $val) {
811 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
812 if (!empty($mimefilename_list[$i])) {
813 $attachment->setFilename($mimefilename_list[$i]);
815 $this->message->attach($attachment);
819 if (!empty($this->addr_cc)) {
823 $this->errors[] = $e->getMessage();
826 if (!empty($this->addr_bcc)) {
830 $this->errors[] = $e->getMessage();
833 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
835 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
837 $this->errors[] = $e->getMessage();
843 $this->error =
'Bad value for sendmode';
856 global
$conf, $db, $langs, $hookmanager;
858 $errorlevel = error_reporting();
864 if (!is_object($hookmanager)) {
865 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
868 $hookmanager->initHooks(array(
'mail'));
870 $parameters = array();
872 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
874 $this->error =
"Error in hook maildao sendMail ".$reshook;
875 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
880 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
884 $sendingmode = $this->sendmode;
885 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
887 $listofmethods = array();
888 $listofmethods[
'mail'] =
'PHP mail function';
890 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
894 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
895 $linktoadminemailend =
'</a>';
896 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
897 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
898 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
899 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
901 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
902 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
905 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
911 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
913 $tmparray1 = explode(
',', $this->addr_to);
914 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
915 $this->error =
'Too much recipients in to:';
916 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
920 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
922 $tmparray2 = explode(
',', $this->addr_cc);
923 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
924 $this->error =
'Too much recipients in cc:';
925 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
929 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
931 $tmparray3 = explode(
',', $this->addr_bcc);
932 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
933 $this->error =
'Too much recipients in bcc:';
934 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
938 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
940 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
941 $this->error =
'Too much recipients in to:, cc:, bcc:';
942 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
946 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
947 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
948 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
949 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
950 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
951 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
952 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
953 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
954 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
955 if (!empty($this->sendcontext)) {
956 $smtpContextKey = strtoupper($this->sendcontext);
958 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
959 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
960 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
961 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
962 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
963 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
964 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
965 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
966 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
967 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
972 if ($this->sendmode ==
'mail') {
975 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
980 if (isset($_SERVER[
"WINDIR"])) {
981 if (empty($this->addr_from)) {
982 $this->addr_from =
'robot@example.com';
984 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
997 if ($res && !$this->subject) {
998 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
999 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1003 if ($res && !$dest) {
1004 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
1005 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1010 $additionnalparam =
'';
1017 $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) :
''));
1020 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
1024 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1028 if (preg_match(
'/^win/i', PHP_OS)) {
1031 if (preg_match(
'/^mac/i', PHP_OS)) {
1035 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1037 $this->message = stripslashes($this->message);
1044 $subjecttouse = $this->subject;
1049 if (!empty($additionnalparam)) {
1050 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1052 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1056 $langs->load(
"errors");
1057 $this->error =
"Failed to send mail with php mail";
1059 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1061 $this->error .=
".<br>";
1062 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1063 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1069 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1073 if (isset($_SERVER[
"WINDIR"])) {
1074 @ini_restore(
'sendmail_from');
1079 ini_restore(
'SMTP');
1082 ini_restore(
'smtp_port');
1084 } elseif ($this->sendmode ==
'smtps') {
1085 if (!is_object($this->smtps)) {
1086 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1087 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1093 $this->smtps->setTransportType(0);
1096 if (empty(
$conf->global->$keyforsmtpserver)) {
1097 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1099 if (empty(
$conf->global->$keyforsmtpport)) {
1100 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1112 $server = ($secure ? $secure.
'://' :
'').$server;
1116 $this->smtps->setHost($server);
1117 $this->smtps->setPort($port);
1123 $this->smtps->setID($loginid);
1127 $this->smtps->setPW($loginpass);
1131 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1136 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1137 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1139 $keyforprovider =
'';
1141 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1142 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1144 if (!empty($supportedoauth2array)) {
1145 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1146 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1147 $OAUTH_SERVICENAME = $nameofservice;
1149 $OAUTH_SERVICENAME =
'Unknown';
1152 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1154 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1158 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1162 if (is_object($tokenobj)) {
1163 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1166 if (is_object($tokenobj) && $expire) {
1167 $credentials =
new Credentials(
1172 $serviceFactory = new \OAuth\ServiceFactory();
1173 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1175 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1178 $refreshtoken = $tokenobj->getRefreshToken();
1180 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1182 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1183 $tokenobj->setRefreshToken($refreshtoken);
1184 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1187 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1190 if (is_object($tokenobj)) {
1191 $this->smtps->setToken($tokenobj->getAccessToken());
1193 $this->error =
"Token not found";
1197 $this->error = $e->getMessage();
1198 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1203 $from = $this->smtps->getFrom(
'org');
1204 if ($res && !$from) {
1205 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1206 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1209 $dest = $this->smtps->getTo();
1210 if ($res && !$dest) {
1211 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1212 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1220 $this->smtps->setDebug(
true);
1223 $result = $this->smtps->sendMsg();
1231 $smtperrorcode = $this->smtps->lastretval;
1232 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1234 if ($smtperrorcode ==
'421') {
1249 $result = $this->smtps->getErrors();
1250 if (empty($this->error) && empty($result)) {
1251 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1254 if (empty($this->error)) {
1255 $this->error = $result;
1257 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1265 } elseif ($this->sendmode ==
'swiftmailer') {
1268 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1271 if (empty(
$conf->global->$keyforsmtpserver)) {
1272 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1274 if (empty(
$conf->global->$keyforsmtpport)) {
1275 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1288 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1297 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1302 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1303 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1305 $keyforprovider =
'';
1307 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1308 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1310 if (!empty($supportedoauth2array)) {
1311 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1312 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1313 $OAUTH_SERVICENAME = $nameofservice;
1315 $OAUTH_SERVICENAME =
'Unknown';
1318 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1320 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1325 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1329 if (is_object($tokenobj)) {
1330 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1333 if (is_object($tokenobj) && $expire) {
1334 $credentials =
new Credentials(
1339 $serviceFactory = new \OAuth\ServiceFactory();
1340 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1342 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1343 $refreshtoken = $tokenobj->getRefreshToken();
1345 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1348 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1349 $tokenobj->setRefreshToken($refreshtoken);
1350 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1352 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1356 if (is_object($tokenobj)) {
1357 $this->transport->setAuthMode(
'XOAUTH2');
1358 $this->transport->setPassword($tokenobj->getAccessToken());
1360 $this->errors[] =
"Token not found";
1364 $this->errors[] = $e->getMessage();
1365 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1369 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1374 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1375 $this->message->setEncoder($contentEncoderBase64);
1378 $this->mailer =
new Swift_Mailer($this->transport);
1385 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1386 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1391 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1394 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1400 $failedRecipients = array();
1402 $result = $this->mailer->send($this->message, $failedRecipients);
1404 $this->errors[] = $e->getMessage();
1411 if (!empty($this->error) || !empty($this->errors) || !$result) {
1412 if (!empty($failedRecipients)) {
1413 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1414 $this->errors[] = $this->error;
1416 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1423 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1429 $this->error =
'Bad value for sendmode';
1442 $parameters = array(
'sent' => $res);
1444 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1446 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1447 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1452 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1453 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1456 error_reporting($errorlevel);