230 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 =
'')
232 global
$conf, $dolibarr_main_data_root, $user;
234 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");
235 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
239 if (is_array($mimefilename_list)) {
240 foreach ($mimefilename_list as $key => $val) {
247 $this->sendcontext = $sendcontext;
250 $this->sendmode =
'';
251 if (!empty($this->sendcontext)) {
252 $smtpContextKey = strtoupper($this->sendcontext);
254 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
255 $this->sendmode = $smtpContextSendMode;
258 if (empty($this->sendmode)) {
268 $moreinheader .=
"Feedback-ID: ".$trackid.
':'.dol_getprefix(
'email').
":dolib\r\n";
274 $this->eol2 =
"\r\n";
278 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
282 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
285 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"),
'3');
288 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"),
'3');
290 if (empty($subject)) {
291 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
292 $this->error =
'ErrorSubjectIsRequired';
296 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
301 if ($msgishtml == -1) {
302 $this->msgishtml = 0;
304 $this->msgishtml = 1;
307 $this->msgishtml = $msgishtml;
314 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
318 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
321 $this->msgishtml = 1;
323 dol_syslog(
"CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
326 if ($this->msgishtml) {
335 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
336 if ((
int)
$conf->entity !== 1) {
337 $dolibarr_main_data_root_images.=
'/'.$conf->entity.
'/';
339 $findimg = $this->
findHtmlImages($dolibarr_main_data_root_images.
'/medias');
341 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
342 $this->error =
'ErrorInAddAttachmentsImageBaseOnMedia';
351 if ($resultImageData < 0) {
352 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData.
" upload_dir_tmp=".$upload_dir_tmp);
353 $this->error =
'ErrorInAddAttachmentsImageBaseIsSrcData';
356 $findimg += $resultImageData;
361 foreach ($this->html_images as $i => $val) {
362 if ($this->html_images[$i]) {
363 $this->atleastoneimage = 1;
364 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
365 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
367 $posindice = count($filename_list);
368 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
369 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
370 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
372 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
375 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
377 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
386 if (is_array($filename_list)) {
387 foreach ($filename_list as $i => $val) {
388 if ($filename_list[$i]) {
389 $this->atleastonefile = 1;
390 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);
399 foreach ($listofemailstoadd as $key => $val) {
400 $emailtoadd = $listofemailstoadd[$key];
401 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
402 if (!empty($user) && !empty($user->email)) {
403 $emailtoadd = $user->email;
408 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
412 $listofemailstoadd[$key] = $emailtoadd;
414 unset($listofemailstoadd[$key]);
417 if (!empty($listofemailstoadd)) {
418 $addr_bcc .= ($addr_bcc ?
', ' :
'').implode(
', ', $listofemailstoadd);
425 $listofemailstonotsendto = explode(
',',
getDolGlobalString(
'MAIN_MAIL_FORCE_NOT_SENDING_TO'));
429 $tabto = explode(
",", $to);
430 foreach ($tabto as $key => $addrto) {
432 if (in_array($addrto[0], $listofemailstonotsendto)) {
440 $to = implode(
',', $tabto);
444 $tabcc = explode(
',', $addr_cc);
445 foreach ($tabcc as $key => $cc) {
447 if (in_array($cc[0], $listofemailstonotsendto)) {
455 $addr_cc = implode(
',', $tabcc);
459 $tabbcc = explode(
',', $addr_bcc);
460 foreach ($tabbcc as $key => $bcc) {
462 if (in_array($bcc[0], $listofemailstonotsendto)) {
463 unset($tabbcc[$key]);
470 $addr_bcc = implode(
',', $tabbcc);
474 if (empty($replyto)) {
482 $this->subject = $subject;
488 $this->deliveryreceipt = $deliveryreceipt;
491 $this->trackid = $trackid;
492 $this->in_reply_to = $in_reply_to;
493 $this->references = $references;
495 $this->filename_list = $filename_list;
496 $this->mimetype_list = $mimetype_list;
497 $this->mimefilename_list = $mimefilename_list;
498 $this->cid_list = $cid_list;
503 $this->addr_bcc =
'';
506 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
507 if (!empty($this->sendcontext)) {
508 $smtpContextKey = strtoupper($this->sendcontext);
510 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
511 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
515 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
519 if ($this->sendmode ==
'mail') {
530 if (!empty($moreinheader)) {
531 $smtp_headers .= $moreinheader;
537 if (!empty($this->html)) {
550 if (!empty($this->atleastonefile) && $filename_list !==
null && $mimetype_list !==
null && $mimefilename_list !==
null) {
551 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
555 $this->headers = $smtp_headers.$mime_headers;
558 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
561 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
562 $this->message .= $text_body.$files_encoded;
563 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
564 } elseif ($this->sendmode ==
'smtps') {
567 $host = dol_getprefix(
'email');
569 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
570 $smtps =
new SMTPs();
571 $smtps->setCharSet(
$conf->file->character_set_client);
574 $subjecttouse = $this->subject;
579 $smtps->setSubject($subjecttouse);
584 $smtps->setTrackId($this->trackid);
586 if (!empty($this->in_reply_to)) {
587 $smtps->setInReplyTo($this->in_reply_to);
589 if (!empty($this->references)) {
590 $smtps->setReferences($this->references);
593 if (!empty($moreinheader)) {
594 $smtps->setMoreInHeader($moreinheader);
599 if (!empty($this->html)) {
612 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
614 if ($this->msgishtml) {
615 $smtps->setBodyContent($msg,
'html');
617 $smtps->setBodyContent($msg,
'plain');
620 if ($this->atleastoneimage) {
621 foreach ($this->images_encoded as $img) {
622 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
626 if (!empty($this->atleastonefile)) {
627 foreach ($filename_list as $i => $val) {
628 $content = file_get_contents($filename_list[$i]);
629 if (empty($cid_list[$i])) {
630 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ?
'' : $cid_list[$i]));
635 $smtps->setCC($this->addr_cc);
636 $smtps->setBCC($this->addr_bcc);
637 $smtps->setErrorsTo($this->errors_to);
638 $smtps->setDeliveryReceipt($this->deliveryreceipt);
642 $options = array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true));
645 $options = array_merge($options, array(
'ssl' => array(
'capture_peer_cert' =>
true,
'capture_peer_cert_chain' =>
true)));
655 $options = array_merge($options, array(
'socket' => [
'bindto' =>
'0.0.0.0:0']));
657 if (!empty($options)) {
658 $smtps->setOptions($options);
661 $this->msgid = time().
'.'.mt_rand(100, 999).
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
663 $smtps->setMessageID($this->msgid);
665 $this->smtps = $smtps;
666 } elseif ($this->sendmode ==
'swiftmailer') {
669 $host = dol_getprefix(
'email');
671 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
674 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
676 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
680 $this->message =
new Swift_Message();
683 $headers = $this->message->getHeaders();
685 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
686 $this->msgid = time().
'.'.mt_rand(100, 999).
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
687 $headerID = $this->msgid;
688 $msgid = $headers->get(
'Message-ID');
689 if ($msgid instanceof Swift_Mime_Headers_IdentificationHeader) {
690 $msgid->setId($headerID);
694 if (!empty($this->in_reply_to)) {
695 $headers->addIdHeader(
'In-Reply-To', $this->in_reply_to);
698 if (!empty($this->references)) {
699 $headers->addIdHeader(
'References', $this->references);
702 if (!empty($moreinheader)) {
703 $moreinheaderarray = preg_split(
'/[\r\n]+/', $moreinheader);
704 foreach ($moreinheaderarray as $moreinheaderval) {
705 $moreinheadervaltmp = explode(
':', $moreinheaderval, 2);
706 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
707 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
714 $this->message->setSubject($this->subject);
716 $this->errors[] = $e->getMessage();
721 if (!empty($this->addr_from)) {
725 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
726 $adressEmailFrom = array();
727 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
728 $adressEmailFrom = reset($adressEmailFrom);
729 if ($emailMatchs !==
false && filter_var(
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID'), FILTER_VALIDATE_EMAIL) &&
getDolGlobalString(
'MAIN_MAIL_SMTPS_ID') !== $adressEmailFrom[0]) {
738 $this->errors[] = $e->getMessage();
743 if (!empty($this->addr_to)) {
747 $this->errors[] = $e->getMessage();
751 if (!empty($this->reply_to)) {
755 $this->errors[] = $e->getMessage();
759 if (!empty($this->errors_to)) {
761 $headers->addMailboxHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
763 $this->errors[] = $e->getMessage();
768 $this->message->setCharSet(
$conf->file->character_set_client);
770 $this->errors[] = $e->getMessage();
773 if (!empty($this->html)) {
782 if ($this->atleastoneimage) {
783 foreach ($this->html_images as $img) {
785 $attachment = Swift_Image::fromPath($img[
'fullpath']);
787 $imgcid = $this->message->embed($attachment);
789 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
791 foreach ($this->images_encoded as $img) {
793 $attachment = Swift_Image::fromPath($img[
'fullpath']);
795 $imgcid = $this->message->embed($attachment);
797 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
801 if ($this->msgishtml) {
802 $this->message->setBody($msg,
'text/html');
804 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
806 $this->message->setBody($msg,
'text/plain');
808 $this->message->addPart(
dol_nl2br($msg),
'text/html');
811 if (!empty($this->atleastonefile)) {
812 foreach ($filename_list as $i => $val) {
814 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
815 if (!empty($mimefilename_list[$i])) {
816 $attachment->setFilename($mimefilename_list[$i]);
818 $this->message->attach($attachment);
822 if (!empty($this->addr_cc)) {
826 $this->errors[] = $e->getMessage();
829 if (!empty($this->addr_bcc)) {
833 $this->errors[] = $e->getMessage();
836 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
838 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
840 $this->errors[] = $e->getMessage();
846 $this->error =
'Bad value for sendmode';
859 global
$conf, $db, $langs, $hookmanager;
861 $errorlevel = error_reporting();
867 if (!is_object($hookmanager)) {
868 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
871 $hookmanager->initHooks(array(
'mail'));
873 $parameters = array();
875 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
877 $this->error =
"Error in hook maildao sendMail ".$reshook;
878 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
883 dol_syslog(
"A hook has replaced code to send email", LOG_DEBUG);
887 $sendingmode = $this->sendmode;
888 if ($this->sendcontext ==
'emailing' &&
getDolGlobalString(
'MAILING_NO_USING_PHPMAIL') && $sendingmode ==
'mail') {
890 $listofmethods = array();
891 $listofmethods[
'mail'] =
'PHP mail function';
893 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
897 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
898 $linktoadminemailend =
'</a>';
899 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
900 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
901 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
902 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
904 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
905 $this->errors[] = $langs->trans(
"MailSendSetupIs3",
getDolGlobalString(
'MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
908 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
914 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
916 $tmparray1 = explode(
',', $this->addr_to);
917 if (count($tmparray1) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
918 $this->error =
'Too much recipients in to:';
919 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
923 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
925 $tmparray2 = explode(
',', $this->addr_cc);
926 if (count($tmparray2) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
927 $this->error =
'Too much recipients in cc:';
928 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
932 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
934 $tmparray3 = explode(
',', $this->addr_bcc);
935 if (count($tmparray3) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
936 $this->error =
'Too much recipients in bcc:';
937 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
941 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
943 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) >
$conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
944 $this->error =
'Too much recipients in to:, cc:, bcc:';
945 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
949 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
950 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
951 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
952 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
953 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
954 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
955 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
956 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
957 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
958 if (!empty($this->sendcontext)) {
959 $smtpContextKey = strtoupper($this->sendcontext);
961 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
962 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
963 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
964 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
965 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
966 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
967 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
968 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
969 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
970 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
975 if ($this->sendmode ==
'mail') {
978 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_NOTICE);
983 if (isset($_SERVER[
"WINDIR"])) {
984 if (empty($this->addr_from)) {
985 $this->addr_from =
'robot@example.com';
987 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
1000 if ($res && !$this->subject) {
1001 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
1002 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1006 if ($res && !$dest) {
1007 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
1008 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1013 $additionnalparam =
'';
1020 $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) :
''));
1023 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
1027 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
1031 if (preg_match(
'/^win/i', PHP_OS)) {
1034 if (preg_match(
'/^mac/i', PHP_OS)) {
1038 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
1040 $this->message = stripslashes($this->message);
1047 $subjecttouse = $this->subject;
1052 if (!empty($additionnalparam)) {
1053 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
1055 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
1059 $langs->load(
"errors");
1060 $this->error =
"Failed to send mail with php mail";
1062 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
1064 $this->error .=
".<br>";
1065 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
1066 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1072 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1076 if (isset($_SERVER[
"WINDIR"])) {
1077 @ini_restore(
'sendmail_from');
1082 ini_restore(
'SMTP');
1085 ini_restore(
'smtp_port');
1087 } elseif ($this->sendmode ==
'smtps') {
1088 if (!is_object($this->smtps)) {
1089 $this->error =
"Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1090 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1096 $this->smtps->setTransportType(0);
1099 if (empty(
$conf->global->$keyforsmtpserver)) {
1100 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1102 if (empty(
$conf->global->$keyforsmtpport)) {
1103 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1115 $server = ($secure ? $secure.
'://' :
'').$server;
1119 $this->smtps->setHost($server);
1120 $this->smtps->setPort($port);
1126 $this->smtps->setID($loginid);
1130 $this->smtps->setPW($loginpass);
1134 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1139 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1140 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1142 $keyforprovider =
'';
1144 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1145 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1147 if (!empty($supportedoauth2array)) {
1148 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1149 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1150 $OAUTH_SERVICENAME = $nameofservice;
1152 $OAUTH_SERVICENAME =
'Unknown';
1155 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1157 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1161 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1165 if (is_object($tokenobj)) {
1166 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1169 if (is_object($tokenobj) && $expire) {
1170 $credentials =
new Credentials(
1175 $serviceFactory = new \OAuth\ServiceFactory();
1176 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1178 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1181 $refreshtoken = $tokenobj->getRefreshToken();
1183 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1185 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1186 $tokenobj->setRefreshToken($refreshtoken);
1187 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1190 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1193 if (is_object($tokenobj)) {
1194 $this->smtps->setToken($tokenobj->getAccessToken());
1196 $this->error =
"Token not found";
1200 $this->error = $e->getMessage();
1201 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1206 $from = $this->smtps->getFrom(
'org');
1207 if ($res && !$from) {
1208 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Sender address '$from' invalid";
1209 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1212 $dest = $this->smtps->getTo();
1213 if ($res && !$dest) {
1214 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - Recipient address '$dest' invalid";
1215 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1223 $this->smtps->setDebug(
true);
1226 $result = $this->smtps->sendMsg();
1234 $smtperrorcode = $this->smtps->lastretval;
1235 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1237 if ($smtperrorcode ==
'421') {
1251 dol_syslog(
"CMailFile::sendfile: mail SMTP sendMsg is success", LOG_DEBUG);
1254 $result = $this->smtps->getErrors();
1256 if (empty($this->error) && empty($result)) {
1257 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1260 if (empty($this->error)) {
1261 $this->error = $result;
1263 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=" .
getDolGlobalString($keyforsmtpport).
" - ".$this->error, LOG_ERR);
1267 $this->
save_dump_mail_in_err(
'Mail smtp error '.$smtperrorcode.
' with topic '.$this->subject.
' - '.$this->error);
1271 } elseif ($this->sendmode ==
'swiftmailer') {
1274 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1277 if (empty(
$conf->global->$keyforsmtpserver)) {
1278 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1280 if (empty(
$conf->global->$keyforsmtpport)) {
1281 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1294 $this->transport =
new Swift_SmtpTransport($server,
getDolGlobalInt($keyforsmtpport), $secure);
1303 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1308 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1309 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1311 $keyforprovider =
'';
1313 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1314 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1316 if (!empty($supportedoauth2array)) {
1317 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1318 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1319 $OAUTH_SERVICENAME = $nameofservice;
1321 $OAUTH_SERVICENAME =
'Unknown';
1324 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1326 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1331 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1335 if (is_object($tokenobj)) {
1336 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1339 if (is_object($tokenobj) && $expire) {
1340 $credentials =
new Credentials(
1345 $serviceFactory = new \OAuth\ServiceFactory();
1346 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1348 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1349 $refreshtoken = $tokenobj->getRefreshToken();
1351 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1354 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1355 $tokenobj->setRefreshToken($refreshtoken);
1356 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1358 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1362 if (is_object($tokenobj)) {
1363 $this->transport->setAuthMode(
'XOAUTH2');
1364 $this->transport->setPassword($tokenobj->getAccessToken());
1366 $this->errors[] =
"Token not found";
1370 $this->errors[] = $e->getMessage();
1371 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1375 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1380 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1381 $this->message->setEncoder($contentEncoderBase64);
1384 $this->mailer =
new Swift_Mailer($this->transport);
1391 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1392 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1397 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1400 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1406 $failedRecipients = array();
1408 $result = $this->mailer->send($this->message, $failedRecipients);
1410 $this->errors[] = $e->getMessage();
1417 if (!empty($this->error) || !empty($this->errors) || !$result) {
1418 if (!empty($failedRecipients)) {
1419 $this->error =
'Transport failed for the following addresses: "' . implode(
'", "', $failedRecipients) .
'".';
1420 $this->errors[] = $this->error;
1422 dol_syslog(
"CMailFile::sendfile: mail end error=". implode(
' ', $this->errors), LOG_ERR);
1429 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1435 $this->error =
'Bad value for sendmode';
1448 $parameters = array(
'sent' => $res);
1450 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1452 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1453 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1458 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1459 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1462 error_reporting($errorlevel);