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