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 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
334 if ((
int)
$conf->entity !== 1) {
335 $dolibarr_main_data_root_images.=
'/'.$conf->entity.
'/';
337 $findimg = $this->
findHtmlImages($dolibarr_main_data_root_images.
'/medias');
339 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
340 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
349 if ($resultImageData < 0) {
350 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
351 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
354 $findimg += $resultImageData;
359 foreach ($this->html_images as $i => $val) {
360 if ($this->html_images[$i]) {
361 $this->atleastoneimage = 1;
362 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
363 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
365 $posindice = count($filename_list);
366 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
367 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
368 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
370 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
373 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
375 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
384 if (is_array($filename_list)) {
385 foreach ($filename_list as $i => $val) {
386 if ($filename_list[$i]) {
387 $this->atleastonefile = 1;
388 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);
397 foreach ($listofemailstoadd as $key => $val) {
398 $emailtoadd = $listofemailstoadd[$key];
399 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
400 if (!empty($user) && !empty($user->email)) {
401 $emailtoadd = $user->email;
406 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
410 $listofemailstoadd[$key] = $emailtoadd;
412 unset($listofemailstoadd[$key]);
415 if (!empty($listofemailstoadd)) {
416 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
423 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
427 $tabto = explode(
",", $to);
428 foreach ($tabto as $key => $addrto) {
430 if (in_array($addrto[0], $listofemailstonotsendto)) {
438 $to = implode(
',', $tabto);
442 $tabcc = explode(
',', $addr_cc);
443 foreach ($tabcc as $key => $cc) {
445 if (in_array($cc[0], $listofemailstonotsendto)) {
453 $addr_cc = implode(
',', $tabcc);
457 $tabbcc = explode(
',', $addr_bcc);
458 foreach ($tabbcc as $key => $bcc) {
460 if (in_array($bcc[0], $listofemailstonotsendto)) {
461 unset($tabbcc[$key]);
468 $addr_bcc = implode(
',', $tabbcc);
472 if (empty($replyto)) {
480 $this->subject = $subject;
486 $this->deliveryreceipt = $deliveryreceipt;
489 $this->trackid = $trackid;
490 $this->in_reply_to = $in_reply_to;
491 $this->references = $references;
493 $this->filename_list = $filename_list;
494 $this->mimetype_list = $mimetype_list;
495 $this->mimefilename_list = $mimefilename_list;
496 $this->cid_list = $cid_list;
501 $this->addr_bcc =
'';
504 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
505 if (!empty($this->sendcontext)) {
506 $smtpContextKey = strtoupper($this->sendcontext);
508 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
509 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
513 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
517 if ($this->sendmode ==
'mail') {
528 if (!empty($moreinheader)) {
529 $smtp_headers .= $moreinheader;
535 if (!empty($this->html)) {
548 if (!empty($this->atleastonefile)) {
549 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
553 $this->headers = $smtp_headers.$mime_headers;
556 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
559 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
560 $this->message .= $text_body.$files_encoded;
561 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
562 } elseif ($this->sendmode ==
'smtps') {
565 $host = dol_getprefix(
'email');
567 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
568 $smtps =
new SMTPs();
569 $smtps->setCharSet(
$conf->file->character_set_client);
572 $subjecttouse = $this->subject;
577 $smtps->setSubject($subjecttouse);
582 $smtps->setTrackId($this->trackid);
584 if (!empty($this->in_reply_to)) {
585 $smtps->setInReplyTo($this->in_reply_to);
587 if (!empty($this->references)) {
588 $smtps->setReferences($this->references);
591 if (!empty($moreinheader)) {
592 $smtps->setMoreInHeader($moreinheader);
597 if (!empty($this->html)) {
610 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
612 if ($this->msgishtml) {
613 $smtps->setBodyContent($msg,
'html');
615 $smtps->setBodyContent($msg,
'plain');
618 if ($this->atleastoneimage) {
619 foreach ($this->images_encoded as $img) {
620 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
624 if (!empty($this->atleastonefile)) {
625 foreach ($filename_list as $i => $val) {
626 $content = file_get_contents($filename_list[$i]);
627 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
631 $smtps->setCC($this->addr_cc);
632 $smtps->setBCC($this->addr_bcc);
633 $smtps->setErrorsTo($this->errors_to);
634 $smtps->setDeliveryReceipt($this->deliveryreceipt);
638 $options = array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true));
641 $options = array_merge($options, array(
'ssl' => array(
'capture_peer_cert' =>
true,
'capture_peer_cert_chain' =>
true)));
651 $options = array_merge($options, array(
'socket' => [
'bindto' =>
'0.0.0.0:0']));
653 if (!empty($options)) {
654 $smtps->setOptions($options);
657 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
659 $this->smtps = $smtps;
660 } elseif ($this->sendmode ==
'swiftmailer') {
663 $host = dol_getprefix(
'email');
665 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
668 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
670 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
674 $this->message =
new Swift_Message();
677 $headers = $this->message->getHeaders();
679 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
680 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
681 $headerID = $this->msgid;
682 $msgid = $headers->get(
'Message-ID');
683 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
684 $msgid->setId($headerID);
688 if (!empty($this->in_reply_to)) {
689 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
692 if (!empty($this->references)) {
693 $headers->addIdHeader(
'References', $this->references);
696 if (!empty($moreinheader)) {
697 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
698 foreach ($moreinheaderarray as $moreinheaderval) {
699 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
700 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
701 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
708 $this->message->setSubject($this->subject);
710 $this->errors[] = $e->getMessage();
715 if (!empty($this->addr_from)) {
719 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
720 $adressEmailFrom = array();
721 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
722 $adressEmailFrom = reset($adressEmailFrom);
723 if ($emailMatchs !==
false && filter_var(
$conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) &&
$conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
724 $this->message->setFrom(
$conf->global->MAIN_MAIL_SMTPS_ID);
732 $this->errors[] = $e->getMessage();
737 if (!empty($this->addr_to)) {
741 $this->errors[] = $e->getMessage();
745 if (!empty($this->reply_to)) {
749 $this->errors[] = $e->getMessage();
753 if (!empty($this->errors_to)) {
755 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
757 $this->errors[] = $e->getMessage();
762 $this->message->setCharSet(
$conf->file->character_set_client);
764 $this->errors[] = $e->getMessage();
767 if (!empty($this->html)) {
776 if ($this->atleastoneimage) {
777 foreach ($this->html_images as $img) {
779 $attachment = Swift_Image::fromPath($img[
'fullpath']);
781 $imgcid = $this->message->embed($attachment);
783 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
785 foreach ($this->images_encoded as $img) {
787 $attachment = Swift_Image::fromPath($img[
'fullpath']);
789 $imgcid = $this->message->embed($attachment);
791 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
795 if ($this->msgishtml) {
796 $this->message->setBody($msg,
'text/html');
798 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
800 $this->message->setBody($msg,
'text/plain');
802 $this->message->addPart(
dol_nl2br($msg),
'text/html');
805 if (!empty($this->atleastonefile)) {
806 foreach ($filename_list as $i => $val) {
808 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
809 if (!empty($mimefilename_list[$i])) {
810 $attachment->setFilename($mimefilename_list[$i]);
812 $this->message->attach($attachment);
816 if (!empty($this->addr_cc)) {
820 $this->errors[] = $e->getMessage();
823 if (!empty($this->addr_bcc)) {
827 $this->errors[] = $e->getMessage();
830 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
832 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
834 $this->errors[] = $e->getMessage();
840 $this->error =
'Bad value for sendmode';
853 global
$conf, $db, $langs, $hookmanager;
855 $errorlevel = error_reporting();
861 if (!is_object($hookmanager)) {
862 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
865 $hookmanager->initHooks(array(
'mail'));
867 $parameters = array();
869 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
871 $this->error =
"Error in hook maildao sendMail ".$reshook;
872 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
877 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
881 $sendingmode = $this->sendmode;
882 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
884 $listofmethods = array();
885 $listofmethods[
'mail'] =
'PHP mail function';
887 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
891 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
892 $linktoadminemailend =
'</a>';
893 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
894 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
895 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
896 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
898 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
899 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
902 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
908 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
910 $tmparray1 = explode(
',', $this->addr_to);
911 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
912 $this->error =
'Too much recipients in to:';
913 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
917 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
919 $tmparray2 = explode(
',', $this->addr_cc);
920 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
921 $this->error =
'Too much recipients in cc:';
922 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
926 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
928 $tmparray3 = explode(
',', $this->addr_bcc);
929 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
930 $this->error =
'Too much recipients in bcc:';
931 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
935 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
937 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
938 $this->error =
'Too much recipients in to:, cc:, bcc:';
939 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
943 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
944 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
945 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
946 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
947 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
948 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
949 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
950 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
951 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
952 if (!empty($this->sendcontext)) {
953 $smtpContextKey = strtoupper($this->sendcontext);
955 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
956 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
957 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
958 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
959 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
960 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
961 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
962 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
963 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
964 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
969 if ($this->sendmode ==
'mail') {
972 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
977 if (isset($_SERVER[
"WINDIR"])) {
978 if (empty($this->addr_from)) {
979 $this->addr_from =
'robot@example.com';
981 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
994 if ($res && !$this->subject) {
995 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
996 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1000 if ($res && !$dest) {
1001 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
1002 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1007 $additionnalparam =
'';
1014 $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) :
''));
1017 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
1021 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1025 if (preg_match(
'/^win/i', PHP_OS)) {
1028 if (preg_match(
'/^mac/i', PHP_OS)) {
1032 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1034 $this->message = stripslashes($this->message);
1041 $subjecttouse = $this->subject;
1046 if (!empty($additionnalparam)) {
1047 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1049 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1053 $langs->load(
"errors");
1054 $this->error =
"Failed to send mail with php mail";
1056 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1058 $this->error .=
".<br>";
1059 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1060 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1066 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1070 if (isset($_SERVER[
"WINDIR"])) {
1071 @ini_restore(
'sendmail_from');
1076 ini_restore(
'SMTP');
1079 ini_restore(
'smtp_port');
1081 } elseif ($this->sendmode ==
'smtps') {
1082 if (!is_object($this->smtps)) {
1083 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1084 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1090 $this->smtps->setTransportType(0);
1093 if (empty(
$conf->global->$keyforsmtpserver)) {
1094 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1096 if (empty(
$conf->global->$keyforsmtpport)) {
1097 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1109 $server = ($secure ? $secure.
'://' :
'').$server;
1113 $this->smtps->setHost($server);
1114 $this->smtps->setPort($port);
1120 $this->smtps->setID($loginid);
1124 $this->smtps->setPW($loginpass);
1128 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1133 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1134 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1136 $keyforprovider =
'';
1138 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1139 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1141 if (!empty($supportedoauth2array)) {
1142 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1143 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1144 $OAUTH_SERVICENAME = $nameofservice;
1146 $OAUTH_SERVICENAME =
'Unknown';
1149 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1151 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1155 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1159 if (is_object($tokenobj)) {
1160 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1163 if (is_object($tokenobj) && $expire) {
1164 $credentials =
new Credentials(
1169 $serviceFactory = new \OAuth\ServiceFactory();
1170 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1172 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1175 $refreshtoken = $tokenobj->getRefreshToken();
1177 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1179 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1180 $tokenobj->setRefreshToken($refreshtoken);
1181 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1184 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1187 if (is_object($tokenobj)) {
1188 $this->smtps->setToken($tokenobj->getAccessToken());
1190 $this->error =
"Token not found";
1194 $this->error = $e->getMessage();
1195 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1200 $from = $this->smtps->getFrom(
'org');
1201 if ($res && !$from) {
1202 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1203 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1206 $dest = $this->smtps->getTo();
1207 if ($res && !$dest) {
1208 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1209 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1217 $this->smtps->setDebug(
true);
1220 $result = $this->smtps->sendMsg();
1228 $smtperrorcode = $this->smtps->lastretval;
1229 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1231 if ($smtperrorcode ==
'421') {
1246 $result = $this->smtps->getErrors();
1247 if (empty($this->error) && empty($result)) {
1248 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1251 if (empty($this->error)) {
1252 $this->error = $result;
1254 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1262 } elseif ($this->sendmode ==
'swiftmailer') {
1265 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1268 if (empty(
$conf->global->$keyforsmtpserver)) {
1269 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1271 if (empty(
$conf->global->$keyforsmtpport)) {
1272 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1285 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1294 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1299 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1300 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1302 $keyforprovider =
'';
1304 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1305 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1307 if (!empty($supportedoauth2array)) {
1308 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1309 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1310 $OAUTH_SERVICENAME = $nameofservice;
1312 $OAUTH_SERVICENAME =
'Unknown';
1315 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1317 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1322 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1326 if (is_object($tokenobj)) {
1327 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1330 if (is_object($tokenobj) && $expire) {
1331 $credentials =
new Credentials(
1336 $serviceFactory = new \OAuth\ServiceFactory();
1337 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1339 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1340 $refreshtoken = $tokenobj->getRefreshToken();
1342 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1345 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1346 $tokenobj->setRefreshToken($refreshtoken);
1347 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1349 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1353 if (is_object($tokenobj)) {
1354 $this->transport->setAuthMode(
'XOAUTH2');
1355 $this->transport->setPassword($tokenobj->getAccessToken());
1357 $this->errors[] =
"Token not found";
1361 $this->errors[] = $e->getMessage();
1362 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1366 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1371 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1372 $this->message->setEncoder($contentEncoderBase64);
1375 $this->mailer =
new Swift_Mailer($this->transport);
1382 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1383 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1388 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1391 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1397 $failedRecipients = array();
1399 $result = $this->mailer->send($this->message, $failedRecipients);
1401 $this->errors[] = $e->getMessage();
1408 if (!empty($this->error) || !empty($this->errors) || !$result) {
1409 if (!empty($failedRecipients)) {
1410 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1411 $this->errors[] = $this->error;
1413 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1420 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1426 $this->error =
'Bad value for sendmode';
1439 $parameters = array(
'sent' => $res);
1441 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1443 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1444 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1449 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1450 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1453 error_reporting($errorlevel);