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);
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;
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';
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) && $filename_list !==
null && $mimetype_list !==
null && $mimefilename_list !==
null) {
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)) {
590 $smtps->setReferences(preg_split(
'/[\s,]+/', trim($this->references), -1, PREG_SPLIT_NO_EMPTY));
593 if (!empty($moreinheader)) {
594 $smtps->setMoreInHeader($moreinheader);
599 if (!empty($this->html)) {
610 if ($this->msgishtml) {
611 $smtps->setBodyContent($msg,
'html');
613 $smtps->setBodyContent($msg,
'plain');
616 if (!empty($this->atleastoneimage)) {
617 foreach ($this->images_encoded as $img) {
618 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
622 if (!empty($this->atleastonefile)) {
623 foreach ($filename_list as $i => $val) {
624 $content = file_get_contents($filename_list[$i]);
625 if (empty($cid_list[$i])) {
626 $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 = uniqid(
'',
true).
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
659 $smtps->setMessageID($this->msgid);
661 $this->smtps = $smtps;
662 } elseif ($this->sendmode ==
'swiftmailer') {
665 $host = dol_getprefix(
'email');
667 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
670 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
672 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
676 $this->message =
new Swift_Message();
679 $headers = $this->message->getHeaders();
681 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
682 $this->msgid = uniqid(
'',
true).
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
683 $headerID = $this->msgid;
684 $msgid = $headers->get(
'Message-ID');
685 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
686 $msgid->setId($headerID);
691 if (!empty($this->in_reply_to)) {
692 $headers->addIdHeader(
'In-Reply-To', trim($this->in_reply_to,
" \t<>"));
695 if (!empty($this->references)) {
696 $references = array();
697 foreach (preg_split(
'/[\s,]+/', trim($this->references), -1, PREG_SPLIT_NO_EMPTY) as $reference) {
698 $references[] = trim($reference,
'<>');
700 $headers->addIdHeader(
'References', $references);
703 if (!empty($moreinheader)) {
704 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
705 foreach ($moreinheaderarray as $moreinheaderval) {
706 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
707 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
708 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
715 $this->message->setSubject($this->subject);
717 $this->errors[] = $e->getMessage();
722 if (!empty($this->addr_from)) {
726 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
727 $adressEmailFrom = array();
728 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
729 $adressEmailFrom = reset($adressEmailFrom);
730 if ($emailMatchs !==
false && filter_var(
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID'), FILTER_VALIDATE_EMAIL) &&
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID') !== $adressEmailFrom[0]) {
739 $this->errors[] = $e->getMessage();
744 if (!empty($this->addr_to)) {
748 $this->errors[] = $e->getMessage();
752 if (!empty($this->reply_to)) {
756 $this->errors[] = $e->getMessage();
760 if (!empty($this->errors_to)) {
762 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
764 $this->errors[] = $e->getMessage();
769 $this->message->setCharSet(
$conf->file->character_set_client);
771 $this->errors[] = $e->getMessage();
774 if (!empty($this->html)) {
783 if ($this->atleastoneimage) {
784 foreach ($this->html_images as $img) {
786 $attachment = Swift_Image::fromPath($img[
'fullpath']);
788 $imgcid = $this->message->embed($attachment);
790 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
792 foreach ($this->images_encoded as $img) {
794 $attachment = Swift_Image::fromPath($img[
'fullpath']);
796 $imgcid = $this->message->embed($attachment);
798 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
802 if ($this->msgishtml) {
803 $this->message->setBody($msg,
'text/html');
805 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
807 $this->message->setBody($msg,
'text/plain');
809 $this->message->addPart(
dol_nl2br($msg),
'text/html');
812 if (!empty($this->atleastonefile)) {
813 foreach ($filename_list as $i => $val) {
815 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
816 if (!empty($mimefilename_list[$i])) {
817 $attachment->setFilename($mimefilename_list[$i]);
819 $this->message->attach($attachment);
823 if (!empty($this->addr_cc)) {
827 $this->errors[] = $e->getMessage();
830 if (!empty($this->addr_bcc)) {
834 $this->errors[] = $e->getMessage();
837 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
839 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
841 $this->errors[] = $e->getMessage();
847 $this->error =
'Bad value for sendmode';
860 global
$conf, $db, $langs, $hookmanager;
862 $errorlevel = error_reporting();
868 if (!is_object($hookmanager)) {
869 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
872 $hookmanager->initHooks(array(
'mail'));
874 $parameters = array();
876 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
878 $this->error =
"Error in hook maildao sendMail ".$reshook;
879 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
884 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
888 $sendingmode = $this->sendmode;
889 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
891 $listofmethods = array();
892 $listofmethods[
'mail'] =
'PHP mail function';
894 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
898 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
899 $linktoadminemailend =
'</a>';
900 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
901 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
902 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
903 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
905 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
906 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
909 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
915 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
917 $tmparray1 = explode(
',', $this->addr_to);
918 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
919 $this->error =
'Too much recipients in to:';
920 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
924 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
926 $tmparray2 = explode(
',', $this->addr_cc);
927 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
928 $this->error =
'Too much recipients in cc:';
929 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
933 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
935 $tmparray3 = explode(
',', $this->addr_bcc);
936 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
937 $this->error =
'Too much recipients in bcc:';
938 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
942 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
944 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
945 $this->error =
'Too much recipients in to:, cc:, bcc:';
946 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
950 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
951 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
952 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
953 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
954 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
955 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
956 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
957 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
958 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
959 if (!empty($this->sendcontext)) {
960 $smtpContextKey = strtoupper($this->sendcontext);
962 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
963 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
964 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
965 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
966 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
967 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
968 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
969 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
970 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
971 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
976 if ($this->sendmode ==
'mail') {
979 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
984 if (isset($_SERVER[
"WINDIR"])) {
985 if (empty($this->addr_from)) {
986 $this->addr_from =
'robot@example.com';
988 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
1001 if ($res && !$this->subject) {
1002 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
1003 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1007 if ($res && !$dest) {
1008 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
1009 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1014 $additionnalparam =
'';
1021 $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) :
''));
1024 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
1028 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1032 if (preg_match(
'/^win/i', PHP_OS)) {
1035 if (preg_match(
'/^mac/i', PHP_OS)) {
1039 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1041 $this->message = stripslashes($this->message);
1048 $subjecttouse = $this->subject;
1053 if (!empty($additionnalparam)) {
1054 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1056 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1060 $langs->load(
"errors");
1061 $this->error =
"Failed to send mail with php mail";
1063 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1065 $this->error .=
".<br>";
1066 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1067 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1073 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1077 if (isset($_SERVER[
"WINDIR"])) {
1078 @ini_restore(
'sendmail_from');
1083 ini_restore(
'SMTP');
1086 ini_restore(
'smtp_port');
1088 } elseif ($this->sendmode ==
'smtps') {
1089 if (!is_object($this->smtps)) {
1090 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1091 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1097 $this->smtps->setTransportType(0);
1100 if (empty(
$conf->global->$keyforsmtpserver)) {
1101 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1103 if (empty(
$conf->global->$keyforsmtpport)) {
1104 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1116 $server = ($secure ? $secure.
'://' :
'').$server;
1122 $this->error = $tmpresult;
1123 $this->errors[] = $this->error;
1124 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
1128 $this->smtps->setHost($server);
1129 $this->smtps->setPort($port);
1135 $this->smtps->setID($loginid);
1139 $this->smtps->setPW($loginpass);
1145 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1150 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1151 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1153 $keyforprovider =
'';
1155 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1156 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1158 if (!empty($supportedoauth2array)) {
1159 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1160 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1161 $OAUTH_SERVICENAME = $nameofservice;
1163 $OAUTH_SERVICENAME =
'Unknown';
1166 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1168 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1172 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1176 if (is_object($tokenobj)) {
1178 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1181 if (is_object($tokenobj) && $expire) {
1182 $credentials =
new Credentials(
1187 $serviceFactory = new \OAuth\ServiceFactory();
1191 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
1192 $serviceFactory->setHttpClient($httpClient);
1193 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1196 $oauthScopes = array();
1198 if (!empty($oauthScopesStr)) {
1199 $oauthScopes = preg_split(
'/\s*,\s*/', $oauthScopesStr);
1200 if (!is_array($oauthScopes)) {
1201 $oauthScopes = array();
1206 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $oauthScopes);
1210 $refreshtoken = $tokenobj->getRefreshToken();
1212 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1214 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1215 if (empty($tokenobj->getRefreshToken())) {
1216 $tokenobj->setRefreshToken($refreshtoken);
1218 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1221 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1224 if (is_object($tokenobj)) {
1225 $this->smtps->setToken($tokenobj->getAccessToken());
1227 $this->error =
"OAuth2 token not found for service '".$OAUTH_SERVICENAME.
"' (setup constant ".$keyforsmtpoauthservice.
", send context '".$this->sendcontext.
"'). Compare it with the service column of llx_oauth_token.";
1228 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_ERR);
1232 $this->error = $e->getMessage();
1233 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1238 $from = $this->smtps->getFrom(
'org');
1239 if ($res && !$from) {
1240 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1241 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1244 $dest = $this->smtps->getTo();
1245 if ($res && !$dest) {
1246 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1247 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1255 $this->smtps->setDebug(
true);
1258 $result = $this->smtps->sendMsg();
1266 $smtperrorcode = $this->smtps->lastretval;
1267 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1269 if ($smtperrorcode ==
'421') {
1283 dol_syslog(
"CMailFile::sendfile: mail SMTP sendMsg is success", LOG_DEBUG);
1286 $result = $this->smtps->getErrors();
1288 if (empty($this->error) && empty($result)) {
1289 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1292 if (empty($this->error)) {
1293 $this->error = $result;
1295 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1299 $this->
save_dump_mail_in_err(
'Mail smtp error '.$smtperrorcode.
' with topic '.$this->subject.
' - '.$this->error);
1303 } elseif ($this->sendmode ==
'swiftmailer') {
1306 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1309 if (empty(
$conf->global->$keyforsmtpserver)) {
1310 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1312 if (empty(
$conf->global->$keyforsmtpport)) {
1313 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1328 $this->error = $tmpresult;
1329 $this->errors[] = $this->error;
1330 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
1334 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1343 $this->transport->setAuthMode(
'XOAUTH2');
1344 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1349 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1350 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1352 $keyforprovider =
'';
1354 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1355 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1357 if (!empty($supportedoauth2array)) {
1358 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1359 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1360 $OAUTH_SERVICENAME = $nameofservice;
1362 $OAUTH_SERVICENAME =
'Unknown';
1365 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1367 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1372 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1376 if (is_object($tokenobj)) {
1378 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1381 if (is_object($tokenobj) && $expire) {
1382 $credentials =
new Credentials(
1387 $serviceFactory = new \OAuth\ServiceFactory();
1391 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
1392 $serviceFactory->setHttpClient($httpClient);
1393 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1396 $oauthScopes = array();
1398 if (!empty($oauthScopesStr)) {
1399 $oauthScopes = preg_split(
'/\s*,\s*/', $oauthScopesStr);
1400 if (!is_array($oauthScopes)) {
1401 $oauthScopes = array();
1406 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $oauthScopes);
1407 $refreshtoken = $tokenobj->getRefreshToken();
1409 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1411 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1412 if (empty($tokenobj->getRefreshToken())) {
1413 $tokenobj->setRefreshToken($refreshtoken);
1415 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1417 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1421 if (is_object($tokenobj)) {
1422 $this->transport->setAuthMode(
'XOAUTH2');
1423 $this->transport->setPassword($tokenobj->getAccessToken());
1425 $this->errors[] =
"OAuth2 token not found for service '".$OAUTH_SERVICENAME.
"' (setup constant ".$keyforsmtpoauthservice.
", send context '".$this->sendcontext.
"'). Compare it with the service column of llx_oauth_token.";
1426 dol_syslog(
"CMailFile::sendfile: ".end($this->errors), LOG_ERR);
1430 $this->errors[] = $e->getMessage();
1431 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1435 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1440 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1441 $this->message->setEncoder($contentEncoderBase64);
1444 $this->mailer =
new Swift_Mailer($this->transport);
1451 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1452 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1457 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1460 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1466 $failedRecipients = array();
1470 $result = $this->mailer->send($this->message, $failedRecipients);
1472 $this->errors[] = $e->getMessage();
1479 if (!empty($this->error) || !empty($this->errors) || !$result) {
1480 if (!empty($failedRecipients)) {
1481 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1482 $this->errors[] = $this->error;
1484 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1491 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1497 $this->error =
'Bad value for sendmode';
1510 $parameters = array(
'sent' => $res);
1512 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1514 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1515 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1520 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1521 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1524 error_reporting($errorlevel);