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 $result = $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();
561 $this->message->setCharSet($conf->file->character_set_client);
563 $this->errors[] = $e->getMessage();
566 if (!empty($this->html)) {
575 if ($this->atleastoneimage) {
576 foreach ($this->images_encoded as $img) {
578 $attachment = Swift_Image::fromPath($img[
'fullpath']);
580 $imgcid = $this->message->embed($attachment);
582 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
586 if ($this->msgishtml) {
587 $this->message->setBody($msg,
'text/html');
589 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
591 $this->message->setBody($msg,
'text/plain');
593 $this->message->addPart(
dol_nl2br($msg),
'text/html');
596 if (!empty($this->atleastonefile)) {
597 foreach ($filename_list as $i => $val) {
599 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
600 if (!empty($mimefilename_list[$i])) {
601 $attachment->setFilename($mimefilename_list[$i]);
603 $this->message->attach($attachment);
607 if (!empty($this->addr_cc)) {
611 $this->errors[] = $e->getMessage();
614 if (!empty($this->addr_bcc)) {
618 $this->errors[] = $e->getMessage();
622 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
624 $this->message->setReadReceiptTo($this->
getArrayAddress($this->addr_from));
626 $this->errors[] = $e->getMessage();
632 $this->error =
'Bad value for sendmode';
644 global $conf, $db, $langs, $hookmanager;
646 $errorlevel = error_reporting();
651 if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
652 if (!is_object($hookmanager)) {
653 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
656 $hookmanager->initHooks(array(
'mail'));
658 $parameters = array();
660 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
662 $this->error =
"Error in hook maildao sendMail ".$reshook;
663 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
671 $sendingmode = $this->sendmode;
672 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode ==
'mail') {
674 $listofmethods = array();
675 $listofmethods[
'mail'] =
'PHP mail function';
677 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
681 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
682 $linktoadminemailend =
'</a>';
683 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
684 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
685 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
686 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
687 if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
688 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
689 $this->errors[] = $langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
692 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
697 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
698 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
700 $tmparray1 = explode(
',', $this->addr_to);
701 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
702 $this->error =
'Too much recipients in to:';
703 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
706 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
707 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
709 $tmparray2 = explode(
',', $this->addr_cc);
710 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
711 $this->error =
'Too much recipients in cc:';
712 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
715 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
716 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
718 $tmparray3 = explode(
',', $this->addr_bcc);
719 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
720 $this->error =
'Too much recipients in bcc:';
721 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
724 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
725 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
727 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
728 $this->error =
'Too much recipients in to:, cc:, bcc:';
729 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
733 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
734 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
735 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
736 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
737 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
738 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
739 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
740 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
741 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
742 if (!empty($this->sendcontext)) {
743 $smtpContextKey = strtoupper($this->sendcontext);
745 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
746 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
747 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
748 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
749 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
750 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
751 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
752 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
753 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
754 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
759 if ($this->sendmode ==
'mail') {
762 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_DEBUG);
767 if (isset($_SERVER[
"WINDIR"])) {
768 if (empty($this->addr_from)) {
769 $this->addr_from =
'robot@example.com';
771 @ini_set(
'sendmail_from', $this->
getValidAddress($this->addr_from, 2));
776 if (!empty($conf->global->$keyforsmtpserver)) {
777 ini_set(
'SMTP', $conf->global->$keyforsmtpserver);
779 if (!empty($conf->global->$keyforsmtpport)) {
780 ini_set(
'smtp_port', $conf->global->$keyforsmtpport);
784 if ($res && !$this->subject) {
785 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
786 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
790 if ($res && !$dest) {
791 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
792 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
797 $additionnalparam =
'';
798 if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
803 $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) :
''));
805 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
806 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
809 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
810 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
814 if (preg_match(
'/^win/i', PHP_OS)) {
817 if (preg_match(
'/^mac/i', PHP_OS)) {
821 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
823 $this->message = stripslashes($this->message);
825 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
830 $subjecttouse = $this->subject;
835 if (!empty($additionnalparam)) {
836 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
838 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
842 $langs->load(
"errors");
843 $this->error =
"Failed to send mail with php mail";
845 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
847 $this->error .=
".<br>";
848 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
849 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
851 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
855 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
859 if (isset($_SERVER[
"WINDIR"])) {
860 @ini_restore(
'sendmail_from');
864 if (!empty($conf->global->$keyforsmtpserver)) {
867 if (!empty($conf->global->$keyforsmtpport)) {
868 ini_restore(
'smtp_port');
870 } elseif ($this->sendmode ==
'smtps') {
871 if (!is_object($this->smtps)) {
872 $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.";
873 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
879 $this->smtps->setTransportType(0);
882 if (empty($conf->global->$keyforsmtpserver)) {
883 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
885 if (empty($conf->global->$keyforsmtpport)) {
886 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
890 $server = $conf->global->$keyforsmtpserver;
892 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
895 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
898 $server = ($secure ? $secure.
'://' :
'').$server;
900 $port = $conf->global->$keyforsmtpport;
902 $this->smtps->setHost($server);
903 $this->smtps->setPort($port);
907 if (!empty($conf->global->$keyforsmtpid)) {
908 $loginid = $conf->global->$keyforsmtpid;
909 $this->smtps->setID($loginid);
911 if (!empty($conf->global->$keyforsmtppw)) {
912 $loginpass = $conf->global->$keyforsmtppw;
913 $this->smtps->setPW($loginpass);
917 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
919 $keyforsupportedoauth2array = $conf->global->$keyforsmtpoauthservice;
920 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
921 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
923 $keyforprovider =
'';
925 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
926 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
928 if (isset($supportedoauth2array)) {
929 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
931 $OAUTH_SERVICENAME =
'Unknown';
934 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
936 $storage =
new DoliStorage($db, $conf, $keyforprovider);
938 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
941 if (is_object($tokenobj)) {
942 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
945 if (is_object($tokenobj) && $expire) {
946 $credentials =
new Credentials(
951 $serviceFactory = new \OAuth\ServiceFactory();
952 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
954 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
956 $refreshtoken = $tokenobj->getRefreshToken();
957 $tokenobj = $apiService->refreshAccessToken($tokenobj);
958 $tokenobj->setRefreshToken($refreshtoken);
959 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
962 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
963 if (is_object($tokenobj)) {
964 $this->smtps->setToken($tokenobj->getAccessToken());
966 $this->error =
"Token not found";
970 $this->error = $e->getMessage();
971 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
976 $from = $this->smtps->getFrom(
'org');
977 if ($res && !$from) {
978 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Sender address '$from' invalid";
979 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
982 $dest = $this->smtps->getTo();
983 if ($res && !$dest) {
984 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Recipient address '$dest' invalid";
985 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
990 dol_syslog(
"CMailFile::sendfile: sendMsg, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
992 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
993 $this->smtps->setDebug(
true);
996 $result = $this->smtps->sendMsg();
998 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1003 $smtperrorcode = $this->smtps->lastretval;
1004 dol_syslog(
"CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1006 if ($smtperrorcode ==
'421') {
1021 $result = $this->smtps->getErrors();
1022 if (empty($this->error) && empty($result)) {
1023 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1026 if (empty($this->error)) {
1027 $this->error = $result;
1029 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - ".$this->error, LOG_ERR);
1032 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1037 } elseif ($this->sendmode ==
'swiftmailer') {
1040 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
1043 if (empty($conf->global->$keyforsmtpserver)) {
1044 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
1046 if (empty($conf->global->$keyforsmtpport)) {
1047 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
1051 $server = $conf->global->$keyforsmtpserver;
1053 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1056 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
1060 $this->transport =
new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
1062 if (!empty($conf->global->$keyforsmtpid)) {
1063 $this->transport->setUsername($conf->global->$keyforsmtpid);
1065 if (!empty($conf->global->$keyforsmtppw) &&
getDolGlobalString($keyforsmtpauthtype) !=
"XOAUTH2") {
1066 $this->transport->setPassword($conf->global->$keyforsmtppw);
1069 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1072 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1073 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1075 $keyforprovider =
'';
1077 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
1078 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1080 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
1082 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1084 $storage =
new DoliStorage($db, $conf, $keyforprovider);
1087 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1090 if (is_object($tokenobj)) {
1091 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1094 if (is_object($tokenobj) && $expire) {
1095 $credentials =
new Credentials(
1100 $serviceFactory = new \OAuth\ServiceFactory();
1101 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1103 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1105 $refreshtoken = $tokenobj->getRefreshToken();
1106 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1107 $tokenobj->setRefreshToken($refreshtoken);
1108 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1110 if (is_object($tokenobj)) {
1111 $this->transport->setAuthMode(
'XOAUTH2');
1112 $this->transport->setPassword($tokenobj->getAccessToken());
1114 $this->errors[] =
"Token not found";
1118 $this->errors[] = $e->getMessage();
1119 dol_syslog(
"CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1122 if (!empty($conf->global->$keyforsslseflsigned)) {
1123 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
1128 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1129 $this->message->setEncoder($contentEncoderBase64);
1132 $this->mailer =
new Swift_Mailer($this->transport);
1135 if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
1136 $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
1137 $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
1138 $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
1139 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1140 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
1143 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1145 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
1148 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
1151 dol_syslog(
"CMailFile::sendfile: mailer->send, HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport, LOG_DEBUG);
1154 $failedRecipients = array();
1156 $result = $this->mailer->send($this->message, $failedRecipients);
1158 $this->errors[] = $e->getMessage();
1160 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1165 if (!empty($this->error) || !empty($this->errors) || !$result) {
1166 if (!empty($failedRecipients)) {
1167 $this->errors[] =
'Transport failed for the following addresses: "' . join(
'", "', $failedRecipients) .
'".';
1169 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1172 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1176 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
1182 return 'Bad value for sendmode';
1186 foreach ($this->html_images as $val) {
1187 if (!empty($val[
'type']) && $val[
'type'] ==
'cidfromdata') {
1192 $parameters = array(
'sent' => $res);
1194 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
1196 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
1197 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1202 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1203 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
1206 error_reporting($errorlevel);
1220 return '=?'.$conf->file->character_set_client.
'?B?'.base64_encode($stringtoencode).
'?=';
1235 if (is_readable($newsourcefile)) {
1236 $contents = file_get_contents($newsourcefile);
1237 $encoded = chunk_split(base64_encode($contents), 76, $this->eol);
1240 $this->error =
"Error: Can't read file '".$sourcefile.
"' into _encode_file";
1241 dol_syslog(
"CMailFile::encode_file: ".$this->error, LOG_ERR);
1258 global $conf, $dolibarr_main_data_root;
1260 if (@is_writeable($dolibarr_main_data_root)) {
1261 $outputfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1262 $fp = fopen($outputfile,
"w");
1264 if ($this->sendmode ==
'mail') {
1265 fputs($fp, $this->headers);
1266 fputs($fp, $this->eol);
1267 fputs($fp, $this->message);
1268 } elseif ($this->sendmode ==
'smtps') {
1269 fputs($fp, $this->smtps->log);
1270 } elseif ($this->sendmode ==
'swiftmailer') {
1271 fputs($fp, $this->logger->dump());
1289 global $dolibarr_main_data_root;
1291 if (@is_writeable($dolibarr_main_data_root)) {
1292 $srcfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1298 if (defined(
'SYSLOG_FILE_NO_ERROR')) {
1299 $filefd = @fopen($srcfile,
'a+');
1301 $filefd = fopen($srcfile,
'a+');
1304 fwrite($filefd, $message.
"\n");
1312 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.".
dol_print_date(
dol_now(),
'dayhourlog',
'gmt').
".err";
1314 $destfile = $dolibarr_main_data_root.
"/dolibarr_mail.err";
1317 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1318 dol_move($srcfile, $destfile, 0, 1, 0, 0);
1331 if (!preg_match(
'/^[\s\t]*<html/i', $msg)) {
1332 $out =
"<html><head><title></title>";
1333 if (!empty($this->styleCSS)) {
1336 $out .=
"</head><body";
1337 if (!empty($this->bodyCSS)) {
1342 $out .=
"</body></html>";
1357 if (!empty($this->css)) {
1359 $this->styleCSS =
'<style type="text/css">';
1360 $this->styleCSS .=
'body {';
1362 if ($this->css[
'bgcolor']) {
1363 $this->styleCSS .=
' background-color: '.$this->css[
'bgcolor'].
';';
1364 $this->bodyCSS .=
' bgcolor="'.$this->css[
'bgcolor'].
'"';
1366 if ($this->css[
'bgimage']) {
1368 $this->styleCSS .=
' background-image: url("cid:'.$this->css[
'bgimage_cid'].
'");';
1370 $this->styleCSS .=
'}';
1371 $this->styleCSS .=
'</style>';
1388 $host = dol_getprefix(
'email');
1392 $out .=
"From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1393 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
1394 $out .=
"To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1397 $out .=
"Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1398 if (isset($this->reply_to) && $this->reply_to) {
1399 $out .=
"Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1401 if (isset($this->errors_to) && $this->errors_to) {
1402 $out .=
"Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1406 if (isset($this->addr_cc) && $this->addr_cc) {
1407 $out .=
"Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1409 if (isset($this->addr_bcc) && $this->addr_bcc) {
1410 $out .=
"Bcc: ".$this->getValidAddress($this->addr_bcc, 2).$this->eol2;
1414 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1415 $out .=
"Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1420 $out .=
'Date: '.date(
"r").$this->eol2;
1422 $trackid = $this->trackid;
1425 $this->msgid = time().
'.phpmail-dolibarr-'.$trackid.
'@'.$host;
1426 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1427 $out .=
'References: <'.$this->msgid.
">".$this->eol2;
1428 $out .=
'X-Dolibarr-TRACKID: '.$trackid.
'@'.$host.$this->eol2;
1430 $this->msgid = time().
'.phpmail@'.$host;
1431 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1434 if (!empty($_SERVER[
'REMOTE_ADDR'])) {
1435 $out .=
"X-RemoteAddr: ".$_SERVER[
'REMOTE_ADDR'].$this->eol2;
1437 $out .=
"X-Mailer: Dolibarr version ".DOL_VERSION.
" (using php mail)".$this->eol2;
1438 $out .=
"Mime-Version: 1.0".$this->eol2;
1442 $out .=
"Content-Type: multipart/mixed;".$this->eol2.
" boundary=\"".$this->mixed_boundary.
"\"".$this->eol2;
1443 $out .=
"Content-Transfer-Encoding: 8bit".$this->eol2;
1445 dol_syslog(
"CMailFile::write_smtpheaders smtp_header=\n".$out);
1464 if (is_array($filename_list)) {
1465 $filename_list_size = count($filename_list);
1466 for ($i = 0; $i < $filename_list_size; $i++) {
1467 if ($filename_list[$i]) {
1468 if ($mimefilename_list[$i]) {
1469 $filename_list[$i] = $mimefilename_list[$i];
1471 $out .=
"X-attachments: $filename_list[$i]".$this->eol2;
1476 dol_syslog(
"CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1494 $out .=
"--".$this->mixed_boundary.$this->eol;
1496 if ($this->atleastoneimage) {
1497 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1499 $out .=
"--".$this->alternative_boundary.$this->eol;
1503 $strContent = preg_replace(
"/(?<!\r)\n/si",
"\r\n", $msgtext);
1504 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
1505 $strContent = preg_replace(
"/\r\n/si",
"\n", $strContent);
1508 $strContentAltText =
'';
1509 if ($this->msgishtml) {
1511 $strContentAltText = preg_replace(
"/<br\s*[^>]*>/",
" ", $strContent);
1513 $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
1514 $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1523 $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1525 if ($this->msgishtml) {
1526 if ($this->atleastoneimage) {
1527 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1529 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol;
1530 $out .=
"--".$this->alternative_boundary.$this->eol;
1531 $out .=
"Content-Type: multipart/related;".$this->eol.
" boundary=\"".$this->related_boundary.
"\"".$this->eol;
1533 $out .=
"--".$this->related_boundary.$this->eol;
1536 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1537 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1539 $out .=
"--".$this->alternative_boundary.$this->eol;
1540 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1542 $out .= $this->eol.$strContentAltText.$this->eol;
1543 $out .=
"--".$this->alternative_boundary.$this->eol;
1546 $out .=
"Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1548 $out .= $this->eol.$strContent.$this->eol;
1550 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1551 $out .=
"--".$this->alternative_boundary.
"--".$this->eol;
1554 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1556 $out .= $this->eol.$strContent.$this->eol;
1562 if ($this->atleastoneimage) {
1565 $out .=
"--".$this->related_boundary.
"--".$this->eol;
1566 $out .= $this->eol.
"--".$this->alternative_boundary.
"--".$this->eol;
1583 private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
1588 $filename_list_size = count($filename_list);
1589 for ($i = 0; $i < $filename_list_size; $i++) {
1590 if ($filename_list[$i]) {
1593 if ($encoded >= 0) {
1594 if ($mimefilename_list[$i]) {
1595 $filename_list[$i] = $mimefilename_list[$i];
1597 if (!$mimetype_list[$i]) {
1598 $mimetype_list[$i] =
"application/octet-stream";
1601 $out .=
"--".$this->mixed_boundary.$this->eol;
1602 $out .=
"Content-Disposition: attachment; filename=\"".$filename_list[$i].
"\"".$this->eol;
1603 $out .=
"Content-Type: ".$mimetype_list[$i].
"; name=\"".$filename_list[$i].
"\"".$this->eol;
1604 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1605 $out .=
"Content-Description: ".$filename_list[$i].$this->eol;
1606 if (!empty($cidlist) && is_array($cidlist) && $cidlist[$i]) {
1607 $out .=
"X-Attachment-Id: ".$cidlist[$i].$this->eol;
1608 $out .=
"Content-ID: <".$cidlist[$i].
'>'.$this->eol;
1636 if (is_array($images_list)) {
1637 foreach ($images_list as $img) {
1638 dol_syslog(
"CMailFile::write_images: ".$img[
"name"]);
1640 $out .=
"--".$this->related_boundary.$this->eol;
1641 $out .=
"Content-Type: ".$img[
"content_type"].
"; name=\"".$img[
"name"].
"\"".$this->eol;
1642 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1643 $out .=
"Content-Disposition: inline; filename=\"".$img[
"name"].
"\"".$this->eol;
1644 $out .=
"Content-ID: <".$img[
"cid"].
">".$this->eol;
1646 $out .= $img[
"image_encoded"];
1671 if (function_exists(
'fsockopen')) {
1672 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
1673 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
1674 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
1675 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
1676 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE';
1677 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
1678 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
1679 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
1680 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
1682 if (!empty($this->sendcontext)) {
1683 $smtpContextKey = strtoupper($this->sendcontext);
1685 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
1686 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
1687 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
1688 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
1689 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
1690 $keyforsmtpauthtype =
'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
1691 $keyforsmtpoauthservice =
'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
1692 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
1693 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
1694 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
1699 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1700 $host =
'ssl://'.$host;
1705 dol_syslog(
"Try socket connection to host=".$host.
" port=".$port);
1707 $errno = 0; $errstr =
'';
1708 if ($socket = @fsockopen(
1716 if (function_exists(
'stream_set_timeout')) {
1717 stream_set_timeout($socket, $timeout, 0);
1727 $this->error =
utf8_check(
'Error '.$errno.
' - '.$errstr) ?
'Error '.$errno.
' - '.$errstr : utf8_encode(
'Error '.$errno.
' - '.$errstr);
1746 $server_response =
'';
1748 while (substr($server_response, 3, 1) !=
' ') {
1749 if (!($server_response = fgets($socket, 256))) {
1750 $this->error =
"Couldn't get mail server response codes";
1755 if (!(substr($server_response, 0, 3) == $response)) {
1756 $this->error =
"Ran into problems sending Mail.\r\nResponse: $server_response";
1772 $extensions = array_keys($this->image_types);
1778 preg_match_all(
'/(?:"|\')([^"\']+\.('.implode(
'|', $extensions).
'))(?:"|\')/Ui', $this->html, $matches);
1780 if (!empty($matches)) {
1783 foreach ($matches[1] as $full) {
1785 if (preg_match(
'/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) {
1788 if (file_exists($images_dir.
'/'.$img)) {
1790 $src = preg_quote($full,
'/');
1792 $this->html_images[$i][
"fullpath"] = $images_dir.
'/'.$img;
1794 $this->html_images[$i][
"name"] = $img;
1797 if (preg_match(
'/^.+\.(\w{3,4})$/', $img, $regext)) {
1798 $ext = strtolower($regext[1]);
1799 $this->html_images[$i][
"content_type"] = $this->image_types[$ext];
1802 $this->html_images[$i][
"cid"] =
dol_hash($this->html_images[$i][
"fullpath"],
'md5');
1804 $this->html_images[$i][
"type"] =
'cidfromurl';
1806 $this->html = preg_replace(
"/src=\"$src\"|src='$src'/i",
"src=\"cid:".$this->html_images[$i][
"cid"].
"\"", $this->html);
1812 if (!empty($this->html_images)) {
1817 foreach ($this->html_images as $img) {
1818 $fullpath = $images_dir.
'/'.$img[
"name"];
1821 if (!in_array($fullpath, $inline)) {
1823 if ($image = file_get_contents($fullpath)) {
1826 preg_match(
'/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img[
"name"], $regs);
1827 $imgName = $regs[1];
1829 $this->images_encoded[$i][
'name'] = $imgName;
1830 $this->images_encoded[$i][
'fullpath'] = $fullpath;
1831 $this->images_encoded[$i][
'content_type'] = $img[
"content_type"];
1832 $this->images_encoded[$i][
'cid'] = $img[
"cid"];
1834 $this->images_encoded[$i][
"image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
1835 $inline[] = $fullpath;
1862 $extensions = array_keys($this->image_types);
1864 if ($images_dir && !
dol_is_dir($images_dir)) {
1881 preg_match_all(
'/src="data:image\/('.implode(
'|', $extensions).
');base64,([^"]+)"/Ui', $this->html, $matches);
1883 if (!empty($matches) && !empty($matches[1])) {
1884 if (empty($images_dir)) {
1886 $this->error =
'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
1891 foreach ($matches[1] as $key => $ext) {
1893 $filecontent = $matches[2][$key];
1895 $cid =
'cid000'.dol_hash($filecontent,
'md5');
1897 $destfiletmp = $images_dir.
'/'.$cid.
'.'.$ext;
1900 dol_syslog(
"write the cid file ".$destfiletmp);
1901 $fhandle = @fopen($destfiletmp,
'w');
1903 $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
1907 $this->errors[] =
"Failed to open file '".$destfiletmp.
"' for write";
1912 if (file_exists($destfiletmp)) {
1914 $this->html_images[$i][
"fullpath"] = $destfiletmp;
1916 $this->html_images[$i][
"name"] = basename($destfiletmp);
1918 $this->html_images[$i][
"content_type"] = $this->image_types[strtolower($ext)];
1920 $this->html_images[$i][
"cid"] = $cid;
1922 $this->html_images[$i][
"type"] =
'cidfromdata';
1924 $this->html = str_replace(
'src="data:image/'.$ext.
';base64,'.$filecontent.
'"',
'src="cid:'.$this->html_images[$i][
"cid"].
'"', $this->html);
1950 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
1956 $arrayaddress = explode(
',', $address);
1960 foreach ($arrayaddress as $val) {
1962 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
1963 $name = trim($regs[1]);
1964 $email = trim($regs[2]);
1967 $email = trim($val);
1975 $newemail = $name ? $name : $email;
1976 $newemail =
'<a href="mailto:'.$email.
'">'.$newemail.
'</a>';
1979 $newemail = $name ? $name : $email;
1984 if ($format == 1 || $format == 3) {
1985 $newemail =
'<'.$email.
'>';
1987 if ($format == 0 || $format == 3) {
1988 if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
1989 $newemail =
'<'.$email.
'>';
1991 $newemail =
'<'.$email.
'>';
1993 $newemail = ($format == 3 ?
'"' :
'').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ?
'"' :
'').
' <'.$email.
'>';
1997 $ret = ($ret ? $ret.
',' :
'').$newemail;
2000 if ($maxnumberofemail && $i >= $maxnumberofemail) {
2001 if (count($arrayaddress) > $maxnumberofemail) {
2025 $arrayaddress = explode(
',', $address);
2028 foreach ($arrayaddress as $val) {
2029 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
2030 $name = trim($regs[1]);
2031 $email = trim($regs[2]);
2034 $email = trim($val);
2037 $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name :
null;