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