33 use OAuth\Common\Storage\DoliStorage;
34 use OAuth\Common\Consumer\Credentials;
62 public $mixed_boundary;
63 public $related_boundary;
64 public $alternative_boundary;
65 public $deliveryreceipt;
67 public $atleastonefile;
81 public $errors = array();
116 public $filename_list = array();
120 public $mimetype_list = array();
124 public $mimefilename_list = array();
128 public $cid_list = array();
132 public $image_boundary;
133 public $atleastoneimage = 0;
134 public $html_images = array();
135 public $images_encoded = array();
136 public $image_types = array(
137 'gif' =>
'image/gif',
138 'jpg' =>
'image/jpeg',
139 'jpeg' =>
'image/jpeg',
140 'jpe' =>
'image/jpeg',
141 'bmp' =>
'image/bmp',
142 'png' =>
'image/png',
143 'tif' =>
'image/tiff',
144 'tiff' =>
'image/tiff',
170 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 =
'')
172 global $conf, $dolibarr_main_data_root, $user;
174 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);
175 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
179 if (is_array($mimefilename_list)) {
180 foreach ($mimefilename_list as $key => $val) {
187 $this->sendcontext = $sendcontext;
190 $this->sendmode =
'';
191 if (!empty($this->sendcontext)) {
192 $smtpContextKey = strtoupper($this->sendcontext);
194 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
195 $this->sendmode = $smtpContextSendMode;
198 if (empty($this->sendmode)) {
199 $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE :
'mail');
205 $this->eol2 =
"\r\n";
206 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
209 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
213 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
216 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
219 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
221 if (empty($subject)) {
222 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
223 $this->error =
'ErrorSubjectIsRequired';
227 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
232 if ($msgishtml == -1) {
233 $this->msgishtml = 0;
235 $this->msgishtml = 1;
238 $this->msgishtml = $msgishtml;
241 global $dolibarr_main_url_root;
244 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
245 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
249 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
251 if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
252 $this->msgishtml = 1;
256 if ($this->msgishtml) {
260 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
265 $findimg = $this->
findHtmlImages($dolibarr_main_data_root.
'/medias');
268 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA)) {
276 foreach ($this->html_images as $i => $val) {
277 if ($this->html_images[$i]) {
278 $this->atleastoneimage = 1;
279 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
280 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
282 $posindice = count($filename_list);
283 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
284 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
285 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
287 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
290 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
292 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
301 if (is_array($filename_list)) {
302 foreach ($filename_list as $i => $val) {
303 if ($filename_list[$i]) {
304 $this->atleastonefile = 1;
305 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);
312 if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
313 $listofemailstoadd = explode(
',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
314 foreach ($listofemailstoadd as $key => $val) {
315 $emailtoadd = $listofemailstoadd[$key];
316 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
317 if (!empty($user) && !empty($user->email)) {
318 $emailtoadd = $user->email;
323 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
327 $listofemailstoadd[$key] = $emailtoadd;
329 unset($listofemailstoadd[$key]);
332 if (!empty($listofemailstoadd)) {
333 $addr_bcc .= ($addr_bcc ?
', ' :
'').join(
', ', $listofemailstoadd);
337 $this->subject = $subject;
343 $this->deliveryreceipt = $deliveryreceipt;
344 if (empty($replyto)) {
349 $this->trackid = $trackid;
351 $this->filename_list = $filename_list;
352 $this->mimetype_list = $mimetype_list;
353 $this->mimefilename_list = $mimefilename_list;
354 $this->cid_list = $cid_list;
356 if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
359 $this->addr_bcc =
'';
362 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
363 if (!empty($this->sendcontext)) {
364 $smtpContextKey = strtoupper($this->sendcontext);
366 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
367 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
371 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
375 if ($this->sendmode ==
'mail') {
386 if (!empty($moreinheader)) {
387 $smtp_headers .= $moreinheader;
393 if (!empty($this->html)) {
406 if (!empty($this->atleastonefile)) {
407 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
411 $this->headers = $smtp_headers.$mime_headers;
414 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
417 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
418 $this->message .= $text_body.$files_encoded;
419 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
420 } elseif ($this->sendmode ==
'smtps') {
424 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
425 $smtps =
new SMTPs();
426 $smtps->setCharSet($conf->file->character_set_client);
429 $subjecttouse = $this->subject;
434 $smtps->setSubject($subjecttouse);
437 $smtps->setTrackId($this->trackid);
440 if (!empty($moreinheader)) {
441 $smtps->setMoreInHeader($moreinheader);
444 if (!empty($this->html)) {
454 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
456 if ($this->msgishtml) {
457 $smtps->setBodyContent($msg,
'html');
459 $smtps->setBodyContent($msg,
'plain');
462 if ($this->atleastoneimage) {
463 foreach ($this->images_encoded as $img) {
464 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
468 if (!empty($this->atleastonefile)) {
469 foreach ($filename_list as $i => $val) {
470 $content = file_get_contents($filename_list[$i]);
471 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], $cid_list[$i]);
475 $smtps->setCC($this->addr_cc);
476 $smtps->setBCC($this->addr_bcc);
477 $smtps->setErrorsTo($this->errors_to);
478 $smtps->setDeliveryReceipt($this->deliveryreceipt);
479 if (!empty($conf->global->$keyforsslseflsigned)) {
480 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
483 $host = dol_getprefix(
'email');
484 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
486 $this->smtps = $smtps;
487 } elseif ($this->sendmode ==
'swiftmailer') {
489 $host = dol_getprefix(
'email');
491 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
494 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
496 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
500 $this->message =
new Swift_Message();
503 $headers = $this->message->getHeaders();
504 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
505 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
506 $headerID = $this->msgid;
507 $msgid = $headers->get(
'Message-ID');
508 $msgid->setId($headerID);
509 $headers->addIdHeader(
'References', $headerID);
514 $result = $this->message->setSubject($this->subject);
516 $this->errors[] = $e->getMessage();
521 if (!empty($this->addr_from)) {
523 if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
525 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
526 $adressEmailFrom = array();
527 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
528 $adressEmailFrom = reset($adressEmailFrom);
529 if ($emailMatchs !==
false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
530 $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
538 $this->errors[] = $e->getMessage();
543 if (!empty($this->addr_to)) {
547 $this->errors[] = $e->getMessage();
551 if (!empty($this->reply_to)) {
555 $this->errors[] = $e->getMessage();
560 $this->message->setCharSet($conf->file->character_set_client);
562 $this->errors[] = $e->getMessage();
565 if (!empty($this->html)) {
574 if ($this->atleastoneimage) {
575 foreach ($this->images_encoded as $img) {
577 $attachment = Swift_Image::fromPath($img[
'fullpath']);
579 $imgcid = $this->message->embed($attachment);
581 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
585 if ($this->msgishtml) {
586 $this->message->setBody($msg,
'text/html');
588 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
590 $this->message->setBody($msg,
'text/plain');
592 $this->message->addPart(
dol_nl2br($msg),
'text/html');
595 if (!empty($this->atleastonefile)) {
596 foreach ($filename_list as $i => $val) {
598 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
599 if (!empty($mimefilename_list[$i])) {
600 $attachment->setFilename($mimefilename_list[$i]);
602 $this->message->attach($attachment);
606 if (!empty($this->addr_cc)) {
610 $this->errors[] = $e->getMessage();
613 if (!empty($this->addr_bcc)) {
617 $this->errors[] = $e->getMessage();
621 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
623 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
625 $this->errors[] = $e->getMessage();
631 $this->error =
'Bad value for sendmode';
643 global $conf, $db, $langs;
645 $errorlevel = error_reporting();
650 if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
651 require_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
653 $hookmanager->initHooks(array(
'mail'));
655 $parameters = array();
657 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
659 $this->error =
"Error in hook maildao sendMail ".$reshook;
660 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
668 $sendingmode = $this->sendmode;
669 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode ==
'mail') {
671 $listofmethods = array();
672 $listofmethods[
'mail'] =
'PHP mail function';
674 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
678 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
679 $linktoadminemailend =
'</a>';
680 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
681 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
682 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
683 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
684 if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
685 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
686 $this->errors[] = $langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
692 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
693 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
695 $tmparray1 = explode(
',', $this->addr_to);
696 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
697 $this->error =
'Too much recipients in to:';
698 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
701 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
702 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
704 $tmparray2 = explode(
',', $this->addr_cc);
705 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
706 $this->error =
'Too much recipients in cc:';
707 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
710 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
711 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
713 $tmparray3 = explode(
',', $this->addr_bcc);
714 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
715 $this->error =
'Too much recipients in bcc:';
716 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
719 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
720 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
722 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
723 $this->error =
'Too much recipients in to:, cc:, bcc:';
724 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
728 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
729 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
730 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
731 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
732 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
733 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
734 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
735 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
736 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
737 if (!empty($this->sendcontext)) {
738 $smtpContextKey = strtoupper($this->sendcontext);
740 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
741 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
742 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
743 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
744 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
745 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
746 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
747 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
748 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
749 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
754 if ($this->sendmode ==
'mail') {
757 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_DEBUG);
758 dol_syslog(
"CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
762 if (isset($_SERVER[
"WINDIR"])) {
763 if (empty($this->addr_from)) {
764 $this->addr_from =
'robot@example.com';
766 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
771 if (!empty($conf->global->$keyforsmtpserver)) {
772 ini_set(
'SMTP', $conf->global->$keyforsmtpserver);
774 if (!empty($conf->global->$keyforsmtpport)) {
775 ini_set(
'smtp_port', $conf->global->$keyforsmtpport);
779 if ($res && !$this->subject) {
780 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
781 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
785 if ($res && !$dest) {
786 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
787 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
792 $additionnalparam =
'';
793 if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
798 $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) :
''));
800 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
801 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
804 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
805 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
809 if (preg_match(
'/^win/i', PHP_OS)) {
812 if (preg_match(
'/^mac/i', PHP_OS)) {
816 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
818 $this->message = stripslashes($this->message);
820 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
825 $subjecttouse = $this->subject;
830 if (!empty($additionnalparam)) {
831 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
833 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
837 $langs->load(
"errors");
838 $this->error =
"Failed to send mail with php mail";
840 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
842 $this->error .=
".<br>";
843 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
844 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
846 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
850 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
854 if (isset($_SERVER[
"WINDIR"])) {
855 @ini_restore(
'sendmail_from');
859 if (!empty($conf->global->$keyforsmtpserver)) {
862 if (!empty($conf->global->$keyforsmtpport)) {
863 ini_restore(
'smtp_port');
865 } elseif ($this->sendmode ==
'smtps') {
866 if (!is_object($this->smtps)) {
867 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
"<br>Constructor of object CMailFile was not initialized without errors.";
868 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
874 $this->smtps->setTransportType(0);
877 if (empty($conf->global->$keyforsmtpserver)) {
878 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
880 if (empty($conf->global->$keyforsmtpport)) {
881 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
885 $server = $conf->global->$keyforsmtpserver;
887 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
890 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
893 $server = ($secure ? $secure.
'://' :
'').$server;
895 $port = $conf->global->$keyforsmtpport;
897 $this->smtps->setHost($server);
898 $this->smtps->setPort($port);
902 if (!empty($conf->global->$keyforsmtpid)) {
903 $loginid = $conf->global->$keyforsmtpid;
904 $this->smtps->setID($loginid);
906 if (!empty($conf->global->$keyforsmtppw)) {
907 $loginpass = $conf->global->$keyforsmtppw;
908 $this->smtps->setPW($loginpass);
912 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
914 $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
915 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
916 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
918 $keyforprovider =
'';
920 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
921 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
923 if (isset($supportedoauth2array)) {
924 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
926 $OAUTH_SERVICENAME =
'Unknown';
929 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
931 $storage =
new DoliStorage($db, $conf, $keyforprovider);
933 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
936 if (is_object($tokenobj)) {
937 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
940 if (is_object($tokenobj) && $expire) {
941 $credentials =
new Credentials(
946 $serviceFactory = new \OAuth\ServiceFactory();
947 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
949 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
951 $refreshtoken = $tokenobj->getRefreshToken();
952 $tokenobj = $apiService->refreshAccessToken($tokenobj);
953 $tokenobj->setRefreshToken($refreshtoken);
954 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
957 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
958 if (is_object($tokenobj)) {
959 $this->smtps->setToken($tokenobj->getAccessToken());
961 $this->error =
"Token not found";
965 $this->error = $e->getMessage();
966 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
971 $from = $this->smtps->getFrom(
'org');
972 if ($res && !$from) {
973 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Sender address '$from' invalid";
974 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
977 $dest = $this->smtps->getTo();
978 if ($res && !$dest) {
979 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Recipient address '$dest' invalid";
980 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
985 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
986 $this->smtps->setDebug(
true);
989 $result = $this->smtps->sendMsg();
991 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
996 $smtperrorcode = $this->smtps->lastretval;
997 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
999 if ($smtperrorcode ==
'421') {
1014 $result = $this->smtps->getErrors();
1015 if (empty($this->error) && empty($result)) {
1016 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1019 if (empty($this->error)) {
1020 $this->error = $result;
1022 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - ".$this->error, LOG_ERR);
1025 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1030 } elseif ($this->sendmode ==
'swiftmailer') {
1033 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1036 if (empty($conf->global->$keyforsmtpserver)) {
1037 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1039 if (empty($conf->global->$keyforsmtpport)) {
1040 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1044 $server = $conf->global->$keyforsmtpserver;
1046 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1049 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1053 $this->transport =
new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
1055 if (!empty($conf->global->$keyforsmtpid)) {
1056 $this->transport->setUsername($conf->global->$keyforsmtpid);
1058 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1059 $this->transport->setPassword($conf->global->$keyforsmtppw);
1062 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1065 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1066 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1068 $keyforprovider =
'';
1070 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1071 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1073 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
1075 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1077 $storage =
new DoliStorage($db, $conf, $keyforprovider);
1080 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1083 if (is_object($tokenobj)) {
1084 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1087 if (is_object($tokenobj) && $expire) {
1088 $credentials =
new Credentials(
1093 $serviceFactory = new \OAuth\ServiceFactory();
1094 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1096 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1098 $refreshtoken = $tokenobj->getRefreshToken();
1099 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1100 $tokenobj->setRefreshToken($refreshtoken);
1101 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1103 if (is_object($tokenobj)) {
1104 $this->transport->setAuthMode(
'XOAUTH2');
1105 $this->transport->setPassword($tokenobj->getAccessToken());
1107 $this->errors[] =
"Token not found";
1111 $this->errors[] = $e->getMessage();
1112 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1115 if (!empty($conf->global->$keyforsslseflsigned)) {
1116 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1121 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1122 $this->message->setEncoder($contentEncoderBase64);
1125 $this->mailer =
new Swift_Mailer($this->transport);
1128 if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
1129 $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
1130 $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
1131 $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
1132 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1133 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1136 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1138 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1141 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1144 $failedRecipients = array();
1146 $result = $this->mailer->send($this->message, $failedRecipients);
1148 $this->errors[] = $e->getMessage();
1150 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1155 if (!empty($this->error) || !empty($this->errors) || !$result) {
1156 if (!empty($failedRecipients)) {
1157 $this->errors[] =
'Transport failed for the following addresses: "' . join(
'", "', $failedRecipients) .
'".';
1159 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1162 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1166 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1172 return 'Bad value for sendmode';
1176 foreach ($this->html_images as $val) {
1177 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1182 $parameters = array(
'sent' => $res);
1184 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1186 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1187 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1192 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1193 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1196 error_reporting($errorlevel);
1210 return '=?'.$conf->file->character_set_client.
'?B?'.base64_encode($stringtoencode).
'?=';
1225 if (is_readable($newsourcefile)) {
1226 $contents = file_get_contents($newsourcefile);
1227 $encoded = chunk_split(base64_encode($contents), 76, $this->eol);
1230 $this->error =
"Error in _encode_file() method: Can't read file '".$sourcefile.
"'";
1231 dol_syslog(
"CMailFile::_encode_file: ".$this->error, LOG_ERR);
1248 global $conf, $dolibarr_main_data_root;
1250 if (@is_writeable($dolibarr_main_data_root)) {
1251 $outputfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1252 $fp = fopen($outputfile,
"w");
1254 if ($this->sendmode ==
'mail') {
1255 fputs($fp, $this->headers);
1256 fputs($fp, $this->eol);
1257 fputs($fp, $this->message);
1258 } elseif ($this->sendmode ==
'smtps') {
1259 fputs($fp, $this->smtps->log);
1260 } elseif ($this->sendmode ==
'swiftmailer') {
1261 fputs($fp, $this->logger->dump());
1265 if (!empty($conf->global->MAIN_UMASK)) {
1266 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
1280 global $dolibarr_main_data_root;
1282 if (@is_writeable($dolibarr_main_data_root)) {
1283 $srcfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1285 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.".
dol_print_date(
dol_now(),
'dayhourlog',
'gmt').
".err";
1287 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.err";
1290 dol_move($srcfile, $destfile, 0, 1, 0, 0);
1303 if (!preg_match(
'/^[\s\t]*<html/i', $msg)) {
1304 $out =
"<html><head><title></title>";
1305 if (!empty($this->styleCSS)) {
1308 $out .=
"</head><body";
1309 if (!empty($this->bodyCSS)) {
1314 $out .=
"</body></html>";
1329 if (!empty($this->css)) {
1331 $this->styleCSS =
'<style type="text/css">';
1332 $this->styleCSS .=
'body {';
1334 if ($this->css[
'bgcolor']) {
1335 $this->styleCSS .=
' background-color: '.$this->css[
'bgcolor'].
';';
1336 $this->bodyCSS .=
' bgcolor="'.$this->css[
'bgcolor'].
'"';
1338 if ($this->css[
'bgimage']) {
1340 $this->styleCSS .=
' background-image: url("cid:'.$this->css[
'bgimage_cid'].
'");';
1342 $this->styleCSS .=
'}';
1343 $this->styleCSS .=
'</style>';
1360 $host = dol_getprefix(
'email');
1364 $out .=
"From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1365 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
1366 $out .=
"To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1369 $out .=
"Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1370 if (isset($this->reply_to) && $this->reply_to) {
1371 $out .=
"Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1373 if (isset($this->errors_to) && $this->errors_to) {
1374 $out .=
"Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1378 if (isset($this->addr_cc) && $this->addr_cc) {
1379 $out .=
"Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1381 if (isset($this->addr_bcc) && $this->addr_bcc) {
1382 $out .=
"Bcc: ".$this->getValidAddress($this->addr_bcc, 2).$this->eol2;
1386 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1387 $out .=
"Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1392 $out .=
'Date: '.date(
"r").$this->eol2;
1394 $trackid = $this->trackid;
1397 $this->msgid = time().
'.phpmail-dolibarr-'.$trackid.
'@'.$host;
1398 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1399 $out .=
'References: <'.$this->msgid.
">".$this->eol2;
1400 $out .=
'X-Dolibarr-TRACKID: '.$trackid.
'@'.$host.$this->eol2;
1402 $this->msgid = time().
'.phpmail@'.$host;
1403 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1406 if (!empty($_SERVER[
'REMOTE_ADDR'])) {
1407 $out .=
"X-RemoteAddr: ".$_SERVER[
'REMOTE_ADDR'].$this->eol2;
1409 $out .=
"X-Mailer: Dolibarr version ".DOL_VERSION.
" (using php mail)".$this->eol2;
1410 $out .=
"Mime-Version: 1.0".$this->eol2;
1414 $out .=
"Content-Type: multipart/mixed;".$this->eol2.
" boundary=\"".$this->mixed_boundary.
"\"".$this->eol2;
1415 $out .=
"Content-Transfer-Encoding: 8bit".$this->eol2;
1417 dol_syslog(
"CMailFile::write_smtpheaders smtp_header=\n".$out);
1436 if (is_array($filename_list)) {
1437 $filename_list_size = count($filename_list);
1438 for ($i = 0; $i < $filename_list_size; $i++) {
1439 if ($filename_list[$i]) {
1440 if ($mimefilename_list[$i]) {
1441 $filename_list[$i] = $mimefilename_list[$i];
1443 $out .=
"X-attachments: $filename_list[$i]".$this->eol2;
1448 dol_syslog(
"CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1466 $out .=
"--".$this->mixed_boundary.$this->eol;
1468 if ($this->atleastoneimage) {
1469 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1471 $out .=
"--".$this->alternative_boundary.$this->eol;
1475 $strContent = preg_replace(
"/(?<!\r)\n/si",
"\r\n", $msgtext);
1476 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
1477 $strContent = preg_replace(
"/\r\n/si",
"\n", $strContent);
1480 $strContentAltText =
'';
1481 if ($this->msgishtml) {
1483 $strContentAltText = preg_replace(
"/<br\s*[^>]*>/",
" ", $strContent);
1485 $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
1486 $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1495 $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1497 if ($this->msgishtml) {
1498 if ($this->atleastoneimage) {
1499 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1501 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol;
1502 $out .=
"--".$this->alternative_boundary.$this->eol;
1503 $out .=
"Content-Type: multipart/related;".$this->eol.
" boundary=\"".$this->related_boundary.
"\"".$this->eol;
1505 $out .=
"--".$this->related_boundary.$this->eol;
1508 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1509 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1511 $out .=
"--".$this->alternative_boundary.$this->eol;
1512 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1514 $out .= $this->eol.$strContentAltText.$this->eol;
1515 $out .=
"--".$this->alternative_boundary.$this->eol;
1518 $out .=
"Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1520 $out .= $this->eol.$strContent.$this->eol;
1522 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1523 $out .=
"--".$this->alternative_boundary.
"--".$this->eol;
1526 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1528 $out .= $this->eol.$strContent.$this->eol;
1534 if ($this->atleastoneimage) {
1537 $out .=
"--".$this->related_boundary.
"--".$this->eol;
1538 $out .= $this->eol.
"--".$this->alternative_boundary.
"--".$this->eol;
1555 private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
1560 $filename_list_size = count($filename_list);
1561 for ($i = 0; $i < $filename_list_size; $i++) {
1562 if ($filename_list[$i]) {
1565 if ($encoded >= 0) {
1566 if ($mimefilename_list[$i]) {
1567 $filename_list[$i] = $mimefilename_list[$i];
1569 if (!$mimetype_list[$i]) {
1570 $mimetype_list[$i] =
"application/octet-stream";
1573 $out .=
"--".$this->mixed_boundary.$this->eol;
1574 $out .=
"Content-Disposition: attachment; filename=\"".$filename_list[$i].
"\"".$this->eol;
1575 $out .=
"Content-Type: ".$mimetype_list[$i].
"; name=\"".$filename_list[$i].
"\"".$this->eol;
1576 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1577 $out .=
"Content-Description: ".$filename_list[$i].$this->eol;
1578 if (!empty($cidlist) && is_array($cidlist) && $cidlist[$i]) {
1579 $out .=
"X-Attachment-Id: ".$cidlist[$i].$this->eol;
1580 $out .=
"Content-ID: <".$cidlist[$i].
'>'.$this->eol;
1608 if (is_array($images_list)) {
1609 foreach ($images_list as $img) {
1610 dol_syslog(
"CMailFile::write_images: ".$img[
"name"]);
1612 $out .=
"--".$this->related_boundary.$this->eol;
1613 $out .=
"Content-Type: ".$img[
"content_type"].
"; name=\"".$img[
"name"].
"\"".$this->eol;
1614 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1615 $out .=
"Content-Disposition: inline; filename=\"".$img[
"name"].
"\"".$this->eol;
1616 $out .=
"Content-ID: <".$img[
"cid"].
">".$this->eol;
1618 $out .= $img[
"image_encoded"];
1643 if (function_exists(
'fsockopen')) {
1644 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
1645 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
1646 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
1647 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
1648 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
1649 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
1650 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
1651 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
1652 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
1654 if (!empty($this->sendcontext)) {
1655 $smtpContextKey = strtoupper($this->sendcontext);
1657 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
1658 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
1659 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
1660 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
1661 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
1662 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
1663 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
1664 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
1665 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
1666 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
1671 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1672 $host =
'ssl://'.$host;
1677 dol_syslog(
"Try socket connection to host=".$host.
" port=".$port);
1679 $errno = 0; $errstr =
'';
1680 if ($socket = @fsockopen(
1688 if (function_exists(
'stream_set_timeout')) {
1689 stream_set_timeout($socket, $timeout, 0);
1699 $this->error =
utf8_check(
'Error '.$errno.
' - '.$errstr) ?
'Error '.$errno.
' - '.$errstr : utf8_encode(
'Error '.$errno.
' - '.$errstr);
1718 $server_response =
'';
1720 while (substr($server_response, 3, 1) !=
' ') {
1721 if (!($server_response = fgets($socket, 256))) {
1722 $this->error =
"Couldn't get mail server response codes";
1727 if (!(substr($server_response, 0, 3) == $response)) {
1728 $this->error =
"Ran into problems sending Mail.\r\nResponse: $server_response";
1744 $extensions = array_keys($this->image_types);
1750 preg_match_all(
'/(?:"|\')([^"\']+\.('.implode(
'|', $extensions).
'))(?:"|\')/Ui', $this->html, $matches);
1752 if (!empty($matches)) {
1755 foreach ($matches[1] as $full) {
1757 if (preg_match(
'/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) {
1760 if (file_exists($images_dir.
'/'.$img)) {
1762 $src = preg_quote($full,
'/');
1764 $this->html_images[$i][
"fullpath"] = $images_dir.
'/'.$img;
1766 $this->html_images[$i][
"name"] = $img;
1769 if (preg_match(
'/^.+\.(\w{3,4})$/', $img, $regext)) {
1770 $ext = strtolower($regext[1]);
1771 $this->html_images[$i][
"content_type"] = $this->image_types[$ext];
1774 $this->html_images[$i][
"cid"] =
dol_hash($this->html_images[$i][
"fullpath"],
'md5');
1776 $this->html_images[$i][
"type"] =
'cidfromurl';
1778 $this->html = preg_replace(
"/src=\"$src\"|src='$src'/i",
"src=\"cid:".$this->html_images[$i][
"cid"].
"\"", $this->html);
1784 if (!empty($this->html_images)) {
1789 foreach ($this->html_images as $img) {
1790 $fullpath = $images_dir.
'/'.$img[
"name"];
1793 if (!in_array($fullpath, $inline)) {
1795 if ($image = file_get_contents($fullpath)) {
1798 preg_match(
'/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img[
"name"], $regs);
1799 $imgName = $regs[1];
1801 $this->images_encoded[$i][
'name'] = $imgName;
1802 $this->images_encoded[$i][
'fullpath'] = $fullpath;
1803 $this->images_encoded[$i][
'content_type'] = $img[
"content_type"];
1804 $this->images_encoded[$i][
'cid'] = $img[
"cid"];
1806 $this->images_encoded[$i][
"image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
1807 $inline[] = $fullpath;
1834 $extensions = array_keys($this->image_types);
1836 if ($images_dir && !
dol_is_dir($images_dir)) {
1853 preg_match_all(
'/src="data:image\/('.implode(
'|', $extensions).
');base64,([^"]+)"/Ui', $this->html, $matches);
1855 if (!empty($matches) && !empty($matches[1])) {
1856 if (empty($images_dir)) {
1858 $this->error =
'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
1863 foreach ($matches[1] as $key => $ext) {
1865 $filecontent = $matches[2][$key];
1867 $cid =
'cid000'.dol_hash($filecontent,
'md5');
1869 $destfiletmp = $images_dir.
'/'.$cid.
'.'.$ext;
1872 dol_syslog(
"write the cid file ".$destfiletmp);
1873 $fhandle = @fopen($destfiletmp,
'w');
1875 $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
1877 @chmod($destfiletmp, octdec($conf->global->MAIN_UMASK));
1879 $this->errors[] =
"Failed to open file '".$destfiletmp.
"' for write";
1884 if (file_exists($destfiletmp)) {
1886 $this->html_images[$i][
"fullpath"] = $destfiletmp;
1888 $this->html_images[$i][
"name"] = basename($destfiletmp);
1890 $this->html_images[$i][
"content_type"] = $this->image_types[strtolower($ext)];
1892 $this->html_images[$i][
"cid"] = $cid;
1894 $this->html_images[$i][
"type"] =
'cidfromdata';
1896 $this->html = str_replace(
'src="data:image/'.$ext.
';base64,'.$filecontent.
'"',
'src="cid:'.$this->html_images[$i][
"cid"].
'"', $this->html);
1922 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
1928 $arrayaddress = explode(
',', $address);
1932 foreach ($arrayaddress as $val) {
1934 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
1935 $name = trim($regs[1]);
1936 $email = trim($regs[2]);
1939 $email = trim($val);
1947 $newemail = $name ? $name : $email;
1948 $newemail =
'<a href="mailto:'.$email.
'">'.$newemail.
'</a>';
1951 $newemail = $name ? $name : $email;
1956 if ($format == 1 || $format == 3) {
1957 $newemail =
'<'.$email.
'>';
1959 if ($format == 0 || $format == 3) {
1960 if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
1961 $newemail =
'<'.$email.
'>';
1963 $newemail =
'<'.$email.
'>';
1965 $newemail = ($format == 3 ?
'"' :
'').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ?
'"' :
'').
' <'.$email.
'>';
1969 $ret = ($ret ? $ret.
',' :
'').$newemail;
1972 if ($maxnumberofemail && $i >= $maxnumberofemail) {
1973 if (count($arrayaddress) > $maxnumberofemail) {
1997 $arrayaddress = explode(
',', $address);
2000 foreach ($arrayaddress as $val) {
2001 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
2002 $name = trim($regs[1]);
2003 $email = trim($regs[2]);
2006 $email = trim($val);
2009 $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name :
null;