176 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 =
'')
178 global $conf, $dolibarr_main_data_root, $user;
180 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", LOG_DEBUG);
181 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
185 if (is_array($mimefilename_list)) {
186 foreach ($mimefilename_list as $key => $val) {
193 $this->sendcontext = $sendcontext;
196 $this->sendmode =
'';
197 if (!empty($this->sendcontext)) {
198 $smtpContextKey = strtoupper($this->sendcontext);
200 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
201 $this->sendmode = $smtpContextSendMode;
204 if (empty($this->sendmode)) {
205 $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE :
'mail');
211 $this->eol2 =
"\r\n";
212 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
215 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
219 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
222 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
225 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
227 if (empty($subject)) {
228 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
229 $this->error =
'ErrorSubjectIsRequired';
233 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
238 if ($msgishtml == -1) {
239 $this->msgishtml = 0;
241 $this->msgishtml = 1;
244 $this->msgishtml = $msgishtml;
247 global $dolibarr_main_url_root;
250 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
251 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
255 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
257 if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
258 $this->msgishtml = 1;
262 if ($this->msgishtml) {
266 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
271 $findimg = $this->
findHtmlImages($dolibarr_main_data_root.
'/medias');
273 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImages");
274 $this->error =
'ErrorInAddAttachementsImageBaseOnMedia';
279 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA)) {
283 if ($resultImageData<0) {
284 dol_syslog(
"CMailFile::CMailfile: Error on findHtmlImagesInSrcData");
285 $this->error =
'ErrorInAddAttachementsImageBaseOnMedia';
288 $findimg += $resultImageData;
293 foreach ($this->html_images as $i => $val) {
294 if ($this->html_images[$i]) {
295 $this->atleastoneimage = 1;
296 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
297 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
299 $posindice = count($filename_list);
300 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
301 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
302 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
304 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
307 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
309 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
318 if (is_array($filename_list)) {
319 foreach ($filename_list as $i => $val) {
320 if ($filename_list[$i]) {
321 $this->atleastonefile = 1;
322 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]=".$cid_list[$i], LOG_DEBUG);
329 if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
330 $listofemailstoadd = explode(
',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
331 foreach ($listofemailstoadd as $key => $val) {
332 $emailtoadd = $listofemailstoadd[$key];
333 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
334 if (!empty($user) && !empty($user->email)) {
335 $emailtoadd = $user->email;
340 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
344 $listofemailstoadd[$key] = $emailtoadd;
346 unset($listofemailstoadd[$key]);
349 if (!empty($listofemailstoadd)) {
350 $addr_bcc .= ($addr_bcc ?
', ' :
'').join(
', ', $listofemailstoadd);
354 $this->subject = $subject;
360 $this->deliveryreceipt = $deliveryreceipt;
361 if (empty($replyto)) {
366 $this->trackid = $trackid;
368 $this->filename_list = $filename_list;
369 $this->mimetype_list = $mimetype_list;
370 $this->mimefilename_list = $mimefilename_list;
371 $this->cid_list = $cid_list;
373 if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
376 $this->addr_bcc =
'';
379 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
380 if (!empty($this->sendcontext)) {
381 $smtpContextKey = strtoupper($this->sendcontext);
383 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
384 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
388 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
392 if ($this->sendmode ==
'mail') {
403 if (!empty($moreinheader)) {
404 $smtp_headers .= $moreinheader;
410 if (!empty($this->html)) {
423 if (!empty($this->atleastonefile)) {
424 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
428 $this->headers = $smtp_headers.$mime_headers;
431 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
434 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
435 $this->message .= $text_body.$files_encoded;
436 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
437 } elseif ($this->sendmode ==
'smtps') {
441 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
442 $smtps =
new SMTPs();
443 $smtps->setCharSet($conf->file->character_set_client);
446 $subjecttouse = $this->subject;
451 $smtps->setSubject($subjecttouse);
454 $smtps->setTrackId($this->trackid);
457 if (!empty($moreinheader)) {
458 $smtps->setMoreInHeader($moreinheader);
461 if (!empty($this->html)) {
471 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
473 if ($this->msgishtml) {
474 $smtps->setBodyContent($msg,
'html');
476 $smtps->setBodyContent($msg,
'plain');
479 if ($this->atleastoneimage) {
480 foreach ($this->images_encoded as $img) {
481 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
485 if (!empty($this->atleastonefile)) {
486 foreach ($filename_list as $i => $val) {
487 $content = file_get_contents($filename_list[$i]);
488 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], $cid_list[$i]);
492 $smtps->setCC($this->addr_cc);
493 $smtps->setBCC($this->addr_bcc);
494 $smtps->setErrorsTo($this->errors_to);
495 $smtps->setDeliveryReceipt($this->deliveryreceipt);
496 if (!empty($conf->global->$keyforsslseflsigned)) {
497 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
500 $host = dol_getprefix(
'email');
501 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
503 $this->smtps = $smtps;
504 } elseif ($this->sendmode ==
'swiftmailer') {
506 $host = dol_getprefix(
'email');
508 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
511 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
513 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
517 $this->message =
new Swift_Message();
520 $headers = $this->message->getHeaders();
522 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
523 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
524 $headerID = $this->msgid;
525 $msgid = $headers->get(
'Message-ID');
526 $msgid->setId($headerID);
533 $this->message->setSubject($this->subject);
535 $this->errors[] = $e->getMessage();
540 if (!empty($this->addr_from)) {
542 if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
544 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
545 $adressEmailFrom = array();
546 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
547 $adressEmailFrom = reset($adressEmailFrom);
548 if ($emailMatchs !==
false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
549 $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
557 $this->errors[] = $e->getMessage();
562 if (!empty($this->addr_to)) {
566 $this->errors[] = $e->getMessage();
570 if (!empty($this->reply_to)) {
574 $this->errors[] = $e->getMessage();
578 if (!empty($this->errors_to)) {
580 $headers->addTextHeader(
'Errors-To', $this->
getValidAddress($this->errors_to, 0));
582 $this->errors[] = $e->getMessage();
587 $this->message->setCharSet($conf->file->character_set_client);
589 $this->errors[] = $e->getMessage();
592 if (!empty($this->html)) {
601 if ($this->atleastoneimage) {
602 foreach ($this->images_encoded as $img) {
604 $attachment = Swift_Image::fromPath($img[
'fullpath']);
606 $imgcid = $this->message->embed($attachment);
608 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
612 if ($this->msgishtml) {
613 $this->message->setBody($msg,
'text/html');
615 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
617 $this->message->setBody($msg,
'text/plain');
619 $this->message->addPart(
dol_nl2br($msg),
'text/html');
622 if (!empty($this->atleastonefile)) {
623 foreach ($filename_list as $i => $val) {
625 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
626 if (!empty($mimefilename_list[$i])) {
627 $attachment->setFilename($mimefilename_list[$i]);
629 $this->message->attach($attachment);
633 if (!empty($this->addr_cc)) {
637 $this->errors[] = $e->getMessage();
640 if (!empty($this->addr_bcc)) {
644 $this->errors[] = $e->getMessage();
647 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
649 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
651 $this->errors[] = $e->getMessage();
657 $this->error =
'Bad value for sendmode';
668 global $conf, $db, $langs, $hookmanager;
670 $errorlevel = error_reporting();
675 if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
676 if (!is_object($hookmanager)) {
677 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
680 $hookmanager->initHooks(array(
'mail'));
682 $parameters = array();
684 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
686 $this->error =
"Error in hook maildao sendMail ".$reshook;
687 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
695 $sendingmode = $this->sendmode;
696 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode ==
'mail') {
698 $listofmethods = array();
699 $listofmethods[
'mail'] =
'PHP mail function';
701 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
705 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
706 $linktoadminemailend =
'</a>';
707 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
708 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
709 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
710 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
711 if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
712 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
713 $this->errors[] = $langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
716 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
721 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
722 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
724 $tmparray1 = explode(
',', $this->addr_to);
725 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
726 $this->error =
'Too much recipients in to:';
727 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
730 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
731 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
733 $tmparray2 = explode(
',', $this->addr_cc);
734 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
735 $this->error =
'Too much recipients in cc:';
736 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
739 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
740 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
742 $tmparray3 = explode(
',', $this->addr_bcc);
743 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
744 $this->error =
'Too much recipients in bcc:';
745 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
748 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
749 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
751 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
752 $this->error =
'Too much recipients in to:, cc:, bcc:';
753 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
757 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
758 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
759 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
760 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
761 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
762 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
763 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
764 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
765 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
766 if (!empty($this->sendcontext)) {
767 $smtpContextKey = strtoupper($this->sendcontext);
769 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
770 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
771 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
772 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
773 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
774 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
775 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
776 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
777 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
778 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
783 if ($this->sendmode ==
'mail') {
786 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_DEBUG);
791 if (isset($_SERVER[
"WINDIR"])) {
792 if (empty($this->addr_from)) {
793 $this->addr_from =
'robot@example.com';
795 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
800 if (!empty($conf->global->$keyforsmtpserver)) {
801 ini_set(
'SMTP', $conf->global->$keyforsmtpserver);
803 if (!empty($conf->global->$keyforsmtpport)) {
804 ini_set(
'smtp_port', $conf->global->$keyforsmtpport);
808 if ($res && !$this->subject) {
809 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
810 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
814 if ($res && !$dest) {
815 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
816 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
821 $additionnalparam =
'';
822 if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
827 $additionnalparam .= ($additionnalparam ?
' ' :
'').(!empty($conf->global->MAIN_MAIL_ERRORS_TO) ?
'-f'.$this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO, 2) : ($this->addr_from !=
'' ?
'-f'.$this->getValidAddress($this->addr_from, 2) :
''));
829 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
830 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
833 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
834 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
838 if (preg_match(
'/^win/i', PHP_OS)) {
841 if (preg_match(
'/^mac/i', PHP_OS)) {
845 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
847 $this->message = stripslashes($this->message);
849 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
854 $subjecttouse = $this->subject;
859 if (!empty($additionnalparam)) {
860 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
862 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
866 $langs->load(
"errors");
867 $this->error =
"Failed to send mail with php mail";
869 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
871 $this->error .=
".<br>";
872 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
873 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
875 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
879 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
883 if (isset($_SERVER[
"WINDIR"])) {
884 @ini_restore(
'sendmail_from');
888 if (!empty($conf->global->$keyforsmtpserver)) {
891 if (!empty($conf->global->$keyforsmtpport)) {
892 ini_restore(
'smtp_port');
894 } elseif ($this->sendmode ==
'smtps') {
895 if (!is_object($this->smtps)) {
896 $this->error =
"Failed to send mail with smtps lib to HOST=".ini_get(
'SMTP').
", PORT=".$conf->global->$keyforsmtpport.
"<br>Constructor of object CMailFile was not initialized without errors.";
897 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
903 $this->smtps->setTransportType(0);
906 if (empty($conf->global->$keyforsmtpserver)) {
907 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
909 if (empty($conf->global->$keyforsmtpport)) {
910 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
914 $server = $conf->global->$keyforsmtpserver;
916 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
919 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
922 $server = ($secure ? $secure.
'://' :
'').$server;
924 $port = $conf->global->$keyforsmtpport;
926 $this->smtps->setHost($server);
927 $this->smtps->setPort($port);
931 if (!empty($conf->global->$keyforsmtpid)) {
932 $loginid = $conf->global->$keyforsmtpid;
933 $this->smtps->setID($loginid);
935 if (!empty($conf->global->$keyforsmtppw)) {
936 $loginpass = $conf->global->$keyforsmtppw;
937 $this->smtps->setPW($loginpass);
941 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
943 $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
944 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
945 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
947 $keyforprovider =
'';
949 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
950 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
952 if (isset($supportedoauth2array)) {
953 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
955 $OAUTH_SERVICENAME =
'Unknown';
958 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
960 $storage =
new DoliStorage($db, $conf, $keyforprovider);
962 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
965 if (is_object($tokenobj)) {
966 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
969 if (is_object($tokenobj) && $expire) {
970 $credentials =
new Credentials(
975 $serviceFactory = new \OAuth\ServiceFactory();
976 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
978 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
980 $refreshtoken = $tokenobj->getRefreshToken();
981 $tokenobj = $apiService->refreshAccessToken($tokenobj);
982 $tokenobj->setRefreshToken($refreshtoken);
983 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
986 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
987 if (is_object($tokenobj)) {
988 $this->smtps->setToken($tokenobj->getAccessToken());
990 $this->error =
"Token not found";
994 $this->error = $e->getMessage();
995 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1000 $from = $this->smtps->getFrom(
'org');
1001 if ($res && !$from) {
1002 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Sender address '$from' invalid";
1003 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1006 $dest = $this->smtps->getTo();
1007 if ($res && !$dest) {
1008 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Recipient address '$dest' invalid";
1009 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1014 dol_syslog(
"CMailFile::sendfile: sendMsg, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
1016 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1017 $this->smtps->setDebug(
true);
1020 $result = $this->smtps->sendMsg();
1022 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1028 $smtperrorcode = $this->smtps->lastretval;
1029 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1031 if ($smtperrorcode ==
'421') {
1046 $result = $this->smtps->getErrors();
1047 if (empty($this->error) && empty($result)) {
1048 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1051 if (empty($this->error)) {
1052 $this->error = $result;
1054 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - ".$this->error, LOG_ERR);
1057 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1062 } elseif ($this->sendmode ==
'swiftmailer') {
1065 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1068 if (empty($conf->global->$keyforsmtpserver)) {
1069 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1071 if (empty($conf->global->$keyforsmtpport)) {
1072 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1076 $server = $conf->global->$keyforsmtpserver;
1078 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1081 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1085 $this->transport =
new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
1087 if (!empty($conf->global->$keyforsmtpid)) {
1088 $this->transport->setUsername($conf->global->$keyforsmtpid);
1090 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1091 $this->transport->setPassword($conf->global->$keyforsmtppw);
1094 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1097 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1098 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1100 $keyforprovider =
'';
1102 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1103 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1105 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
1107 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1109 $storage =
new DoliStorage($db, $conf, $keyforprovider);
1112 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1115 if (is_object($tokenobj)) {
1116 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1119 if (is_object($tokenobj) && $expire) {
1120 $credentials =
new Credentials(
1125 $serviceFactory = new \OAuth\ServiceFactory();
1126 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1128 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1130 $refreshtoken = $tokenobj->getRefreshToken();
1131 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1132 $tokenobj->setRefreshToken($refreshtoken);
1133 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1135 if (is_object($tokenobj)) {
1136 $this->transport->setAuthMode(
'XOAUTH2');
1137 $this->transport->setPassword($tokenobj->getAccessToken());
1139 $this->errors[] =
"Token not found";
1143 $this->errors[] = $e->getMessage();
1144 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1147 if (!empty($conf->global->$keyforsslseflsigned)) {
1148 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1153 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1154 $this->message->setEncoder($contentEncoderBase64);
1157 $this->mailer =
new Swift_Mailer($this->transport);
1160 if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
1161 $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
1162 $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
1163 $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
1164 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1165 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1168 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1170 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1173 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1176 dol_syslog(
"CMailFile::sendfile: mailer->send, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
1179 $failedRecipients = array();
1181 $result = $this->mailer->send($this->message, $failedRecipients);
1183 $this->errors[] = $e->getMessage();
1185 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1190 if (!empty($this->error) || !empty($this->errors) || !$result) {
1191 if (!empty($failedRecipients)) {
1192 $this->error =
'Transport failed for the following addresses: "' . join(
'", "', $failedRecipients) .
'".';
1193 $this->errors[] = $this->error;
1195 dol_syslog(
"CMailFile::sendfile: mail end error=". join(
' ', $this->errors), LOG_ERR);
1198 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1202 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1208 return 'Bad value for sendmode';
1212 foreach ($this->html_images as $val) {
1213 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1218 $parameters = array(
'sent' => $res);
1220 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1222 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1223 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1228 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1229 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1232 error_reporting($errorlevel);