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();
133 public $image_boundary;
134 public $atleastoneimage = 0;
135 public $html_images = array();
136 public $images_encoded = array();
137 public $image_types = array(
138 'gif' =>
'image/gif',
139 'jpg' =>
'image/jpeg',
140 'jpeg' =>
'image/jpeg',
141 'jpe' =>
'image/jpeg',
142 'bmp' =>
'image/bmp',
143 'png' =>
'image/png',
144 'tif' =>
'image/tiff',
145 'tiff' =>
'image/tiff',
171 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 =
'')
173 global $conf, $dolibarr_main_data_root, $user;
175 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);
176 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
180 if (is_array($mimefilename_list)) {
181 foreach ($mimefilename_list as $key => $val) {
188 $this->sendcontext = $sendcontext;
191 $this->sendmode =
'';
192 if (!empty($this->sendcontext)) {
193 $smtpContextKey = strtoupper($this->sendcontext);
195 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
196 $this->sendmode = $smtpContextSendMode;
199 if (empty($this->sendmode)) {
200 $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE :
'mail');
206 $this->eol2 =
"\r\n";
207 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
210 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
214 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
217 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
220 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
222 if (empty($subject)) {
223 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
224 $this->error =
'ErrorSubjectIsRequired';
228 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
233 if ($msgishtml == -1) {
234 $this->msgishtml = 0;
236 $this->msgishtml = 1;
239 $this->msgishtml = $msgishtml;
242 global $dolibarr_main_url_root;
245 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
246 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
250 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
252 if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
253 $this->msgishtml = 1;
257 if ($this->msgishtml) {
261 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
266 $findimg = $this->
findHtmlImages($dolibarr_main_data_root.
'/medias');
269 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA)) {
277 foreach ($this->html_images as $i => $val) {
278 if ($this->html_images[$i]) {
279 $this->atleastoneimage = 1;
280 if ($this->html_images[$i][
'type'] ==
'cidfromdata') {
281 if (!in_array($this->html_images[$i][
'fullpath'], $filename_list)) {
283 $posindice = count($filename_list);
284 $filename_list[$posindice] = $this->html_images[$i][
'fullpath'];
285 $mimetype_list[$posindice] = $this->html_images[$i][
'content_type'];
286 $mimefilename_list[$posindice] = $this->html_images[$i][
'name'];
288 $posindice = array_search($this->html_images[$i][
'fullpath'], $filename_list);
291 $cid_list[$posindice] = $this->html_images[$i][
'cid'];
293 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
302 if (is_array($filename_list)) {
303 foreach ($filename_list as $i => $val) {
304 if ($filename_list[$i]) {
305 $this->atleastonefile = 1;
306 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);
313 if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
314 $listofemailstoadd = explode(
',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
315 foreach ($listofemailstoadd as $key => $val) {
316 $emailtoadd = $listofemailstoadd[$key];
317 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
318 if (!empty($user) && !empty($user->email)) {
319 $emailtoadd = $user->email;
324 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
328 $listofemailstoadd[$key] = $emailtoadd;
330 unset($listofemailstoadd[$key]);
333 if (!empty($listofemailstoadd)) {
334 $addr_bcc .= ($addr_bcc ?
', ' :
'').join(
', ', $listofemailstoadd);
338 $this->subject = $subject;
344 $this->deliveryreceipt = $deliveryreceipt;
345 if (empty($replyto)) {
350 $this->trackid = $trackid;
352 $this->filename_list = $filename_list;
353 $this->mimetype_list = $mimetype_list;
354 $this->mimefilename_list = $mimefilename_list;
355 $this->cid_list = $cid_list;
357 if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
360 $this->addr_bcc =
'';
363 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
364 if (!empty($this->sendcontext)) {
365 $smtpContextKey = strtoupper($this->sendcontext);
367 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
368 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
372 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
376 if ($this->sendmode ==
'mail') {
387 if (!empty($moreinheader)) {
388 $smtp_headers .= $moreinheader;
394 if (!empty($this->html)) {
407 if (!empty($this->atleastonefile)) {
408 $files_encoded = $this->
write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
412 $this->headers = $smtp_headers.$mime_headers;
415 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
418 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
419 $this->message .= $text_body.$files_encoded;
420 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
421 } elseif ($this->sendmode ==
'smtps') {
425 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
426 $smtps =
new SMTPs();
427 $smtps->setCharSet($conf->file->character_set_client);
430 $subjecttouse = $this->subject;
435 $smtps->setSubject($subjecttouse);
438 $smtps->setTrackId($this->trackid);
441 if (!empty($moreinheader)) {
442 $smtps->setMoreInHeader($moreinheader);
445 if (!empty($this->html)) {
455 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
457 if ($this->msgishtml) {
458 $smtps->setBodyContent($msg,
'html');
460 $smtps->setBodyContent($msg,
'plain');
463 if ($this->atleastoneimage) {
464 foreach ($this->images_encoded as $img) {
465 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
469 if (!empty($this->atleastonefile)) {
470 foreach ($filename_list as $i => $val) {
471 $content = file_get_contents($filename_list[$i]);
472 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], $cid_list[$i]);
476 $smtps->setCC($this->addr_cc);
477 $smtps->setBCC($this->addr_bcc);
478 $smtps->setErrorsTo($this->errors_to);
479 $smtps->setDeliveryReceipt($this->deliveryreceipt);
480 if (!empty($conf->global->$keyforsslseflsigned)) {
481 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
484 $host = dol_getprefix(
'email');
485 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
487 $this->smtps = $smtps;
488 } elseif ($this->sendmode ==
'swiftmailer') {
490 $host = dol_getprefix(
'email');
492 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
495 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
497 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
501 $this->message =
new Swift_Message();
504 $headers = $this->message->getHeaders();
505 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
506 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
507 $headerID = $this->msgid;
508 $msgid = $headers->get(
'Message-ID');
509 $msgid->setId($headerID);
510 $headers->addIdHeader(
'References', $headerID);
515 $this->message->setSubject($this->subject);
517 $this->errors[] = $e->getMessage();
522 if (!empty($this->addr_from)) {
524 if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
526 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
527 $adressEmailFrom = array();
528 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
529 $adressEmailFrom = reset($adressEmailFrom);
530 if ($emailMatchs !==
false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
531 $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
539 $this->errors[] = $e->getMessage();
544 if (!empty($this->addr_to)) {
548 $this->errors[] = $e->getMessage();
552 if (!empty($this->reply_to)) {
556 $this->errors[] = $e->getMessage();
560 if (!empty($this->errors_to)) {
562 $headers->addTextHeader(
'Errors-To', $this->
getArrayAddress($this->errors_to));
564 $this->errors[] = $e->getMessage();
569 $this->message->setCharSet($conf->file->character_set_client);
571 $this->errors[] = $e->getMessage();
574 if (!empty($this->html)) {
583 if ($this->atleastoneimage) {
584 foreach ($this->images_encoded as $img) {
586 $attachment = Swift_Image::fromPath($img[
'fullpath']);
588 $imgcid = $this->message->embed($attachment);
590 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
594 if ($this->msgishtml) {
595 $this->message->setBody($msg,
'text/html');
597 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
599 $this->message->setBody($msg,
'text/plain');
601 $this->message->addPart(
dol_nl2br($msg),
'text/html');
604 if (!empty($this->atleastonefile)) {
605 foreach ($filename_list as $i => $val) {
607 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
608 if (!empty($mimefilename_list[$i])) {
609 $attachment->setFilename($mimefilename_list[$i]);
611 $this->message->attach($attachment);
615 if (!empty($this->addr_cc)) {
619 $this->errors[] = $e->getMessage();
622 if (!empty($this->addr_bcc)) {
626 $this->errors[] = $e->getMessage();
630 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
632 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
634 $this->errors[] = $e->getMessage();
640 $this->error =
'Bad value for sendmode';
652 global $conf, $db, $langs, $hookmanager;
654 $errorlevel = error_reporting();
659 if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
660 if (!is_object($hookmanager)) {
661 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
664 $hookmanager->initHooks(array(
'mail'));
666 $parameters = array();
668 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
670 $this->error =
"Error in hook maildao sendMail ".$reshook;
671 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
679 $sendingmode = $this->sendmode;
680 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode ==
'mail') {
682 $listofmethods = array();
683 $listofmethods[
'mail'] =
'PHP mail function';
685 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
689 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
690 $linktoadminemailend =
'</a>';
691 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
692 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
693 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
694 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
695 if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
696 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
697 $this->errors[] = $langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
700 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
705 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
706 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
708 $tmparray1 = explode(
',', $this->addr_to);
709 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
710 $this->error =
'Too much recipients in to:';
711 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
714 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
715 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
717 $tmparray2 = explode(
',', $this->addr_cc);
718 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
719 $this->error =
'Too much recipients in cc:';
720 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
723 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
724 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
726 $tmparray3 = explode(
',', $this->addr_bcc);
727 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
728 $this->error =
'Too much recipients in bcc:';
729 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
732 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
733 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
735 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
736 $this->error =
'Too much recipients in to:, cc:, bcc:';
737 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
741 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
742 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
743 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
744 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
745 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
746 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
747 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
748 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
749 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
750 if (!empty($this->sendcontext)) {
751 $smtpContextKey = strtoupper($this->sendcontext);
753 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
754 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
755 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
756 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
757 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
758 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
759 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
760 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
761 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
762 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
767 if ($this->sendmode ==
'mail') {
770 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_DEBUG);
775 if (isset($_SERVER[
"WINDIR"])) {
776 if (empty($this->addr_from)) {
777 $this->addr_from =
'robot@example.com';
779 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
784 if (!empty($conf->global->$keyforsmtpserver)) {
785 ini_set(
'SMTP', $conf->global->$keyforsmtpserver);
787 if (!empty($conf->global->$keyforsmtpport)) {
788 ini_set(
'smtp_port', $conf->global->$keyforsmtpport);
792 if ($res && !$this->subject) {
793 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
794 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
798 if ($res && !$dest) {
799 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
800 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
805 $additionnalparam =
'';
806 if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
811 $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) :
''));
813 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
814 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
817 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
818 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
822 if (preg_match(
'/^win/i', PHP_OS)) {
825 if (preg_match(
'/^mac/i', PHP_OS)) {
829 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
831 $this->message = stripslashes($this->message);
833 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
838 $subjecttouse = $this->subject;
843 if (!empty($additionnalparam)) {
844 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
846 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
850 $langs->load(
"errors");
851 $this->error =
"Failed to send mail with php mail";
853 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
855 $this->error .=
".<br>";
856 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
857 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
859 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
863 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
867 if (isset($_SERVER[
"WINDIR"])) {
868 @ini_restore(
'sendmail_from');
872 if (!empty($conf->global->$keyforsmtpserver)) {
875 if (!empty($conf->global->$keyforsmtpport)) {
876 ini_restore(
'smtp_port');
878 } elseif ($this->sendmode ==
'smtps') {
879 if (!is_object($this->smtps)) {
880 $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.";
881 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
887 $this->smtps->setTransportType(0);
890 if (empty($conf->global->$keyforsmtpserver)) {
891 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
893 if (empty($conf->global->$keyforsmtpport)) {
894 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
898 $server = $conf->global->$keyforsmtpserver;
900 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
903 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
906 $server = ($secure ? $secure.
'://' :
'').$server;
908 $port = $conf->global->$keyforsmtpport;
910 $this->smtps->setHost($server);
911 $this->smtps->setPort($port);
915 if (!empty($conf->global->$keyforsmtpid)) {
916 $loginid = $conf->global->$keyforsmtpid;
917 $this->smtps->setID($loginid);
919 if (!empty($conf->global->$keyforsmtppw)) {
920 $loginpass = $conf->global->$keyforsmtppw;
921 $this->smtps->setPW($loginpass);
925 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
927 $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
928 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
929 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
931 $keyforprovider =
'';
933 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
934 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
936 if (isset($supportedoauth2array)) {
937 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
939 $OAUTH_SERVICENAME =
'Unknown';
942 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
944 $storage =
new DoliStorage($db, $conf, $keyforprovider);
946 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
949 if (is_object($tokenobj)) {
950 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
953 if (is_object($tokenobj) && $expire) {
954 $credentials =
new Credentials(
959 $serviceFactory = new \OAuth\ServiceFactory();
960 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
962 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
964 $refreshtoken = $tokenobj->getRefreshToken();
965 $tokenobj = $apiService->refreshAccessToken($tokenobj);
966 $tokenobj->setRefreshToken($refreshtoken);
967 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
970 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
971 if (is_object($tokenobj)) {
972 $this->smtps->setToken($tokenobj->getAccessToken());
974 $this->error =
"Token not found";
978 $this->error = $e->getMessage();
979 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
984 $from = $this->smtps->getFrom(
'org');
985 if ($res && !$from) {
986 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Sender address '$from' invalid";
987 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
990 $dest = $this->smtps->getTo();
991 if ($res && !$dest) {
992 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Recipient address '$dest' invalid";
993 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
998 dol_syslog(
"CMailFile::sendfile: sendMsg, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
1000 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1001 $this->smtps->setDebug(
true);
1004 $result = $this->smtps->sendMsg();
1006 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1012 $smtperrorcode = $this->smtps->lastretval;
1013 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1015 if ($smtperrorcode ==
'421') {
1030 $result = $this->smtps->getErrors();
1031 if (empty($this->error) && empty($result)) {
1032 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1035 if (empty($this->error)) {
1036 $this->error = $result;
1038 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - ".$this->error, LOG_ERR);
1041 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1046 } elseif ($this->sendmode ==
'swiftmailer') {
1049 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1052 if (empty($conf->global->$keyforsmtpserver)) {
1053 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1055 if (empty($conf->global->$keyforsmtpport)) {
1056 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1060 $server = $conf->global->$keyforsmtpserver;
1062 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1065 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1069 $this->transport =
new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
1071 if (!empty($conf->global->$keyforsmtpid)) {
1072 $this->transport->setUsername($conf->global->$keyforsmtpid);
1074 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1075 $this->transport->setPassword($conf->global->$keyforsmtppw);
1078 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1081 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1082 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1084 $keyforprovider =
'';
1086 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1087 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1089 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
1091 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1093 $storage =
new DoliStorage($db, $conf, $keyforprovider);
1096 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1099 if (is_object($tokenobj)) {
1100 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1103 if (is_object($tokenobj) && $expire) {
1104 $credentials =
new Credentials(
1109 $serviceFactory = new \OAuth\ServiceFactory();
1110 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1112 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1114 $refreshtoken = $tokenobj->getRefreshToken();
1115 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1116 $tokenobj->setRefreshToken($refreshtoken);
1117 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1119 if (is_object($tokenobj)) {
1120 $this->transport->setAuthMode(
'XOAUTH2');
1121 $this->transport->setPassword($tokenobj->getAccessToken());
1123 $this->errors[] =
"Token not found";
1127 $this->errors[] = $e->getMessage();
1128 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1131 if (!empty($conf->global->$keyforsslseflsigned)) {
1132 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1137 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1138 $this->message->setEncoder($contentEncoderBase64);
1141 $this->mailer =
new Swift_Mailer($this->transport);
1144 if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
1145 $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
1146 $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
1147 $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
1148 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1149 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1152 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1154 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1157 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1160 dol_syslog(
"CMailFile::sendfile: mailer->send, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
1163 $failedRecipients = array();
1165 $result = $this->mailer->send($this->message, $failedRecipients);
1167 $this->errors[] = $e->getMessage();
1169 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1174 if (!empty($this->error) || !empty($this->errors) || !$result) {
1175 if (!empty($failedRecipients)) {
1176 $this->errors[] =
'Transport failed for the following addresses: "' . join(
'", "', $failedRecipients) .
'".';
1178 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1181 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1185 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1191 return 'Bad value for sendmode';
1195 foreach ($this->html_images as $val) {
1196 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1201 $parameters = array(
'sent' => $res);
1203 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1205 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1206 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1211 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1212 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1215 error_reporting($errorlevel);
1229 return '=?'.$conf->file->character_set_client.
'?B?'.base64_encode($stringtoencode).
'?=';
1244 if (is_readable($newsourcefile)) {
1245 $contents = file_get_contents($newsourcefile);
1246 $encoded = chunk_split(base64_encode($contents), 76, $this->eol);
1249 $this->error =
"Error in _encode_file() method: Can't read file '".$sourcefile.
"'";
1250 dol_syslog(
"CMailFile::_encode_file: ".$this->error, LOG_ERR);
1267 global $conf, $dolibarr_main_data_root;
1269 if (@is_writeable($dolibarr_main_data_root)) {
1270 $outputfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1271 $fp = fopen($outputfile,
"w");
1273 if ($this->sendmode ==
'mail') {
1274 fputs($fp, $this->headers);
1275 fputs($fp, $this->eol);
1276 fputs($fp, $this->message);
1277 } elseif ($this->sendmode ==
'smtps') {
1278 fputs($fp, $this->smtps->log);
1279 } elseif ($this->sendmode ==
'swiftmailer') {
1280 fputs($fp, $this->logger->dump());
1298 global $dolibarr_main_data_root;
1300 if (@is_writeable($dolibarr_main_data_root)) {
1301 $srcfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1307 if (defined(
'SYSLOG_FILE_NO_ERROR')) {
1308 $filefd = @fopen($srcfile,
'a+');
1310 $filefd = fopen($srcfile,
'a+');
1313 fwrite($filefd, $message.
"\n");
1321 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.".
dol_print_date(
dol_now(),
'dayhourlog',
'gmt').
".err";
1323 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.err";
1326 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1327 dol_move($srcfile, $destfile, 0, 1, 0, 0);
1340 if (!preg_match(
'/^[\s\t]*<html/i', $msg)) {
1341 $out =
"<html><head><title></title>";
1342 if (!empty($this->styleCSS)) {
1345 $out .=
"</head><body";
1346 if (!empty($this->bodyCSS)) {
1351 $out .=
"</body></html>";
1366 if (!empty($this->css)) {
1368 $this->styleCSS =
'<style type="text/css">';
1369 $this->styleCSS .=
'body {';
1371 if ($this->css[
'bgcolor']) {
1372 $this->styleCSS .=
' background-color: '.$this->css[
'bgcolor'].
';';
1373 $this->bodyCSS .=
' bgcolor="'.$this->css[
'bgcolor'].
'"';
1375 if ($this->css[
'bgimage']) {
1377 $this->styleCSS .=
' background-image: url("cid:'.$this->css[
'bgimage_cid'].
'");';
1379 $this->styleCSS .=
'}';
1380 $this->styleCSS .=
'</style>';
1397 $host = dol_getprefix(
'email');
1401 $out .=
"From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1402 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
1403 $out .=
"To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1406 $out .=
"Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1407 if (isset($this->reply_to) && $this->reply_to) {
1408 $out .=
"Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1410 if (isset($this->errors_to) && $this->errors_to) {
1411 $out .=
"Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1415 if (isset($this->addr_cc) && $this->addr_cc) {
1416 $out .=
"Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1418 if (isset($this->addr_bcc) && $this->addr_bcc) {
1419 $out .=
"Bcc: ".$this->getValidAddress($this->addr_bcc, 2).$this->eol2;
1423 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1424 $out .=
"Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1429 $out .=
'Date: '.date(
"r").$this->eol2;
1431 $trackid = $this->trackid;
1434 $this->msgid = time().
'.phpmail-dolibarr-'.$trackid.
'@'.$host;
1435 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1436 $out .=
'References: <'.$this->msgid.
">".$this->eol2;
1437 $out .=
'X-Dolibarr-TRACKID: '.$trackid.
'@'.$host.$this->eol2;
1439 $this->msgid = time().
'.phpmail@'.$host;
1440 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1443 if (!empty($_SERVER[
'REMOTE_ADDR'])) {
1444 $out .=
"X-RemoteAddr: ".$_SERVER[
'REMOTE_ADDR'].$this->eol2;
1446 $out .=
"X-Mailer: Dolibarr version ".DOL_VERSION.
" (using php mail)".$this->eol2;
1447 $out .=
"Mime-Version: 1.0".$this->eol2;
1451 $out .=
"Content-Type: multipart/mixed;".$this->eol2.
" boundary=\"".$this->mixed_boundary.
"\"".$this->eol2;
1452 $out .=
"Content-Transfer-Encoding: 8bit".$this->eol2;
1454 dol_syslog(
"CMailFile::write_smtpheaders smtp_header=\n".$out);
1473 if (is_array($filename_list)) {
1474 $filename_list_size = count($filename_list);
1475 for ($i = 0; $i < $filename_list_size; $i++) {
1476 if ($filename_list[$i]) {
1477 if ($mimefilename_list[$i]) {
1478 $filename_list[$i] = $mimefilename_list[$i];
1480 $out .=
"X-attachments: $filename_list[$i]".$this->eol2;
1485 dol_syslog(
"CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1503 $out .=
"--".$this->mixed_boundary.$this->eol;
1505 if ($this->atleastoneimage) {
1506 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1508 $out .=
"--".$this->alternative_boundary.$this->eol;
1512 $strContent = preg_replace(
"/(?<!\r)\n/si",
"\r\n", $msgtext);
1513 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
1514 $strContent = preg_replace(
"/\r\n/si",
"\n", $strContent);
1517 $strContentAltText =
'';
1518 if ($this->msgishtml) {
1520 $strContentAltText = preg_replace(
"/<br\s*[^>]*>/",
" ", $strContent);
1522 $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
1523 $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1532 $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1534 if ($this->msgishtml) {
1535 if ($this->atleastoneimage) {
1536 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1538 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol;
1539 $out .=
"--".$this->alternative_boundary.$this->eol;
1540 $out .=
"Content-Type: multipart/related;".$this->eol.
" boundary=\"".$this->related_boundary.
"\"".$this->eol;
1542 $out .=
"--".$this->related_boundary.$this->eol;
1545 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1546 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1548 $out .=
"--".$this->alternative_boundary.$this->eol;
1549 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1551 $out .= $this->eol.$strContentAltText.$this->eol;
1552 $out .=
"--".$this->alternative_boundary.$this->eol;
1555 $out .=
"Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1557 $out .= $this->eol.$strContent.$this->eol;
1559 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1560 $out .=
"--".$this->alternative_boundary.
"--".$this->eol;
1563 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1565 $out .= $this->eol.$strContent.$this->eol;
1571 if ($this->atleastoneimage) {
1574 $out .=
"--".$this->related_boundary.
"--".$this->eol;
1575 $out .= $this->eol.
"--".$this->alternative_boundary.
"--".$this->eol;
1592 private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
1597 $filename_list_size = count($filename_list);
1598 for ($i = 0; $i < $filename_list_size; $i++) {
1599 if ($filename_list[$i]) {
1602 if ($encoded >= 0) {
1603 if ($mimefilename_list[$i]) {
1604 $filename_list[$i] = $mimefilename_list[$i];
1606 if (!$mimetype_list[$i]) {
1607 $mimetype_list[$i] =
"application/octet-stream";
1610 $out .=
"--".$this->mixed_boundary.$this->eol;
1611 $out .=
"Content-Disposition: attachment; filename=\"".$filename_list[$i].
"\"".$this->eol;
1612 $out .=
"Content-Type: ".$mimetype_list[$i].
"; name=\"".$filename_list[$i].
"\"".$this->eol;
1613 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1614 $out .=
"Content-Description: ".$filename_list[$i].$this->eol;
1615 if (!empty($cidlist) && is_array($cidlist) && $cidlist[$i]) {
1616 $out .=
"X-Attachment-Id: ".$cidlist[$i].$this->eol;
1617 $out .=
"Content-ID: <".$cidlist[$i].
'>'.$this->eol;
1645 if (is_array($images_list)) {
1646 foreach ($images_list as $img) {
1647 dol_syslog(
"CMailFile::write_images: ".$img[
"name"]);
1649 $out .=
"--".$this->related_boundary.$this->eol;
1650 $out .=
"Content-Type: ".$img[
"content_type"].
"; name=\"".$img[
"name"].
"\"".$this->eol;
1651 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1652 $out .=
"Content-Disposition: inline; filename=\"".$img[
"name"].
"\"".$this->eol;
1653 $out .=
"Content-ID: <".$img[
"cid"].
">".$this->eol;
1655 $out .= $img[
"image_encoded"];
1680 if (function_exists(
'fsockopen')) {
1681 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
1682 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
1683 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
1684 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
1685 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
1686 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
1687 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
1688 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
1689 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
1691 if (!empty($this->sendcontext)) {
1692 $smtpContextKey = strtoupper($this->sendcontext);
1694 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
1695 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
1696 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
1697 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
1698 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
1699 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
1700 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
1701 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
1702 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
1703 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
1708 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1709 $host =
'ssl://'.$host;
1714 dol_syslog(
"Try socket connection to host=".$host.
" port=".$port);
1716 $errno = 0; $errstr =
'';
1717 if ($socket = @fsockopen(
1725 if (function_exists(
'stream_set_timeout')) {
1726 stream_set_timeout($socket, $timeout, 0);
1736 $this->error =
utf8_check(
'Error '.$errno.
' - '.$errstr) ?
'Error '.$errno.
' - '.$errstr : utf8_encode(
'Error '.$errno.
' - '.$errstr);
1755 $server_response =
'';
1757 while (substr($server_response, 3, 1) !=
' ') {
1758 if (!($server_response = fgets($socket, 256))) {
1759 $this->error =
"Couldn't get mail server response codes";
1764 if (!(substr($server_response, 0, 3) == $response)) {
1765 $this->error =
"Ran into problems sending Mail.\r\nResponse: $server_response";
1781 $extensions = array_keys($this->image_types);
1787 preg_match_all(
'/(?:"|\')([^"\']+\.('.implode(
'|', $extensions).
'))(?:"|\')/Ui', $this->html, $matches);
1789 if (!empty($matches)) {
1792 foreach ($matches[1] as $full) {
1794 if (preg_match(
'/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) {
1797 if (file_exists($images_dir.
'/'.$img)) {
1799 $src = preg_quote($full,
'/');
1801 $this->html_images[$i][
"fullpath"] = $images_dir.
'/'.$img;
1803 $this->html_images[$i][
"name"] = $img;
1806 if (preg_match(
'/^.+\.(\w{3,4})$/', $img, $regext)) {
1807 $ext = strtolower($regext[1]);
1808 $this->html_images[$i][
"content_type"] = $this->image_types[$ext];
1811 $this->html_images[$i][
"cid"] =
dol_hash($this->html_images[$i][
"fullpath"],
'md5');
1813 $this->html_images[$i][
"type"] =
'cidfromurl';
1815 $this->html = preg_replace(
"/src=\"$src\"|src='$src'/i",
"src=\"cid:".$this->html_images[$i][
"cid"].
"\"", $this->html);
1821 if (!empty($this->html_images)) {
1826 foreach ($this->html_images as $img) {
1827 $fullpath = $images_dir.
'/'.$img[
"name"];
1830 if (!in_array($fullpath, $inline)) {
1832 if ($image = file_get_contents($fullpath)) {
1835 preg_match(
'/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img[
"name"], $regs);
1836 $imgName = $regs[1];
1838 $this->images_encoded[$i][
'name'] = $imgName;
1839 $this->images_encoded[$i][
'fullpath'] = $fullpath;
1840 $this->images_encoded[$i][
'content_type'] = $img[
"content_type"];
1841 $this->images_encoded[$i][
'cid'] = $img[
"cid"];
1843 $this->images_encoded[$i][
"image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
1844 $inline[] = $fullpath;
1871 $extensions = array_keys($this->image_types);
1873 if ($images_dir && !
dol_is_dir($images_dir)) {
1890 preg_match_all(
'/src="data:image\/('.implode(
'|', $extensions).
');base64,([^"]+)"/Ui', $this->html, $matches);
1892 if (!empty($matches) && !empty($matches[1])) {
1893 if (empty($images_dir)) {
1895 $this->error =
'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
1900 foreach ($matches[1] as $key => $ext) {
1902 $filecontent = $matches[2][$key];
1904 $cid =
'cid000'.dol_hash($filecontent,
'md5');
1906 $destfiletmp = $images_dir.
'/'.$cid.
'.'.$ext;
1909 dol_syslog(
"write the cid file ".$destfiletmp);
1910 $fhandle = @fopen($destfiletmp,
'w');
1912 $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
1916 $this->errors[] =
"Failed to open file '".$destfiletmp.
"' for write";
1921 if (file_exists($destfiletmp)) {
1923 $this->html_images[$i][
"fullpath"] = $destfiletmp;
1925 $this->html_images[$i][
"name"] = basename($destfiletmp);
1927 $this->html_images[$i][
"content_type"] = $this->image_types[strtolower($ext)];
1929 $this->html_images[$i][
"cid"] = $cid;
1931 $this->html_images[$i][
"type"] =
'cidfromdata';
1933 $this->html = str_replace(
'src="data:image/'.$ext.
';base64,'.$filecontent.
'"',
'src="cid:'.$this->html_images[$i][
"cid"].
'"', $this->html);
1959 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
1965 $arrayaddress = explode(
',', $address);
1969 foreach ($arrayaddress as $val) {
1971 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
1972 $name = trim($regs[1]);
1973 $email = trim($regs[2]);
1976 $email = trim($val);
1984 $newemail = $name ? $name : $email;
1985 $newemail =
'<a href="mailto:'.$email.
'">'.$newemail.
'</a>';
1988 $newemail = $name ? $name : $email;
1993 if ($format == 1 || $format == 3) {
1994 $newemail =
'<'.$email.
'>';
1996 if ($format == 0 || $format == 3) {
1997 if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
1998 $newemail =
'<'.$email.
'>';
2000 $newemail =
'<'.$email.
'>';
2002 $newemail = ($format == 3 ?
'"' :
'').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ?
'"' :
'').
' <'.$email.
'>';
2006 $ret = ($ret ? $ret.
',' :
'').$newemail;
2009 if ($maxnumberofemail && $i >= $maxnumberofemail) {
2010 if (count($arrayaddress) > $maxnumberofemail) {
2034 $arrayaddress = explode(
',', $address);
2037 foreach ($arrayaddress as $val) {
2038 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
2039 $name = trim($regs[1]);
2040 $email = trim($regs[2]);
2043 $email = trim($val);
2046 $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name :
null;