dolibarr 20.0.5
CMailFile.class.php
Go to the documentation of this file.
1<?php
34use OAuth\Common\Storage\DoliStorage;
35use OAuth\Common\Consumer\Credentials;
36
37
44{
45 public $sendcontext;
46 public $sendmode;
47 public $sendsetup;
48
52 public $subject;
53 public $addr_from; // From: Label and EMail of sender (must include '<>'). For example '<myemail@example.com>' or 'John Doe <myemail@example.com>' or '<myemail+trackingid@example.com>'). Note that with gmail smtps, value here is forced by google to account (but not the reply-to).
54 // Sender: Who send the email ("Sender" has sent emails on behalf of "From").
55 // Use it when the "From" is an email of a domain that is a SPF protected domain, and the sending smtp server is not this domain. In such case, add Sender field with an email of the protected domain.
56 // Return-Path: Email where to send bounds.
57 public $reply_to; // Reply-To: Email where to send replies from mailer software (mailer use From if reply-to not defined, Gmail use gmail account if reply-to not defined)
58 public $errors_to; // Errors-To: Email where to send errors.
59 public $addr_to;
60 public $addr_cc;
61 public $addr_bcc;
62 public $trackid;
63
64 public $mixed_boundary;
65 public $related_boundary;
66 public $alternative_boundary;
67 public $deliveryreceipt;
68
69 public $atleastonefile;
70
71 public $msg;
72 public $eol;
73 public $eol2;
74
78 public $error = '';
79
83 public $errors = array();
84
85
89 public $smtps;
93 public $mailer;
94
98 public $transport;
102 public $logger;
103
107 public $css;
109 public $styleCSS;
111 public $bodyCSS;
112
116 public $msgid;
117
121 public $in_reply_to;
122
126 public $references;
127
128 public $headers;
129
130 public $message;
131
135 public $filename_list = array();
139 public $mimetype_list = array();
143 public $mimefilename_list = array();
147 public $cid_list = array();
148
149 // Image
150 public $html;
151 public $msgishtml;
152 public $image_boundary;
153 public $atleastoneimage = 0; // at least one image file with file=xxx.ext into content (TODO Debug this. How can this case be tested. Remove if not used).
154 public $html_images = array();
155 public $images_encoded = array();
156 public $image_types = array(
157 'gif' => 'image/gif',
158 'jpg' => 'image/jpeg',
159 'jpeg' => 'image/jpeg',
160 'jpe' => 'image/jpeg',
161 'bmp' => 'image/bmp',
162 'png' => 'image/png',
163 'tif' => 'image/tiff',
164 'tiff' => 'image/tiff',
165 );
166
167
192 public function __construct($subject, $to, $from, $msg, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $addr_cc = "", $addr_bcc = "", $deliveryreceipt = 0, $msgishtml = 0, $errors_to = '', $css = '', $trackid = '', $moreinheader = '', $sendcontext = 'standard', $replyto = '', $upload_dir_tmp = '', $in_reply_to = '', $references = '')
193 {
194 global $conf, $dolibarr_main_data_root, $user;
195
196 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");
197 dol_syslog("CMailFile::CMailfile: subject=".$subject.", deliveryreceipt=".$deliveryreceipt.", msgishtml=".$msgishtml, LOG_DEBUG);
198
199
200 // Clean values of $mimefilename_list
201 if (is_array($mimefilename_list)) {
202 foreach ($mimefilename_list as $key => $val) {
203 $mimefilename_list[$key] = dol_string_unaccent($mimefilename_list[$key]);
204 }
205 }
206
207 $cid_list = array();
208
209 $this->sendcontext = $sendcontext;
210
211 // Define this->sendmode ('mail', 'smtps', 'swiftmailer', ...) according to $sendcontext ('standard', 'emailing', 'ticket', 'passwordreset')
212 $this->sendmode = '';
213 if (!empty($this->sendcontext)) {
214 $smtpContextKey = strtoupper($this->sendcontext);
215 $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
216 if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
217 $this->sendmode = $smtpContextSendMode;
218 }
219 }
220 if (empty($this->sendmode)) {
221 $this->sendmode = getDolGlobalString('MAIN_MAIL_SENDMODE', 'mail');
222 }
223
224 // Add a Feedback-ID. Must be used for stats on spam report only.
225 if ($trackid) {
226 //Examples:
227 // LinkedIn – Feedback-ID: accept_invite_04:linkedin
228 // Twitter – Feedback-ID: 0040162518f58f41d1f0:15491f3b2ee48656f8e7fb2fac:none:twitterESP
229 // Amazon.com : Feedback-ID: 1.eu-west-1.kjoQSiqb8G+7lWWiDVsxjM2m0ynYd4I6yEFlfoox6aY=:AmazonSES
230 $moreinheader .= "Feedback-ID: ".$trackid.':'.dol_getprefix('email').":dolib\r\n";
231 }
232
233 // We define end of line (RFC 821).
234 $this->eol = "\r\n";
235 // We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
236 $this->eol2 = "\r\n";
237 if (getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA')) {
238 $this->eol = "\n";
239 $this->eol2 = "\n";
240 $moreinheader = str_replace("\r\n", "\n", $moreinheader);
241 }
242
243 // On defini mixed_boundary
244 $this->mixed_boundary = "multipart_x.".time().".x_boundary";
245
246 // On defini related_boundary
247 $this->related_boundary = 'mul_'.dol_hash(uniqid("dolibarr2"), 3); // Force md5 hash (does not contain special chars)
248
249 // On defini alternative_boundary
250 $this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contain special chars)
251
252 if (empty($subject)) {
253 dol_syslog("CMailFile::CMailfile: Try to send an email with empty subject");
254 $this->error = 'ErrorSubjectIsRequired';
255 return;
256 }
257 if (empty($msg)) {
258 dol_syslog("CMailFile::CMailfile: Try to send an email with empty body");
259 $msg = '.'; // Avoid empty message (with empty message content, you will see a multipart structure)
260 }
261
262 // Detect if message is HTML (use fast method)
263 if ($msgishtml == -1) {
264 $this->msgishtml = 0;
265 if (dol_textishtml($msg)) {
266 $this->msgishtml = 1;
267 }
268 } else {
269 $this->msgishtml = $msgishtml;
270 }
271
272 global $dolibarr_main_url_root;
273
274 // Define $urlwithroot
275 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
276 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
277 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
278
279 // Replace relative /viewimage to absolute path
280 $msg = preg_replace('/src="'.preg_quote(DOL_URL_ROOT, '/').'\/viewimage\.php/ims', 'src="'.$urlwithroot.'/viewimage.php', $msg, -1);
281
282 if (getDolGlobalString('MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML')) {
283 $this->msgishtml = 1; // To force to send everything with content type html.
284 }
285 dol_syslog("CMailFile::CMailfile: msgishtml=".$this->msgishtml, LOG_DEBUG);
286
287 // Detect images
288 if ($this->msgishtml) {
289 $this->html = $msg;
290
291 $findimg = 0;
292 if (getDolGlobalString('MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS')) { // Off by default
293 // Search into the body for <img tags of links in medias files to replace them with an embedded file
294 // Note because media links are public, this should be useless, except avoid blocking images with email browser.
295 // This converts an embed file with src="/viewimage.php?modulepart... into a cid link
296 // TODO Exclude viewimage used for the read tracker ?
297 $dolibarr_main_data_root_images = $dolibarr_main_data_root;
298 if ((int) $conf->entity !== 1) {
299 $dolibarr_main_data_root_images.='/'.$conf->entity.'/';
300 }
301 $findimg = $this->findHtmlImages($dolibarr_main_data_root_images.'/medias');
302 if ($findimg < 0) {
303 dol_syslog("CMailFile::CMailfile: Error on findHtmlImages");
304 $this->error = 'ErrorInAddAttachmentsImageBaseOnMedia';
305 return;
306 }
307 }
308
309 if (getDolGlobalString('MAIN_MAIL_ADD_INLINE_IMAGES_IF_DATA')) {
310 // Search into the body for <img src="data:image/ext;base64,..." to replace them with an embedded file
311 // This convert an embedded file with src="data:image... into a cid link + attached file
312 $resultImageData = $this->findHtmlImagesIsSrcData($upload_dir_tmp);
313 if ($resultImageData < 0) {
314 dol_syslog("CMailFile::CMailfile: Error on findHtmlImagesInSrcData code=".$resultImageData." upload_dir_tmp=".$upload_dir_tmp);
315 $this->error = 'ErrorInAddAttachmentsImageBaseIsSrcData';
316 return;
317 }
318 $findimg += $resultImageData;
319 }
320
321 // Set atleastoneimage if there is at least one embedded file (into ->html_images)
322 if ($findimg > 0) {
323 foreach ($this->html_images as $i => $val) {
324 if ($this->html_images[$i]) {
325 $this->atleastoneimage = 1;
326 if ($this->html_images[$i]['type'] == 'cidfromdata') {
327 if (!in_array($this->html_images[$i]['fullpath'], $filename_list)) {
328 // If this file path is not already into the $filename_list, we append it at end of array
329 $posindice = count($filename_list);
330 $filename_list[$posindice] = $this->html_images[$i]['fullpath'];
331 $mimetype_list[$posindice] = $this->html_images[$i]['content_type'];
332 $mimefilename_list[$posindice] = $this->html_images[$i]['name'];
333 } else {
334 $posindice = array_search($this->html_images[$i]['fullpath'], $filename_list);
335 }
336 // We complete the array of cid_list
337 $cid_list[$posindice] = $this->html_images[$i]['cid'];
338 }
339 dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
340 }
341 }
342 }
343 }
344 //var_dump($filename_list);
345 //var_dump($cid_list);exit;
346
347 // Set atleastoneimage if there is at least one file (into $filename_list array)
348 if (is_array($filename_list)) {
349 foreach ($filename_list as $i => $val) {
350 if ($filename_list[$i]) {
351 $this->atleastonefile = 1;
352 dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i]." cid_list[$i]=".(empty($cid_list[$i]) ? '' : $cid_list[$i]), LOG_DEBUG);
353 }
354 }
355 }
356
357 // Add auto copy to if not already in $to (Note: Adding bcc for specific modules are also done from pages)
358 // For example MAIN_MAIL_AUTOCOPY_TO can be 'email@example.com, __USER_EMAIL__, ...'
359 if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO')) {
360 $listofemailstoadd = explode(',', getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO'));
361 foreach ($listofemailstoadd as $key => $val) {
362 $emailtoadd = $listofemailstoadd[$key];
363 if (trim($emailtoadd) == '__USER_EMAIL__') {
364 if (!empty($user) && !empty($user->email)) {
365 $emailtoadd = $user->email;
366 } else {
367 $emailtoadd = '';
368 }
369 }
370 if ($emailtoadd && preg_match('/'.preg_quote($emailtoadd, '/').'/i', $to)) {
371 $emailtoadd = ''; // Email already in the "To"
372 }
373 if ($emailtoadd) {
374 $listofemailstoadd[$key] = $emailtoadd;
375 } else {
376 unset($listofemailstoadd[$key]);
377 }
378 }
379 if (!empty($listofemailstoadd)) {
380 $addr_bcc .= ($addr_bcc ? ', ' : '').implode(', ', $listofemailstoadd);
381 }
382 }
383
384 // Verify if $to, $addr_cc and addr_bcc have unwanted addresses
385 if (getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO')) {
386 // Parse to, cc and bcc to remove MAIN_MAIL_FORCE_NOT_SENDING_TO
387 $listofemailstonotsendto = explode(',', getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO'));
388
389 //Verify for $to
390 $replaceto = false;
391 $tabto = explode(",", $to);
392 foreach ($tabto as $key => $addrto) {
393 $addrto = array_keys($this->getArrayAddress($addrto));
394 if (in_array($addrto[0], $listofemailstonotsendto)) {
395 unset($tabto[$key]);
396 $replaceto = true;
397 }
398 }
399 if ($replaceto && getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE')) {
400 $tabto[] = getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE');
401 }
402 $to = implode(',', $tabto);
403
404 //Verify for $addr_cc
405 $replacecc = false;
406 $tabcc = explode(',', $addr_cc);
407 foreach ($tabcc as $key => $cc) {
408 $cc = array_keys($this->getArrayAddress($cc));
409 if (in_array($cc[0], $listofemailstonotsendto)) {
410 unset($tabcc[$key]);
411 $replacecc = true;
412 }
413 }
414 if ($replacecc && getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE')) {
415 $tabcc[] = getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE');
416 }
417 $addr_cc = implode(',', $tabcc);
418
419 //Verify for $addr_bcc
420 $replacebcc = false;
421 $tabbcc = explode(',', $addr_bcc);
422 foreach ($tabbcc as $key => $bcc) {
423 $bcc = array_keys($this->getArrayAddress($bcc));
424 if (in_array($bcc[0], $listofemailstonotsendto)) {
425 unset($tabbcc[$key]);
426 $replacebcc = true;
427 }
428 }
429 if ($replacebcc && getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE')) {
430 $tabbcc[] = getDolGlobalString('MAIN_MAIL_FORCE_NOT_SENDING_TO_REPLACE');
431 }
432 $addr_bcc = implode(',', $tabbcc);
433 }
434
435 // We always use a replyto
436 if (empty($replyto)) {
437 $replyto = dol_sanitizeEmail($from);
438 }
439 // We can force the from
440 if (getDolGlobalString('MAIN_MAIL_FORCE_FROM')) {
441 $from = getDolGlobalString('MAIN_MAIL_FORCE_FROM');
442 }
443
444 $this->subject = $subject;
445 $this->addr_to = dol_sanitizeEmail($to);
446 $this->addr_from = dol_sanitizeEmail($from);
447 $this->msg = $msg;
448 $this->addr_cc = dol_sanitizeEmail($addr_cc);
449 $this->addr_bcc = dol_sanitizeEmail($addr_bcc);
450 $this->deliveryreceipt = $deliveryreceipt;
451 $this->reply_to = dol_sanitizeEmail($replyto);
452 $this->errors_to = dol_sanitizeEmail($errors_to);
453 $this->trackid = $trackid;
454 $this->in_reply_to = $in_reply_to;
455 $this->references = $references;
456 // Set arrays with attached files info
457 $this->filename_list = $filename_list;
458 $this->mimetype_list = $mimetype_list;
459 $this->mimefilename_list = $mimefilename_list;
460 $this->cid_list = $cid_list;
461
462 if (getDolGlobalString('MAIN_MAIL_FORCE_SENDTO')) {
463 $this->addr_to = dol_sanitizeEmail(getDolGlobalString('MAIN_MAIL_FORCE_SENDTO'));
464 $this->addr_cc = '';
465 $this->addr_bcc = '';
466 }
467
468 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
469 if (!empty($this->sendcontext)) {
470 $smtpContextKey = strtoupper($this->sendcontext);
471 $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
472 if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
473 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
474 }
475 }
476
477 dol_syslog("CMailFile::CMailfile: sendmode=".$this->sendmode." addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
478
479 // We set all data according to chose sending method.
480 // We also set a value for ->msgid
481 if ($this->sendmode == 'mail') {
482 // Use mail php function (default PHP method)
483 // ------------------------------------------
484
485 $smtp_headers = "";
486 $mime_headers = "";
487 $text_body = "";
488 $files_encoded = "";
489
490 // Define smtp_headers (this also set SMTP headers from ->msgid, ->in_reply_to and ->references)
491 $smtp_headers = $this->write_smtpheaders();
492 if (!empty($moreinheader)) {
493 $smtp_headers .= $moreinheader; // $moreinheader contains the \r\n
494 }
495
496 // Define mime_headers
497 $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
498
499 if (!empty($this->html)) {
500 if (!empty($css)) {
501 $this->css = $css;
502 $this->buildCSS(); // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
503 }
504
505 $msg = $this->html;
506 }
507
508 // Define body in text_body
509 $text_body = $this->write_body($msg);
510
511 // Add attachments to text_encoded
512 if (!empty($this->atleastonefile)) {
513 $files_encoded = $this->write_files($filename_list, $mimetype_list, $mimefilename_list, $cid_list);
514 }
515
516 // We now define $this->headers and $this->message
517 $this->headers = $smtp_headers.$mime_headers;
518 // Clean the header to avoid that it terminates with a CR character.
519 // This avoid also empty lines at end that can be interpreted as mail injection by email servers.
520 $this->headers = preg_replace("/([\r\n]+)$/i", "", $this->headers);
521
522 //$this->message = $this->eol.'This is a message with multiple parts in MIME format.'.$this->eol;
523 $this->message = 'This is a message with multiple parts in MIME format.'.$this->eol;
524 $this->message .= $text_body.$files_encoded;
525 $this->message .= "--".$this->mixed_boundary."--".$this->eol;
526 } elseif ($this->sendmode == 'smtps') {
527 // Use SMTPS library
528 // ------------------------------------------
529 $host = dol_getprefix('email');
530
531 require_once DOL_DOCUMENT_ROOT.'/core/class/smtps.class.php';
532 $smtps = new SMTPs();
533 $smtps->setCharSet($conf->file->character_set_client);
534
535 // Encode subject if required.
536 $subjecttouse = $this->subject;
537 if (!ascii_check($subjecttouse)) {
538 $subjecttouse = $this->encodetorfc2822($subjecttouse);
539 }
540
541 $smtps->setSubject($subjecttouse);
542 $smtps->setTO($this->getValidAddress($this->addr_to, 0, 1));
543 $smtps->setFrom($this->getValidAddress($this->addr_from, 0, 1));
544 $smtps->setReplyTo($this->getValidAddress($this->reply_to, 0, 1));
545
546 $smtps->setTrackId($this->trackid);
547
548 if (!empty($this->in_reply_to)) {
549 $smtps->setInReplyTo($this->in_reply_to);
550 }
551 if (!empty($this->references)) {
552 $smtps->setReferences($this->references);
553 }
554
555 if (!empty($moreinheader)) {
556 $smtps->setMoreInHeader($moreinheader);
557 }
558
559 //X-Dolibarr-TRACKID, In-Reply-To, References and $moreinheader will be added to header inside the smtps->getHeader
560
561 if (!empty($this->html)) {
562 if (!empty($css)) {
563 $this->css = $css;
564 $this->buildCSS();
565 }
566 $msg = $this->html;
567 $msg = $this->checkIfHTML($msg); // This add a header and a body including custom CSS to the HTML content
568 }
569
570 // Replace . alone on a new line with .. to avoid to have SMTP interpret this as end of message
571 $msg = preg_replace('/(\r|\n)\.(\r|\n)/ims', '\1..\2', $msg);
572
573 if ($this->msgishtml) {
574 $smtps->setBodyContent($msg, 'html');
575 } else {
576 $smtps->setBodyContent($msg, 'plain');
577 }
578
579 if ($this->atleastoneimage) {
580 foreach ($this->images_encoded as $img) {
581 $smtps->setImageInline($img['image_encoded'], $img['name'], $img['content_type'], $img['cid']);
582 }
583 }
584
585 if (!empty($this->atleastonefile)) {
586 foreach ($filename_list as $i => $val) {
587 $content = file_get_contents($filename_list[$i]);
588 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i], (empty($cid_list[$i]) ? '' : $cid_list[$i]));
589 }
590 }
591
592 $smtps->setCC($this->addr_cc);
593 $smtps->setBCC($this->addr_bcc);
594 $smtps->setErrorsTo($this->errors_to);
595 $smtps->setDeliveryReceipt($this->deliveryreceipt);
596 if (getDolGlobalString($keyforsslseflsigned)) {
597 $smtps->setOptions(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)));
598 }
599
600 $this->msgid = time().'.SMTPs-dolibarr-'.$this->trackid.'@'.$host;
601
602 $this->smtps = $smtps;
603 } elseif ($this->sendmode == 'swiftmailer') {
604 // Use Swift Mailer library
605 // ------------------------------------------
606 $host = dol_getprefix('email');
607
608 require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
609
610 // egulias autoloader lib
611 require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/autoload.php';
612
613 require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
614
615 // Create the message
616 //$this->message = Swift_Message::newInstance();
617 $this->message = new Swift_Message();
618 //$this->message = new Swift_SignedMessage();
619 // Adding a trackid header to a message
620 $headers = $this->message->getHeaders();
621
622 $headers->addTextHeader('X-Dolibarr-TRACKID', $this->trackid.'@'.$host);
623 $this->msgid = time().'.swiftmailer-dolibarr-'.$this->trackid.'@'.$host;
624 $headerID = $this->msgid;
625 $msgid = $headers->get('Message-ID');
626 $msgid->setId($headerID);
627
628 // Add 'In-Reply-To:' header
629 if (!empty($this->in_reply_to)) {
630 $headers->addIdHeader('In-Reply-To', $this->in_reply_to);
631 }
632 // Add 'References:' header
633 if (!empty($this->references)) {
634 $headers->addIdHeader('References', $this->references);
635 }
636
637 if (!empty($moreinheader)) {
638 $moreinheaderarray = preg_split('/[\r\n]+/', $moreinheader);
639 foreach ($moreinheaderarray as $moreinheaderval) {
640 $moreinheadervaltmp = explode(':', $moreinheaderval, 2);
641 if (!empty($moreinheadervaltmp[0]) && !empty($moreinheadervaltmp[1])) {
642 $headers->addTextHeader($moreinheadervaltmp[0], $moreinheadervaltmp[1]);
643 }
644 }
645 }
646
647 // Give the message a subject
648 try {
649 $this->message->setSubject($this->subject);
650 } catch (Exception $e) {
651 $this->errors[] = $e->getMessage();
652 }
653
654 // Set the From address with an associative array
655 //$this->message->setFrom(array('john@doe.com' => 'John Doe'));
656 if (!empty($this->addr_from)) {
657 try {
658 if (getDolGlobalString('MAIN_FORCE_DISABLE_MAIL_SPOOFING')) {
659 // Prevent email spoofing for smtp server with a strict configuration
660 $regexp = '/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; // This regular expression extracts all emails from a string
661 $adressEmailFrom = array();
662 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
663 $adressEmailFrom = reset($adressEmailFrom);
664 if ($emailMatchs !== false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
665 $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
666 } else {
667 $this->message->setFrom($this->getArrayAddress($this->addr_from));
668 }
669 } else {
670 $this->message->setFrom($this->getArrayAddress($this->addr_from));
671 }
672 } catch (Exception $e) {
673 $this->errors[] = $e->getMessage();
674 }
675 }
676
677 // Set the To addresses with an associative array
678 if (!empty($this->addr_to)) {
679 try {
680 $this->message->setTo($this->getArrayAddress($this->addr_to));
681 } catch (Exception $e) {
682 $this->errors[] = $e->getMessage();
683 }
684 }
685
686 if (!empty($this->reply_to)) {
687 try {
688 $this->message->SetReplyTo($this->getArrayAddress($this->reply_to));
689 } catch (Exception $e) {
690 $this->errors[] = $e->getMessage();
691 }
692 }
693
694 if (!empty($this->errors_to)) {
695 try {
696 $headers->addTextHeader('Errors-To', $this->getValidAddress($this->errors_to, 0));
697 } catch (Exception $e) {
698 $this->errors[] = $e->getMessage();
699 }
700 }
701
702 try {
703 $this->message->setCharSet($conf->file->character_set_client);
704 } catch (Exception $e) {
705 $this->errors[] = $e->getMessage();
706 }
707
708 if (!empty($this->html)) {
709 if (!empty($css)) {
710 $this->css = $css;
711 $this->buildCSS();
712 }
713 $msg = $this->html;
714 $msg = $this->checkIfHTML($msg); // This add a header and a body including custom CSS to the HTML content
715 }
716
717 if ($this->atleastoneimage) {
718 foreach ($this->html_images as $img) {
719 // $img['fullpath'],$img['image_encoded'],$img['name'],$img['content_type'],$img['cid']
720 $attachment = Swift_Image::fromPath($img['fullpath']);
721 // embed image
722 $imgcid = $this->message->embed($attachment);
723 // replace cid by the one created by swiftmail in html message
724 $msg = str_replace("cid:".$img['cid'], $imgcid, $msg);
725 }
726 foreach ($this->images_encoded as $img) {
727 //$img['fullpath'],$img['image_encoded'],$img['name'],$img['content_type'],$img['cid']
728 $attachment = Swift_Image::fromPath($img['fullpath']);
729 // embed image
730 $imgcid = $this->message->embed($attachment);
731 // replace cid by the one created by swiftmail in html message
732 $msg = str_replace("cid:".$img['cid'], $imgcid, $msg);
733 }
734 }
735
736 if ($this->msgishtml) {
737 $this->message->setBody($msg, 'text/html');
738 // And optionally an alternative body
739 $this->message->addPart(html_entity_decode(strip_tags($msg)), 'text/plain');
740 } else {
741 $this->message->setBody($msg, 'text/plain');
742 // And optionally an alternative body
743 $this->message->addPart(dol_nl2br($msg), 'text/html');
744 }
745
746 if (!empty($this->atleastonefile)) {
747 foreach ($filename_list as $i => $val) {
748 //$this->message->attach(Swift_Attachment::fromPath($filename_list[$i],$mimetype_list[$i]));
749 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
750 if (!empty($mimefilename_list[$i])) {
751 $attachment->setFilename($mimefilename_list[$i]);
752 }
753 $this->message->attach($attachment);
754 }
755 }
756
757 if (!empty($this->addr_cc)) {
758 try {
759 $this->message->setCc($this->getArrayAddress($this->addr_cc));
760 } catch (Exception $e) {
761 $this->errors[] = $e->getMessage();
762 }
763 }
764 if (!empty($this->addr_bcc)) {
765 try {
766 $this->message->setBcc($this->getArrayAddress($this->addr_bcc));
767 } catch (Exception $e) {
768 $this->errors[] = $e->getMessage();
769 }
770 }
771 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
772 try {
773 $this->message->setReadReceiptTo($this->getArrayAddress($this->addr_from));
774 } catch (Exception $e) {
775 $this->errors[] = $e->getMessage();
776 }
777 }
778 } else {
779 // Send mail method not correctly defined
780 // --------------------------------------
781 $this->error = 'Bad value for sendmode';
782 }
783 }
784
792 public function sendfile()
793 {
794 global $conf, $db, $langs, $hookmanager;
795
796 $errorlevel = error_reporting();
797 //error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
798
799 $res = false;
800
801 if (!getDolGlobalString('MAIN_DISABLE_ALL_MAILS')) {
802 if (!is_object($hookmanager)) {
803 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
804 $hookmanager = new HookManager($db);
805 }
806 $hookmanager->initHooks(array('mail'));
807
808 $parameters = array();
809 $action = '';
810 $reshook = $hookmanager->executeHooks('sendMail', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
811 if ($reshook < 0) {
812 $this->error = "Error in hook maildao sendMail ".$reshook;
813 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
814
815 return false;
816 }
817 if ($reshook == 1) { // Hook replace standard code
818 return true;
819 }
820
821 $sendingmode = $this->sendmode;
822 if ($this->sendcontext == 'emailing' && getDolGlobalString('MAILING_NO_USING_PHPMAIL') && $sendingmode == 'mail') {
823 // List of sending methods
824 $listofmethods = array();
825 $listofmethods['mail'] = 'PHP mail function';
826 //$listofmethods['simplemail']='Simplemail class';
827 $listofmethods['smtps'] = 'SMTP/SMTPS socket library';
828
829 // EMailing feature may be a spam problem, so when you host several users/instance, having this option may force each user to use their own SMTP agent.
830 // You ensure that every user is using its own SMTP server when using the mass emailing module.
831 $linktoadminemailbefore = '<a href="'.DOL_URL_ROOT.'/admin/mails.php">';
832 $linktoadminemailend = '</a>';
833 $this->error = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
834 $this->errors[] = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
835 $this->error .= '<br>'.$langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
836 $this->errors[] = $langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
837 if (getDolGlobalString('MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS')) {
838 $this->error .= '<br>'.$langs->trans("MailSendSetupIs3", getDolGlobalString('MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
839 $this->errors[] = $langs->trans("MailSendSetupIs3", getDolGlobalString('MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS'));
840 }
841
842 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
843 return false;
844 }
845
846 // Check number of recipient is lower or equal than MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL
847 if (!getDolGlobalString('MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL')) {
848 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
849 }
850 $tmparray1 = explode(',', $this->addr_to);
851 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
852 $this->error = 'Too much recipients in to:';
853 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
854 return false;
855 }
856 if (!getDolGlobalString('MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL')) {
857 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
858 }
859 $tmparray2 = explode(',', $this->addr_cc);
860 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
861 $this->error = 'Too much recipients in cc:';
862 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
863 return false;
864 }
865 if (!getDolGlobalString('MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL')) {
866 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
867 }
868 $tmparray3 = explode(',', $this->addr_bcc);
869 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
870 $this->error = 'Too much recipients in bcc:';
871 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
872 return false;
873 }
874 if (!getDolGlobalString('MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL')) {
875 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
876 }
877 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
878 $this->error = 'Too much recipients in to:, cc:, bcc:';
879 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
880 return false;
881 }
882
883 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
884 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
885 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
886 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
887 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE';
888 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
889 $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
890 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
891 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
892 if (!empty($this->sendcontext)) {
893 $smtpContextKey = strtoupper($this->sendcontext);
894 $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
895 if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
896 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
897 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
898 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
899 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
900 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
901 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
902 $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
903 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
904 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
905 }
906 }
907
908 // Action according to chose sending method
909 if ($this->sendmode == 'mail') {
910 // Use mail php function (default PHP method)
911 // ------------------------------------------
912 dol_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject, LOG_NOTICE);
913 //dol_syslog("CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
914 //dol_syslog("CMailFile::sendfile message=\n".$message);
915
916 // If Windows, sendmail_from must be defined
917 if (isset($_SERVER["WINDIR"])) {
918 if (empty($this->addr_from)) {
919 $this->addr_from = 'robot@example.com';
920 }
921 @ini_set('sendmail_from', $this->getValidAddress($this->addr_from, 2));
922 }
923
924 // Force parameters
925 //dol_syslog("CMailFile::sendfile conf->global->".$keyforsmtpserver."=".getDolGlobalString($keyforsmtpserver)." cpnf->global->".$keyforsmtpport."=".$conf->global->$keyforsmtpport, LOG_DEBUG);
926 if (getDolGlobalString($keyforsmtpserver)) {
927 ini_set('SMTP', getDolGlobalString($keyforsmtpserver));
928 }
929 if (getDolGlobalString($keyforsmtpport)) {
930 ini_set('smtp_port', getDolGlobalString($keyforsmtpport));
931 }
932
933 $res = true;
934 if ($res && !$this->subject) {
935 $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Subject is empty";
936 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
937 $res = false;
938 }
939 $dest = $this->getValidAddress($this->addr_to, 2);
940 if ($res && !$dest) {
941 $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
942 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
943 $res = false;
944 }
945
946 if ($res) {
947 $additionnalparam = ''; // By default
948 if (getDolGlobalString('MAIN_MAIL_ALLOW_SENDMAIL_F')) {
949 // When using the phpmail function, the mail command may force the from to the user of the login, for example: linuxuser@myserver.mydomain.com
950 // You can try to set this option to have the command use the From. if it does not work, you can also try the MAIN_MAIL_SENDMAIL_FORCE_BA.
951 // So forcing using the option -f of sendmail is possible if constant MAIN_MAIL_ALLOW_SENDMAIL_F is defined.
952 // Having this variable defined may create problems with some sendmail (option -f refused)
953 // Having this variable not defined may create problems with some other sendmail (option -f required)
954 $additionnalparam .= ($additionnalparam ? ' ' : '').(getDolGlobalString('MAIN_MAIL_ERRORS_TO') ? '-f'.$this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO, 2) : ($this->addr_from != '' ? '-f'.$this->getValidAddress($this->addr_from, 2) : ''));
955 }
956 if (getDolGlobalString('MAIN_MAIL_SENDMAIL_FORCE_BA')) { // To force usage of -ba option. This option tells sendmail to read From: or Sender: to setup sender
957 $additionnalparam .= ($additionnalparam ? ' ' : '').'-ba';
958 }
959
960 if (getDolGlobalString('MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM')) {
961 $additionnalparam .= ($additionnalparam ? ' ' : '').'-U '.$additionnalparam; // Use -U to add additional params
962 }
963
964 $linuxlike = 1;
965 if (preg_match('/^win/i', PHP_OS)) {
966 $linuxlike = 0;
967 }
968 if (preg_match('/^mac/i', PHP_OS)) {
969 $linuxlike = 0;
970 }
971
972 dol_syslog("CMailFile::sendfile: mail start".($linuxlike ? '' : " HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')).", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
973
974 $this->message = stripslashes($this->message);
975
976 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
977 $this->dump_mail();
978 }
979
980 // Encode subject if required.
981 $subjecttouse = $this->subject;
982 if (!ascii_check($subjecttouse)) {
983 $subjecttouse = $this->encodetorfc2822($subjecttouse);
984 }
985
986 if (!empty($additionnalparam)) {
987 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
988 } else {
989 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
990 }
991
992 if (!$res) {
993 $langs->load("errors");
994 $this->error = "Failed to send mail with php mail";
995 if (!$linuxlike) {
996 $this->error .= " to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port'); // This values are value used only for non linuxlike systems
997 }
998 $this->error .= ".<br>";
999 $this->error .= $langs->trans("ErrorPhpMailDelivery");
1000 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1001
1002 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1003 $this->save_dump_mail_in_err('Mail with topic '.$this->subject);
1004 }
1005 } else {
1006 dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
1007 }
1008 }
1009
1010 if (isset($_SERVER["WINDIR"])) {
1011 @ini_restore('sendmail_from');
1012 }
1013
1014 // Restore parameters
1015 if (getDolGlobalString($keyforsmtpserver)) {
1016 ini_restore('SMTP');
1017 }
1018 if (getDolGlobalString($keyforsmtpport)) {
1019 ini_restore('smtp_port');
1020 }
1021 } elseif ($this->sendmode == 'smtps') {
1022 if (!is_object($this->smtps)) {
1023 $this->error = "Failed to send mail with smtps lib<br>Constructor of object CMailFile was not initialized without errors.";
1024 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1025 return false;
1026 }
1027
1028 // Use SMTPS library
1029 // ------------------------------------------
1030 $this->smtps->setTransportType(0); // Only this method is coded in SMTPs library
1031
1032 // Clean parameters
1033 if (empty($conf->global->$keyforsmtpserver)) {
1034 $conf->global->$keyforsmtpserver = ini_get('SMTP');
1035 }
1036 if (empty($conf->global->$keyforsmtpport)) {
1037 $conf->global->$keyforsmtpport = ini_get('smtp_port');
1038 }
1039
1040 // If we use SSL/TLS
1041 $server = getDolGlobalString($keyforsmtpserver);
1042 $secure = '';
1043 if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
1044 $secure = 'ssl';
1045 }
1046 if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
1047 $secure = 'tls';
1048 }
1049 $server = ($secure ? $secure.'://' : '').$server;
1050
1051 $port = getDolGlobalInt($keyforsmtpport);
1052
1053 $this->smtps->setHost($server);
1054 $this->smtps->setPort($port); // 25, 465...;
1055
1056 $loginid = '';
1057 $loginpass = '';
1058 if (!empty($conf->global->$keyforsmtpid)) {
1059 $loginid = getDolGlobalString($keyforsmtpid);
1060 $this->smtps->setID($loginid);
1061 }
1062 if (!empty($conf->global->$keyforsmtppw)) {
1063 $loginpass = getDolGlobalString($keyforsmtppw);
1064 $this->smtps->setPW($loginpass);
1065 }
1066
1067 if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
1068 require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // define $supportedoauth2array
1069
1070 $supportedoauth2array = getSupportedOauth2Array();
1071
1072 $keyforsupportedoauth2array = getDolGlobalString($keyforsmtpoauthservice);
1073 if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
1074 $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
1075 } else {
1076 $keyforprovider = '';
1077 }
1078 $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
1079 $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
1080
1081 if (!empty($supportedoauth2array)) {
1082 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']));
1083 $nameofservice .= ($keyforprovider ? '-'.$keyforprovider : '');
1084 $OAUTH_SERVICENAME = $nameofservice;
1085 } else {
1086 $OAUTH_SERVICENAME = 'Unknown';
1087 }
1088
1089 $keyforparamtenant = 'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']).($keyforprovider ? '-'.$keyforprovider : '').'_TENANT';
1090
1091 require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
1092
1093 $storage = new DoliStorage($db, $conf, $keyforprovider, getDolGlobalString($keyforparamtenant));
1094 try {
1095 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1096
1097 $expire = false;
1098 // Is token expired or will token expire in the next 30 seconds
1099 if (is_object($tokenobj)) {
1100 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1101 }
1102 // Token expired so we refresh it
1103 if (is_object($tokenobj) && $expire) {
1104 $credentials = new Credentials(
1105 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_ID'),
1106 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_SECRET'),
1107 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_URLAUTHORIZE')
1108 );
1109 $serviceFactory = new \OAuth\ServiceFactory();
1110 $oauthname = explode('-', $OAUTH_SERVICENAME);
1111 // ex service is Google-Emails we need only the first part Google
1112 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1113 // We have to save the token because Google give it only once
1114 $refreshtoken = $tokenobj->getRefreshToken();
1115 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1116 $tokenobj->setRefreshToken($refreshtoken);
1117 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1118
1119 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1120 }
1121
1122 if (is_object($tokenobj)) {
1123 $this->smtps->setToken($tokenobj->getAccessToken());
1124 } else {
1125 $this->error = "Token not found";
1126 }
1127 } catch (Exception $e) {
1128 // Return an error if token not found
1129 $this->error = $e->getMessage();
1130 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1131 }
1132 }
1133
1134 $res = true;
1135 $from = $this->smtps->getFrom('org');
1136 if ($res && !$from) {
1137 $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - Sender address '$from' invalid";
1138 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1139 $res = false;
1140 }
1141 $dest = $this->smtps->getTo();
1142 if ($res && !$dest) {
1143 $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - Recipient address '$dest' invalid";
1144 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1145 $res = false;
1146 }
1147
1148 if ($res) {
1149 dol_syslog("CMailFile::sendfile: sendMsg, HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport), LOG_NOTICE);
1150
1151 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1152 $this->smtps->setDebug(true);
1153 }
1154
1155 $result = $this->smtps->sendMsg();
1156
1157 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1158 $this->dump_mail();
1159 }
1160
1161 $smtperrorcode = 0;
1162 if (! $result) {
1163 $smtperrorcode = $this->smtps->lastretval; // SMTP error code
1164 dol_syslog("CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1165
1166 if ($smtperrorcode == '421') { // Try later
1167 // TODO Add a delay and try again
1168 /*
1169 dol_syslog("CMailFile::sendfile: Try later error, so we wait and we retry");
1170 sleep(2);
1171
1172 $result = $this->smtps->sendMsg();
1173
1174 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
1175 $this->dump_mail();
1176 }
1177 */
1178 }
1179 }
1180
1181 $result = $this->smtps->getErrors(); // applicative error code (not SMTP error code)
1182 if (empty($this->error) && empty($result)) {
1183 dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
1184 $res = true;
1185 } else {
1186 if (empty($this->error)) {
1187 $this->error = $result;
1188 }
1189 dol_syslog("CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - ".$this->error, LOG_ERR);
1190 $res = false;
1191
1192 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1193 $this->save_dump_mail_in_err('Mail smtp error '.$smtperrorcode.' with topic '.$this->subject);
1194 }
1195 }
1196 }
1197 } elseif ($this->sendmode == 'swiftmailer') {
1198 // Use Swift Mailer library
1199 // ------------------------------------------
1200 require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
1201
1202 // Clean parameters
1203 if (empty($conf->global->$keyforsmtpserver)) {
1204 $conf->global->$keyforsmtpserver = ini_get('SMTP');
1205 }
1206 if (empty($conf->global->$keyforsmtpport)) {
1207 $conf->global->$keyforsmtpport = ini_get('smtp_port');
1208 }
1209
1210 // If we use SSL/TLS
1211 $server = getDolGlobalString($keyforsmtpserver);
1212 $secure = '';
1213 if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
1214 $secure = 'ssl';
1215 }
1216 if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
1217 $secure = 'tls';
1218 }
1219
1220 $this->transport = new Swift_SmtpTransport($server, getDolGlobalString($keyforsmtpport), $secure);
1221
1222 if (!empty($conf->global->$keyforsmtpid)) {
1223 $this->transport->setUsername(getDolGlobalString($keyforsmtpid));
1224 }
1225 if (!empty($conf->global->$keyforsmtppw) && getDolGlobalString($keyforsmtpauthtype) != "XOAUTH2") {
1226 $this->transport->setPassword(getDolGlobalString($keyforsmtppw));
1227 }
1228 if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
1229 require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
1230
1231 $supportedoauth2array = getSupportedOauth2Array();
1232
1233 $keyforsupportedoauth2array = getDolGlobalString($keyforsmtpoauthservice);
1234 if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
1235 $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
1236 } else {
1237 $keyforprovider = '';
1238 }
1239 $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
1240 $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
1241
1242 if (!empty($supportedoauth2array)) {
1243 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']));
1244 $nameofservice .= ($keyforprovider ? '-'.$keyforprovider : '');
1245 $OAUTH_SERVICENAME = $nameofservice;
1246 } else {
1247 $OAUTH_SERVICENAME = 'Unknown';
1248 }
1249
1250 $keyforparamtenant = 'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']).($keyforprovider ? '-'.$keyforprovider : '').'_TENANT';
1251
1252 require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
1253
1254 $storage = new DoliStorage($db, $conf, $keyforprovider, getDolGlobalString($keyforparamtenant));
1255
1256 try {
1257 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1258
1259 $expire = false;
1260 // Is token expired or will token expire in the next 30 seconds
1261 if (is_object($tokenobj)) {
1262 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1263 }
1264 // Token expired so we refresh it
1265 if (is_object($tokenobj) && $expire) {
1266 $credentials = new Credentials(
1267 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_ID'),
1268 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_SECRET'),
1269 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_URLAUTHORIZE')
1270 );
1271 $serviceFactory = new \OAuth\ServiceFactory();
1272 $oauthname = explode('-', $OAUTH_SERVICENAME);
1273 // ex service is Google-Emails we need only the first part Google
1274 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1275 // We have to save the token because Google give it only once
1276 $refreshtoken = $tokenobj->getRefreshToken();
1277 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1278 $tokenobj->setRefreshToken($refreshtoken);
1279 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1280
1281 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1282 }
1283
1284 if (is_object($tokenobj)) {
1285 $this->transport->setAuthMode('XOAUTH2');
1286 $this->transport->setPassword($tokenobj->getAccessToken());
1287 } else {
1288 $this->errors[] = "Token not found";
1289 }
1290 } catch (Exception $e) {
1291 // Return an error if token not found
1292 $this->errors[] = $e->getMessage();
1293 dol_syslog("CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1294 }
1295 }
1296 if (getDolGlobalString($keyforsslseflsigned)) {
1297 $this->transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
1298 }
1299 //$smtps->_msgReplyTo = 'reply@web.com';
1300
1301 // Switch content encoding to base64 - avoid the doubledot issue with quoted-printable
1302 $contentEncoderBase64 = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1303 $this->message->setEncoder($contentEncoderBase64);
1304
1305 // Create the Mailer using your created Transport
1306 $this->mailer = new Swift_Mailer($this->transport);
1307
1308 // DKIM SIGN
1309 if (getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_ENABLED')) {
1310 $privateKey = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY');
1311 $domainName = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_DOMAIN');
1312 $selector = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_SELECTOR');
1313 $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1314 $this->message->attachSigner($signer->ignoreHeader('Return-Path'));
1315 }
1316
1317 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1318 // To use the ArrayLogger
1319 $this->logger = new Swift_Plugins_Loggers_ArrayLogger();
1320 // Or to use the Echo Logger
1321 //$this->logger = new Swift_Plugins_Loggers_EchoLogger();
1322 $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->logger));
1323 }
1324
1325 dol_syslog("CMailFile::sendfile: mailer->send, HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport), LOG_NOTICE);
1326
1327 // send mail
1328 $failedRecipients = array();
1329 try {
1330 $result = $this->mailer->send($this->message, $failedRecipients);
1331 } catch (Exception $e) {
1332 $this->errors[] = $e->getMessage();
1333 }
1334 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1335 $this->dump_mail();
1336 }
1337
1338 $res = true;
1339 if (!empty($this->error) || !empty($this->errors) || !$result) {
1340 if (!empty($failedRecipients)) {
1341 $this->error = 'Transport failed for the following addresses: "' . implode('", "', $failedRecipients) . '".';
1342 $this->errors[] = $this->error;
1343 }
1344 dol_syslog("CMailFile::sendfile: mail end error=". implode(' ', $this->errors), LOG_ERR);
1345 $res = false;
1346
1347 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1348 $this->save_dump_mail_in_err('Mail with topic '.$this->subject);
1349 }
1350 } else {
1351 dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
1352 }
1353 } else {
1354 // Send mail method not correctly defined
1355 // --------------------------------------
1356
1357 $this->error = 'Bad value for sendmode';
1358 return false;
1359 }
1360
1361 // Now we delete image files that were created dynamically to manage data inline files
1362 foreach ($this->html_images as $val) {
1363 if (!empty($val['type']) && $val['type'] == 'cidfromdata') {
1364 //dol_delete($val['fullpath']);
1365 }
1366 }
1367
1368 $parameters = array('sent' => $res);
1369 $action = '';
1370 $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1371 if ($reshook < 0) {
1372 $this->error = "Error in hook maildao sendMailAfter ".$reshook;
1373 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1374
1375 return false;
1376 }
1377 } else {
1378 $this->error = 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1379 dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
1380 }
1381
1382 error_reporting($errorlevel); // Reactive niveau erreur origine
1383 return $res;
1384 }
1385
1392 public static function encodetorfc2822($stringtoencode)
1393 {
1394 global $conf;
1395 return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
1396 }
1397
1398 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1405 private function _encode_file($sourcefile)
1406 {
1407 // phpcs:enable
1408 $newsourcefile = dol_osencode($sourcefile);
1409
1410 if (is_readable($newsourcefile)) {
1411 $contents = file_get_contents($newsourcefile); // Need PHP 4.3
1412 $encoded = chunk_split(base64_encode($contents), 76, $this->eol); // 76 max is defined into http://tools.ietf.org/html/rfc2047
1413 return $encoded;
1414 } else {
1415 $this->error = "Error in _encode_file() method: Can't read file '".$sourcefile."'";
1416 dol_syslog("CMailFile::_encode_file: ".$this->error, LOG_ERR);
1417 return -1;
1418 }
1419 }
1420
1421
1422 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1430 public function dump_mail()
1431 {
1432 // phpcs:enable
1433 global $dolibarr_main_data_root;
1434
1435 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
1436 $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
1437 $fp = fopen($outputfile, "w"); // overwrite
1438
1439 if ($this->sendmode == 'mail') {
1440 fwrite($fp, $this->headers);
1441 fwrite($fp, $this->eol); // This eol is added by the mail function, so we add it in log
1442 fwrite($fp, $this->message);
1443 } elseif ($this->sendmode == 'smtps') {
1444 fwrite($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
1445 } elseif ($this->sendmode == 'swiftmailer') {
1446 fwrite($fp, "smtpheader=\n".$this->message->getHeaders()->toString()."\n");
1447 fwrite($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
1448 }
1449
1450 fclose($fp);
1451 dolChmod($outputfile);
1452
1453 // Move dolibarr_mail.log into a dolibarr_mail.YYYYMMDD.log
1454 if (getDolGlobalString('MAIN_MAIL_DEBUG_LOG_WITH_DATE')) {
1455 $destfile = $dolibarr_main_data_root."/dolibarr_mail.".dol_print_date(dol_now(), 'dayhourlog', 'gmt').".log";
1456
1457 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1458 dol_move($outputfile, $destfile, 0, 1, 0, 0);
1459 }
1460 }
1461 }
1462
1463 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1471 public function save_dump_mail_in_err($message = '')
1472 {
1473 global $dolibarr_main_data_root;
1474
1475 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
1476 $srcfile = $dolibarr_main_data_root."/dolibarr_mail.log";
1477
1478 // Add message to dolibarr_mail.log. We do not use dol_syslog() on purpose,
1479 // to be sure to write into dolibarr_mail.log
1480 if ($message) {
1481 // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
1482 if (defined('SYSLOG_FILE_NO_ERROR')) {
1483 $filefd = @fopen($srcfile, 'a+');
1484 } else {
1485 $filefd = fopen($srcfile, 'a+');
1486 }
1487 if ($filefd) {
1488 fwrite($filefd, $message."\n");
1489 fclose($filefd);
1490 dolChmod($srcfile);
1491 }
1492 }
1493
1494 // Move dolibarr_mail.log into a dolibarr_mail.err or dolibarr_mail.date.err
1495 if (getDolGlobalString('MAIN_MAIL_DEBUG_ERR_WITH_DATE')) {
1496 $destfile = $dolibarr_main_data_root."/dolibarr_mail.".dol_print_date(dol_now(), 'dayhourlog', 'gmt').".err";
1497 } else {
1498 $destfile = $dolibarr_main_data_root."/dolibarr_mail.err";
1499 }
1500
1501 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1502 dol_move($srcfile, $destfile, 0, 1, 0, 0);
1503 }
1504 }
1505
1506
1513 public function checkIfHTML($msg)
1514 {
1515 if (!preg_match('/^[\s\t]*<html/i', $msg)) {
1516 $out = "<html><head><title></title>";
1517 if (!empty($this->styleCSS)) {
1518 $out .= $this->styleCSS;
1519 }
1520 $out .= "</head><body";
1521 if (!empty($this->bodyCSS)) {
1522 $out .= $this->bodyCSS;
1523 }
1524 $out .= ">";
1525 $out .= $msg;
1526 $out .= "</body></html>";
1527 } else {
1528 $out = $msg;
1529 }
1530
1531 return $out;
1532 }
1533
1539 public function buildCSS()
1540 {
1541 if (!empty($this->css)) {
1542 // Style CSS
1543 $this->styleCSS = '<style type="text/css">';
1544 $this->styleCSS .= 'body {';
1545
1546 if ($this->css['bgcolor']) {
1547 $this->styleCSS .= ' background-color: '.$this->css['bgcolor'].';';
1548 $this->bodyCSS .= ' bgcolor="'.$this->css['bgcolor'].'"';
1549 }
1550 if ($this->css['bgimage']) {
1551 // TODO recuperer cid
1552 $this->styleCSS .= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
1553 }
1554 $this->styleCSS .= '}';
1555 $this->styleCSS .= '</style>';
1556 }
1557 }
1558
1559
1560 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1566 public function write_smtpheaders()
1567 {
1568 // phpcs:enable
1569 $out = "";
1570 $host = dol_getprefix('email');
1571
1572 // Sender
1573 //$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
1574 $out .= "From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1575 if (getDolGlobalString('MAIN_MAIL_SENDMAIL_FORCE_BA')) {
1576 $out .= "To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1577 }
1578 // Return-Path is important because it is used by SPF. Some MTA does not read Return-Path from header but from command line. See option MAIN_MAIL_ALLOW_SENDMAIL_F for that.
1579 $out .= "Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1580 if (isset($this->reply_to) && $this->reply_to) {
1581 $out .= "Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1582 }
1583 if (isset($this->errors_to) && $this->errors_to) {
1584 $out .= "Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1585 }
1586
1587 // Receiver
1588 if (isset($this->addr_cc) && $this->addr_cc) {
1589 $out .= "Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1590 }
1591 if (isset($this->addr_bcc) && $this->addr_bcc) {
1592 $out .= "Bcc: ".$this->getValidAddress($this->addr_bcc, 2).$this->eol2; // TODO Question: bcc must not be into header, only into SMTP command "RCPT TO". Does php mail support this ?
1593 }
1594
1595 // Delivery receipt
1596 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1597 $out .= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1598 }
1599
1600 //$out.= "X-Priority: 3".$this->eol2;
1601
1602 $out .= 'Date: '.date("r").$this->eol2;
1603
1604 $trackid = $this->trackid;
1605 if ($trackid) {
1606 $this->msgid = time().'.phpmail-dolibarr-'.$trackid.'@'.$host;
1607 $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2; // Uppercase seems replaced by phpmail
1608 $out .= 'X-Dolibarr-TRACKID: '.$trackid.'@'.$host.$this->eol2;
1609 } else {
1610 $this->msgid = time().'.phpmail@'.$host;
1611 $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2;
1612 }
1613
1614 // Add 'In-Reply-To:' header with the Message-Id we answer
1615 if (!empty($this->in_reply_to)) {
1616 $out .= 'In-Reply-To: <'.$this->in_reply_to.'>'.$this->eol2;
1617 }
1618 // Add 'References:' header with list of all Message-ID in thread history
1619 if (!empty($this->references)) {
1620 $out .= 'References: '.$this->references.$this->eol2;
1621 }
1622
1623 if (!empty($_SERVER['REMOTE_ADDR'])) {
1624 $out .= "X-RemoteAddr: ".$_SERVER['REMOTE_ADDR'].$this->eol2;
1625 }
1626 $out .= "X-Mailer: Dolibarr version ".DOL_VERSION." (using php mail)".$this->eol2;
1627 $out .= "Mime-Version: 1.0".$this->eol2;
1628
1629 //$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
1630
1631 $out .= "Content-Type: multipart/mixed;".$this->eol2." boundary=\"".$this->mixed_boundary."\"".$this->eol2;
1632 $out .= "Content-Transfer-Encoding: 8bit".$this->eol2; // TODO Seems to be ignored. Header is 7bit once received.
1633
1634 dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out, LOG_DEBUG);
1635 return $out;
1636 }
1637
1638
1639 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1647 public function write_mimeheaders($filename_list, $mimefilename_list)
1648 {
1649 // phpcs:enable
1650 $mimedone = 0;
1651 $out = "";
1652
1653 if (is_array($filename_list)) {
1654 $filename_list_size = count($filename_list);
1655 for ($i = 0; $i < $filename_list_size; $i++) {
1656 if ($filename_list[$i]) {
1657 if ($mimefilename_list[$i]) {
1658 $filename_list[$i] = $mimefilename_list[$i];
1659 }
1660 $out .= "X-attachments: $filename_list[$i]".$this->eol2;
1661 }
1662 }
1663 }
1664
1665 dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1666 return $out;
1667 }
1668
1669 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1676 public function write_body($msgtext)
1677 {
1678 // phpcs:enable
1679 global $conf;
1680
1681 $out = '';
1682
1683 $out .= "--".$this->mixed_boundary.$this->eol;
1684
1685 if ($this->atleastoneimage) {
1686 $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1687 $out .= $this->eol;
1688 $out .= "--".$this->alternative_boundary.$this->eol;
1689 }
1690
1691 // Make RFC821 Compliant, replace bare linefeeds
1692 $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $msgtext); // PCRE modifier /s means new lines are common chars
1693 if (getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA')) {
1694 $strContent = preg_replace("/\r\n/si", "\n", $strContent); // PCRE modifier /s means new lines are common chars
1695 }
1696
1697 $strContentAltText = '';
1698 if ($this->msgishtml) {
1699 // Similar code to forge a text from html is also in smtps.class.php
1700 $strContentAltText = preg_replace("/<br\s*[^>]*>/", " ", $strContent);
1701 // TODO We could replace <img ...> with [Filename.ext] like Gmail do.
1702 $strContentAltText = html_entity_decode(strip_tags($strContentAltText)); // Remove any HTML tags
1703 $strContentAltText = trim(wordwrap($strContentAltText, 75, !getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA') ? "\r\n" : "\n"));
1704
1705 // Check if html header already in message, if not complete the message
1706 $strContent = $this->checkIfHTML($strContent); // This add a header and a body including custom CSS to the HTML content
1707 }
1708
1709 // Make RFC2045 Compliant, split lines
1710 //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems ko if not used on a base64 content
1711 // TODO Encode main content into base64 and use the chunk_split, or quoted-printable
1712 $strContent = rtrim(wordwrap($strContent, 75, !getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA') ? "\r\n" : "\n")); // TODO Using this method creates unexpected line break on text/plain content.
1713
1714 if ($this->msgishtml) {
1715 if ($this->atleastoneimage) {
1716 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1717 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1718 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol; // Add plain text message
1719 $out .= "--".$this->alternative_boundary.$this->eol;
1720 $out .= "Content-Type: multipart/related;".$this->eol." boundary=\"".$this->related_boundary."\"".$this->eol;
1721 $out .= $this->eol;
1722 $out .= "--".$this->related_boundary.$this->eol;
1723 }
1724
1725 if (!$this->atleastoneimage && $strContentAltText && getDolGlobalString('MAIN_MAIL_USE_MULTI_PART')) { // Add plain text message part before html part
1726 $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1727 $out .= $this->eol;
1728 $out .= "--".$this->alternative_boundary.$this->eol;
1729 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1730 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1731 $out .= $this->eol.$strContentAltText.$this->eol;
1732 $out .= "--".$this->alternative_boundary.$this->eol;
1733 }
1734
1735 $out .= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1736 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol; // TODO Use base64
1737 $out .= $this->eol.$strContent.$this->eol;
1738
1739 if (!$this->atleastoneimage && $strContentAltText && getDolGlobalString('MAIN_MAIL_USE_MULTI_PART')) { // Add plain text message part after html part
1740 $out .= "--".$this->alternative_boundary."--".$this->eol;
1741 }
1742 } else {
1743 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1744 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1745 $out .= $this->eol.$strContent.$this->eol;
1746 }
1747
1748 $out .= $this->eol;
1749
1750 // Encode images
1751 if ($this->atleastoneimage) {
1752 $out .= $this->write_images($this->images_encoded);
1753 // always end related and end alternative after inline images
1754 $out .= "--".$this->related_boundary."--".$this->eol;
1755 $out .= $this->eol."--".$this->alternative_boundary."--".$this->eol;
1756 $out .= $this->eol;
1757 }
1758
1759 return $out;
1760 }
1761
1762 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1772 private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
1773 {
1774 // phpcs:enable
1775 $out = '';
1776
1777 $filename_list_size = count($filename_list);
1778 for ($i = 0; $i < $filename_list_size; $i++) {
1779 if ($filename_list[$i]) {
1780 dol_syslog("CMailFile::write_files: i=$i ".$filename_list[$i]);
1781 $encoded = $this->_encode_file($filename_list[$i]);
1782 if ($encoded !== -1) {
1783 if ($mimefilename_list[$i]) {
1784 $filename_list[$i] = $mimefilename_list[$i];
1785 }
1786 if (!$mimetype_list[$i]) {
1787 $mimetype_list[$i] = "application/octet-stream";
1788 }
1789
1790 $out .= "--".$this->mixed_boundary.$this->eol;
1791 $out .= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
1792 $out .= "Content-Type: ".$mimetype_list[$i]."; name=\"".$filename_list[$i]."\"".$this->eol;
1793 $out .= "Content-Transfer-Encoding: base64".$this->eol;
1794 $out .= "Content-Description: ".$filename_list[$i].$this->eol;
1795 if (!empty($cidlist) && is_array($cidlist) && $cidlist[$i]) {
1796 $out .= "X-Attachment-Id: ".$cidlist[$i].$this->eol;
1797 $out .= "Content-ID: <".$cidlist[$i].'>'.$this->eol;
1798 }
1799 $out .= $this->eol;
1800 $out .= $encoded;
1801 $out .= $this->eol;
1802 //$out.= $this->eol;
1803 } else {
1804 return $encoded;
1805 }
1806 }
1807 }
1808
1809 return $out;
1810 }
1811
1812
1813 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1820 public function write_images($images_list)
1821 {
1822 // phpcs:enable
1823 $out = '';
1824
1825 if (is_array($images_list)) {
1826 foreach ($images_list as $img) {
1827 dol_syslog("CMailFile::write_images: ".$img["name"]);
1828
1829 $out .= "--".$this->related_boundary.$this->eol; // always related for an inline image
1830 $out .= "Content-Type: ".$img["content_type"]."; name=\"".$img["name"]."\"".$this->eol;
1831 $out .= "Content-Transfer-Encoding: base64".$this->eol;
1832 $out .= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
1833 $out .= "Content-ID: <".$img["cid"].">".$this->eol;
1834 $out .= $this->eol;
1835 $out .= $img["image_encoded"];
1836 $out .= $this->eol;
1837 }
1838 }
1839
1840 return $out;
1841 }
1842
1843
1844 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1852 public function check_server_port($host, $port)
1853 {
1854 // phpcs:enable
1855 global $conf;
1856
1857 $_retVal = 0;
1858 $timeout = 5; // Timeout in seconds
1859
1860 if (function_exists('fsockopen')) {
1861 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
1862 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
1863 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
1864 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
1865 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE';
1866 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
1867 $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
1868 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
1869 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
1870
1871 if (!empty($this->sendcontext)) {
1872 $smtpContextKey = strtoupper($this->sendcontext);
1873 $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
1874 if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
1875 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
1876 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
1877 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
1878 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
1879 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
1880 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
1881 $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
1882 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
1883 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
1884 }
1885 }
1886
1887 // If we use SSL/TLS
1888 if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
1889 $host = 'ssl://'.$host;
1890 }
1891 // tls smtp start with no encryption
1892 //if (!empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS) && function_exists('openssl_open')) $host='tls://'.$host;
1893
1894 dol_syslog("Try socket connection to host=".$host." port=".$port." timeout=".$timeout);
1895 //See if we can connect to the SMTP server
1896 $errno = 0;
1897 $errstr = '';
1898 if ($socket = @fsockopen(
1899 $host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
1900 $port, // which Port number to use
1901 $errno, // actual system level error
1902 $errstr, // and any text that goes with the error
1903 $timeout // timeout for reading/writing data over the socket
1904 )) {
1905 // Windows still does not have support for this timeout function
1906 if (function_exists('stream_set_timeout')) {
1907 stream_set_timeout($socket, $timeout, 0);
1908 }
1909
1910 dol_syslog("Now we wait for answer 220");
1911
1912 // Check response from Server
1913 if ($_retVal = $this->server_parse($socket, "220")) {
1914 $_retVal = $socket;
1915 }
1916 } else {
1917 $this->error = utf8_check('Error '.$errno.' - '.$errstr) ? 'Error '.$errno.' - '.$errstr : mb_convert_encoding('Error '.$errno.' - '.$errstr, 'UTF-8', 'ISO-8859-1');
1918 }
1919 }
1920 return $_retVal;
1921 }
1922
1923 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1932 public function server_parse($socket, $response)
1933 {
1934 // phpcs:enable
1935 $_retVal = true; // Indicates if Object was created or not
1936 $server_response = '';
1937
1938 while (substr($server_response, 3, 1) != ' ') {
1939 if (!($server_response = fgets($socket, 256))) {
1940 $this->error = "Couldn't get mail server response codes";
1941 return false;
1942 }
1943 }
1944
1945 if (!(substr($server_response, 0, 3) == $response)) {
1946 $this->error = "Ran into problems sending Mail.\r\nResponse: $server_response";
1947 $_retVal = false;
1948 }
1949
1950 return $_retVal;
1951 }
1952
1959 private function findHtmlImages($images_dir)
1960 {
1961 // Build the array of image extensions
1962 $extensions = array_keys($this->image_types);
1963
1964 // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
1965 // For example when:
1966 // <img alt="" src="/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/picture.jpg" style="height:356px; width:1040px" />
1967 $matches = array();
1968 preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
1969
1970 if (!empty($matches) && !empty($matches[1])) {
1971 $i = 0;
1972 // We are interested in $matches[1] only (the second set of parenthesis into regex)
1973 foreach ($matches[1] as $full) {
1974 $regs = array();
1975 if (preg_match('/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) { // If xxx is 'file=aaa'
1976 $img = $regs[1];
1977
1978 if (file_exists($images_dir.'/'.$img)) {
1979 // Image path in src
1980 $src = preg_quote($full, '/');
1981 // Image full path
1982 $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
1983 // Image name
1984 $this->html_images[$i]["name"] = $img;
1985 // Content type
1986 $regext = array();
1987 if (preg_match('/^.+\.(\w{3,4})$/', $img, $regext)) {
1988 $ext = strtolower($regext[1]);
1989 $this->html_images[$i]["content_type"] = $this->image_types[$ext];
1990 }
1991 // cid
1992 $this->html_images[$i]["cid"] = dol_hash($this->html_images[$i]["fullpath"], 'md5'); // Force md5 hash (does not contain special chars)
1993 // type
1994 $this->html_images[$i]["type"] = 'cidfromurl';
1995
1996 $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
1997 }
1998 $i++;
1999 }
2000 }
2001
2002 if (!empty($this->html_images)) {
2003 $inline = array();
2004
2005 $i = 0;
2006
2007 foreach ($this->html_images as $img) {
2008 $fullpath = $images_dir.'/'.$img["name"];
2009
2010 // If duplicate images are embedded, they may show up as attachments, so remove them.
2011 if (!in_array($fullpath, $inline)) {
2012 // Read image file
2013 if ($image = file_get_contents($fullpath)) {
2014 // On garde que le nom de l'image
2015 $regs = array();
2016 preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img["name"], $regs);
2017 $imgName = $regs[1];
2018
2019 $this->images_encoded[$i]['name'] = $imgName;
2020 $this->images_encoded[$i]['fullpath'] = $fullpath;
2021 $this->images_encoded[$i]['content_type'] = $img["content_type"];
2022 $this->images_encoded[$i]['cid'] = $img["cid"];
2023 // Encodage de l'image
2024 $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
2025 $inline[] = $fullpath;
2026 }
2027 }
2028 $i++;
2029 }
2030 } else {
2031 return -1;
2032 }
2033
2034 return 1;
2035 } else {
2036 return 0;
2037 }
2038 }
2039
2047 private function findHtmlImagesIsSrcData($images_dir)
2048 {
2049 global $conf;
2050
2051 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2052
2053 // Build the array of image extensions
2054 $extensions = array_keys($this->image_types);
2055
2056 if (empty($images_dir)) {
2057 //$images_dir = $conf->admin->dir_output.'/temp/'.uniqid('cmailfile');
2058 $images_dir = $conf->admin->dir_output.'/temp/cmailfile';
2059 }
2060
2061 if ($images_dir && !dol_is_dir($images_dir)) {
2062 dol_mkdir($images_dir, DOL_DATA_ROOT);
2063 }
2064
2065 // Uncomment this for debug
2066 /*
2067 global $dolibarr_main_data_root;
2068 $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
2069 $fp = fopen($outputfile, "w+");
2070 fwrite($fp, $this->html);
2071 fclose($fp);
2072 */
2073
2074 // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
2075 // For example when:
2076 // <img alt="" src="/src="data:image....;base64,...." />
2077 $matches = array();
2078 preg_match_all('/src="data:image\/('.implode('|', $extensions).');base64,([^"]+)"/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
2079
2080 if (!empty($matches) && !empty($matches[1])) {
2081 if (empty($images_dir)) {
2082 // No temp directory provided, so we are not able to support conversion of data:image into physical images.
2083 $this->errors[] = 'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
2084 return -1;
2085 }
2086
2087 $i = count($this->html_images);
2088 foreach ($matches[1] as $key => $ext) {
2089 // We save the image to send in disk
2090 $filecontent = $matches[2][$key];
2091
2092 $cid = 'cid000'.dol_hash($filecontent, 'md5'); // The id must not change if image is same
2093
2094 $destfiletmp = $images_dir.'/'.$cid.'.'.$ext;
2095
2096 if (!dol_is_file($destfiletmp)) { // If file does not exist yet (this is the case for the first email sent with a data:image inside)
2097 dol_syslog("write the cid file ".$destfiletmp);
2098 $fhandle = @fopen($destfiletmp, 'w');
2099 if ($fhandle) {
2100 $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
2101 fclose($fhandle);
2102 dolChmod($destfiletmp);
2103 } else {
2104 $this->errors[] = "Failed to open file '".$destfiletmp."' for write";
2105 return -2;
2106 }
2107 }
2108
2109 if (file_exists($destfiletmp)) {
2110 // Image full path
2111 $this->html_images[$i]["fullpath"] = $destfiletmp;
2112 // Image name
2113 $this->html_images[$i]["name"] = basename($destfiletmp);
2114 // Content type
2115 $this->html_images[$i]["content_type"] = $this->image_types[strtolower($ext)];
2116 // cid
2117 $this->html_images[$i]["cid"] = $cid;
2118 // type
2119 $this->html_images[$i]["type"] = 'cidfromdata';
2120
2121 $this->html = str_replace('src="data:image/'.$ext.';base64,'.$filecontent.'"', 'src="cid:'.$this->html_images[$i]["cid"].'"', $this->html);
2122 }
2123 $i++;
2124 }
2125
2126 return 1;
2127 } else {
2128 return 0;
2129 }
2130 }
2131
2147 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
2148 {
2149 global $conf;
2150
2151 $ret = '';
2152
2153 $arrayaddress = (!empty($address) ? explode(',', $address) : array());
2154
2155 // Boucle sur chaque composant de l'address
2156 $i = 0;
2157 foreach ($arrayaddress as $val) {
2158 $regs = array();
2159 if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
2160 $name = trim($regs[1]);
2161 $email = trim($regs[2]);
2162 } else {
2163 $name = '';
2164 $email = trim($val);
2165 }
2166
2167 if ($email) {
2168 $i++;
2169
2170 $newemail = '';
2171 if ($format == 5) {
2172 $newemail = $name ? $name : $email;
2173 $newemail = '<a href="mailto:'.$email.'">'.$newemail.'</a>';
2174 }
2175 if ($format == 4) {
2176 $newemail = $name ? $name : $email;
2177 }
2178 if ($format == 2) {
2179 $newemail = $email;
2180 }
2181 if ($format == 1 || $format == 3) {
2182 $newemail = '<'.$email.'>';
2183 }
2184 if ($format == 0 || $format == 3) {
2185 if (getDolGlobalString('MAIN_MAIL_NO_FULL_EMAIL')) {
2186 $newemail = '<'.$email.'>';
2187 } elseif (!$name) {
2188 $newemail = '<'.$email.'>';
2189 } else {
2190 $newemail = ($format == 3 ? '"' : '').($encode ? self::encodetorfc2822($name) : $name).($format == 3 ? '"' : '').' <'.$email.'>';
2191 }
2192 }
2193
2194 $ret = ($ret ? $ret.',' : '').$newemail;
2195
2196 // Stop if we have too much records
2197 if ($maxnumberofemail && $i >= $maxnumberofemail) {
2198 if (count($arrayaddress) > $maxnumberofemail) {
2199 $ret .= '...';
2200 }
2201 break;
2202 }
2203 }
2204 }
2205
2206 return $ret;
2207 }
2208
2216 public static function getArrayAddress($address)
2217 {
2218 $ret = array();
2219
2220 $arrayaddress = explode(',', $address);
2221
2222 // Boucle sur chaque composant de l'address
2223 foreach ($arrayaddress as $val) {
2224 $regs = array();
2225 if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
2226 $name = trim($regs[1]);
2227 $email = trim($regs[2]);
2228 } else {
2229 $name = null;
2230 $email = trim($val);
2231 }
2232
2233 $ret[$email] = getDolGlobalString('MAIN_MAIL_NO_FULL_EMAIL') ? null : $name;
2234 }
2235
2236 return $ret;
2237 }
2238}
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
_encode_file($sourcefile)
Read a file on disk and return encoded content for emails (mode = 'mail')
write_body($msgtext)
Return email content (mode = 'mail')
__construct($subject, $to, $from, $msg, $filename_list=array(), $mimetype_list=array(), $mimefilename_list=array(), $addr_cc="", $addr_bcc="", $deliveryreceipt=0, $msgishtml=0, $errors_to='', $css='', $trackid='', $moreinheader='', $sendcontext='standard', $replyto='', $upload_dir_tmp='', $in_reply_to='', $references='')
CMailFile.
$bodyCSS
Defined background directly in body tag.
dump_mail()
Write content of a SMTP request into a dump file (mode = all) Used for debugging.
sendfile()
Send mail that was prepared by constructor.
save_dump_mail_in_err($message='')
Save content if mail is in error Used for debugging.
static encodetorfc2822($stringtoencode)
Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word.
checkIfHTML($msg)
Correct an incomplete html string.
static getValidAddress($address, $format, $encode=0, $maxnumberofemail=0)
Return a formatted address string for SMTP protocol.
write_images($images_list)
Attach an image to email (mode = 'mail')
server_parse($socket, $response)
This function has been modified as provided by SirSir to allow multiline responses when using SMTP Ex...
write_smtpheaders()
Create SMTP headers (mode = 'mail')
findHtmlImagesIsSrcData($images_dir)
Seearch images with data:image format into html message.
$styleCSS
Defined css style for body background.
write_mimeheaders($filename_list, $mimefilename_list)
Create header MIME (mode = 'mail')
check_server_port($host, $port)
Try to create a socket connection.
buildCSS()
Build a css style (mode = all) into this->styleCSS and this->bodyCSS.
write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
Attach file to email (mode = 'mail')
static getArrayAddress($address)
Return a formatted array of address string for SMTP protocol.
findHtmlImages($images_dir)
Search images into html message and init array this->images_encoded if found.
Class to manage hooks.
Class to construct and send SMTP compliant email, even to a secure SMTP server, regardless of platfor...
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array())
Move a file into another name.
dol_is_file($pathoffile)
Return if path is a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
ascii_check($str)
Check if a string is in ASCII.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_sanitizeEmail($stringtoclean)
Clean a string to use it as an Email.
getSupportedOauth2Array()
Return array of tabs to used on pages to setup cron module.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.