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
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 $oauthname = explode('-', $OAUTH_SERVICENAME);
1177 // ex service is Google-Emails we need only the first part Google
1178 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1179
1180 // We have to save the refresh token because Google give it only once
1181 $refreshtoken = $tokenobj->getRefreshToken();
1182
1183 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1184 // ServiceInterface does not provide refreshAccessToken, AbstractService does
1185 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1186 $tokenobj->setRefreshToken($refreshtoken); // Restore the refresh token
1187 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1188 }
1189
1190 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1191 }
1192
1193 if (is_object($tokenobj)) {
1194 $this->smtps->setToken($tokenobj->getAccessToken());
1195 } else {
1196 $this->error = "Token not found";
1197 }
1198 } catch (Exception $e) {
1199 // Return an error if token not found
1200 $this->error = $e->getMessage();
1201 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1202 }
1203 }
1204
1205 $res = true;
1206 $from = $this->smtps->getFrom('org');
1207 if ($res && !$from) {
1208 $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - Sender address '$from' invalid";
1209 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1210 $res = false;
1211 }
1212 $dest = $this->smtps->getTo();
1213 if ($res && !$dest) {
1214 $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - Recipient address '$dest' invalid";
1215 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1216 $res = false;
1217 }
1218
1219 if ($res) {
1220 dol_syslog("CMailFile::sendfile: sendMsg, HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport), LOG_NOTICE);
1221
1222 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1223 $this->smtps->setDebug(true);
1224 }
1225
1226 $result = $this->smtps->sendMsg();
1227
1228 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1229 $this->dump_mail(); // Create file dolibarr_mail.log or dolibarr_mail.log.vXXX if option for archive is on
1230 }
1231
1232 $smtperrorcode = 0;
1233 if (! $result) {
1234 $smtperrorcode = $this->smtps->lastretval; // SMTP error code
1235 dol_syslog("CMailFile::sendfile: mail SMTP error code ".$smtperrorcode, LOG_WARNING);
1236
1237 if ($smtperrorcode == '421') { // Try later
1238 // TODO Add a delay and try again
1239 /*
1240 dol_syslog("CMailFile::sendfile: Try later error, so we wait and we retry");
1241 sleep(2);
1242
1243 $result = $this->smtps->sendMsg();
1244
1245 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1246 $this->dump_mail();
1247 }
1248 */
1249 }
1250 } else {
1251 dol_syslog("CMailFile::sendfile: mail SMTP sendMsg is success", LOG_DEBUG);
1252 }
1253
1254 $result = $this->smtps->getErrors(); // applicative error code (not SMTP error code)
1255
1256 if (empty($this->error) && empty($result)) {
1257 dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
1258 $res = true;
1259 } else {
1260 if (empty($this->error)) {
1261 $this->error = $result;
1262 }
1263 dol_syslog("CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport)." - ".$this->error, LOG_ERR);
1264 $res = false;
1265
1266 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1267 $this->save_dump_mail_in_err('Mail smtp error '.$smtperrorcode.' with topic '.$this->subject.' - '.$this->error);
1268 }
1269 }
1270 }
1271 } elseif ($this->sendmode == 'swiftmailer') {
1272 // Use Swift Mailer library
1273 // ------------------------------------------
1274 require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
1275
1276 // Clean parameters
1277 if (empty($conf->global->$keyforsmtpserver)) {
1278 $conf->global->$keyforsmtpserver = ini_get('SMTP');
1279 }
1280 if (empty($conf->global->$keyforsmtpport)) {
1281 $conf->global->$keyforsmtpport = ini_get('smtp_port');
1282 }
1283
1284 // If we use SSL/TLS
1285 $server = getDolGlobalString($keyforsmtpserver);
1286 $secure = '';
1287 if (getDolGlobalString($keyfortls) && function_exists('openssl_open')) {
1288 $secure = 'ssl';
1289 }
1290 if (getDolGlobalString($keyforstarttls) && function_exists('openssl_open')) {
1291 $secure = 'tls';
1292 }
1293
1294 $this->transport = new Swift_SmtpTransport($server, getDolGlobalInt($keyforsmtpport), $secure);
1295
1296 if (getDolGlobalString($keyforsmtpid)) {
1297 $this->transport->setUsername(getDolGlobalString($keyforsmtpid));
1298 }
1299 if (getDolGlobalString($keyforsmtppw) && getDolGlobalString($keyforsmtpauthtype) != "XOAUTH2") {
1300 $this->transport->setPassword(getDolGlobalString($keyforsmtppw));
1301 }
1302 if (getDolGlobalString($keyforsmtpauthtype) === "XOAUTH2") {
1303 require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
1304
1305 $supportedoauth2array = getSupportedOauth2Array();
1306
1307 $keyforsupportedoauth2array = getDolGlobalString($keyforsmtpoauthservice);
1308 if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
1309 $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
1310 } else {
1311 $keyforprovider = '';
1312 }
1313 $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
1314 $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
1315
1316 if (!empty($supportedoauth2array)) {
1317 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']));
1318 $nameofservice .= ($keyforprovider ? '-'.$keyforprovider : '');
1319 $OAUTH_SERVICENAME = $nameofservice;
1320 } else {
1321 $OAUTH_SERVICENAME = 'Unknown';
1322 }
1323
1324 $keyforparamtenant = 'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']) ? 'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array]['callbackfile']).($keyforprovider ? '-'.$keyforprovider : '').'_TENANT';
1325
1326 require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
1327
1328 $storage = new DoliStorage($db, $conf, $keyforprovider, getDolGlobalString($keyforparamtenant));
1329
1330 try {
1331 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1332
1333 $expire = false;
1334 // Is token expired or will token expire in the next 30 seconds
1335 if (is_object($tokenobj)) {
1336 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
1337 }
1338 // Token expired so we refresh it
1339 if (is_object($tokenobj) && $expire) {
1340 $credentials = new Credentials(
1341 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_ID'),
1342 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_SECRET'),
1343 getDolGlobalString('OAUTH_'.getDolGlobalString($keyforsmtpoauthservice).'_URLCALLBACK')
1344 );
1345 $serviceFactory = new \OAuth\ServiceFactory();
1346 $oauthname = explode('-', $OAUTH_SERVICENAME);
1347 // ex service is Google-Emails we need only the first part Google
1348 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
1349 $refreshtoken = $tokenobj->getRefreshToken();
1350
1351 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
1352 // ServiceInterface does not provide refreshAccessToken, AbstractService does
1353 // We must save the token because Google provides it only once
1354 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1355 $tokenobj->setRefreshToken($refreshtoken);
1356 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1357
1358 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1359 }
1360 }
1361
1362 if (is_object($tokenobj)) {
1363 $this->transport->setAuthMode('XOAUTH2');
1364 $this->transport->setPassword($tokenobj->getAccessToken());
1365 } else {
1366 $this->errors[] = "Token not found";
1367 }
1368 } catch (Exception $e) {
1369 // Return an error if token not found
1370 $this->errors[] = $e->getMessage();
1371 dol_syslog("CMailFile::sendfile: mail end error=".$e->getMessage(), LOG_ERR);
1372 }
1373 }
1374 if (getDolGlobalString($keyforsslseflsigned)) {
1375 $this->transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
1376 }
1377 //$smtps->_msgReplyTo = 'reply@web.com';
1378
1379 // Switch content encoding to base64 - avoid the doubledot issue with quoted-printable
1380 $contentEncoderBase64 = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
1381 $this->message->setEncoder($contentEncoderBase64);
1382
1383 // Create the Mailer using your created Transport
1384 $this->mailer = new Swift_Mailer($this->transport);
1385
1386 // DKIM SIGN
1387 if (getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_ENABLED')) {
1388 $privateKey = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY');
1389 $domainName = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_DOMAIN');
1390 $selector = getDolGlobalString('MAIN_MAIL_EMAIL_DKIM_SELECTOR');
1391 $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
1392 $this->message->attachSigner($signer->ignoreHeader('Return-Path'));
1393 }
1394
1395 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1396 // To use the ArrayLogger
1397 $this->logger = new Swift_Plugins_Loggers_ArrayLogger();
1398 // Or to use the Echo Logger
1399 //$this->logger = new Swift_Plugins_Loggers_EchoLogger();
1400 $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->logger));
1401 }
1402
1403 dol_syslog("CMailFile::sendfile: mailer->send, HOST=".$server.", PORT=" . getDolGlobalString($keyforsmtpport), LOG_NOTICE);
1404
1405 // send mail
1406 $failedRecipients = array();
1407 try {
1408 $result = $this->mailer->send($this->message, $failedRecipients);
1409 } catch (Exception $e) {
1410 $this->errors[] = $e->getMessage();
1411 }
1412 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1413 $this->dump_mail();
1414 }
1415
1416 $res = true;
1417 if (!empty($this->error) || !empty($this->errors) || !$result) {
1418 if (!empty($failedRecipients)) {
1419 $this->error = 'Transport failed for the following addresses: "' . implode('", "', $failedRecipients) . '".';
1420 $this->errors[] = $this->error;
1421 }
1422 dol_syslog("CMailFile::sendfile: mail end error=". implode(' ', $this->errors), LOG_ERR);
1423 $res = false;
1424
1425 if (getDolGlobalString('MAIN_MAIL_DEBUG')) {
1426 $this->save_dump_mail_in_err('Mail with topic '.$this->subject);
1427 }
1428 } else {
1429 dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
1430 }
1431 } else {
1432 // Send mail method not correctly defined
1433 // --------------------------------------
1434
1435 $this->error = 'Bad value for sendmode';
1436 return false;
1437 }
1438
1439 // Now we delete image files that were created dynamically to manage data inline files
1440 /* Note: dol_delete call was disabled, so code commented to not trigger empty if body
1441 foreach ($this->html_images as $val) {
1442 if (!empty($val['type']) && $val['type'] == 'cidfromdata') {
1443 //dol_delete($val['fullpath']);
1444 }
1445 }
1446 */
1447
1448 $parameters = array('sent' => $res);
1449 $action = '';
1450 $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1451 if ($reshook < 0) {
1452 $this->error = "Error in hook maildao sendMailAfter ".$reshook;
1453 dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1454
1455 return false;
1456 }
1457 } else {
1458 $this->error = 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
1459 dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
1460 }
1461
1462 error_reporting($errorlevel); // Reactive niveau erreur origine
1463 return $res;
1464 }
1465
1472 public static function encodetorfc2822($stringtoencode)
1473 {
1474 global $conf;
1475 return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
1476 }
1477
1478 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1485 private function _encode_file($sourcefile)
1486 {
1487 // phpcs:enable
1488 $newsourcefile = dol_osencode($sourcefile);
1489
1490 if (is_readable($newsourcefile)) {
1491 $contents = file_get_contents($newsourcefile); // Need PHP 4.3
1492 $encoded = chunk_split(base64_encode($contents), 76, $this->eol); // 76 max is defined into http://tools.ietf.org/html/rfc2047
1493 return $encoded;
1494 } else {
1495 $this->error = "Error in _encode_file() method: Can't read file '".$sourcefile."'";
1496 dol_syslog("CMailFile::_encode_file: ".$this->error, LOG_ERR);
1497 return -1;
1498 }
1499 }
1500
1501
1502 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1510 public function dump_mail()
1511 {
1512 // phpcs:enable
1513 global $dolibarr_main_data_root;
1514
1515 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
1516 $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
1517 $fp = fopen($outputfile, "w"); // overwrite
1518
1519 if ($fp) {
1520 if ($this->sendmode == 'mail') {
1521 fwrite($fp, $this->headers);
1522 fwrite($fp, $this->eol); // This eol is added by the mail function, so we add it in log
1523 fwrite($fp, $this->message);
1524 } elseif ($this->sendmode == 'smtps') {
1525 fwrite($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
1526 } elseif ($this->sendmode == 'swiftmailer') {
1527 fwrite($fp, "smtpheader=\n".$this->message->getHeaders()->toString()."\n");
1528 fwrite($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
1529 }
1530
1531 fclose($fp);
1532 dolChmod($outputfile);
1533
1534 // Move dolibarr_mail.log into a dolibarr_mail.log.v123456789
1535 if (getDolGlobalInt('MAIN_MAIL_DEBUG_LOG_WITH_DATE')) {
1536 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1537 archiveOrBackupFile($outputfile, getDolGlobalInt('MAIN_MAIL_DEBUG_LOG_WITH_DATE'));
1538 }
1539 }
1540 }
1541 }
1542
1543 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1551 public function save_dump_mail_in_err($message = '')
1552 {
1553 global $dolibarr_main_data_root;
1554
1555 if (@is_writable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
1556 $srcfile = $dolibarr_main_data_root."/dolibarr_mail.log";
1557
1558 // Add message to dolibarr_mail.log. We do not use dol_syslog() on purpose,
1559 // to be sure to write into dolibarr_mail.log
1560 if ($message) {
1561 // Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
1562 if (defined('SYSLOG_FILE_NO_ERROR')) {
1563 $filefd = @fopen($srcfile, 'a+');
1564 } else {
1565 $filefd = fopen($srcfile, 'a+');
1566 }
1567 if ($filefd) {
1568 fwrite($filefd, $message."\n");
1569 fclose($filefd);
1570 dolChmod($srcfile);
1571 }
1572 }
1573
1574 // Move dolibarr_mail.log into a dolibarr_mail.err or dolibarr_mail.date.err
1575 if (getDolGlobalString('MAIN_MAIL_DEBUG_ERR_WITH_DATE')) {
1576 $destfile = $dolibarr_main_data_root."/dolibarr_mail.".dol_print_date(dol_now(), 'dayhourlog', 'gmt').".err";
1577 } else {
1578 $destfile = $dolibarr_main_data_root."/dolibarr_mail.err";
1579 }
1580
1581 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1582 dol_move($srcfile, $destfile, '0', 1, 0, 0);
1583 }
1584 }
1585
1586
1593 public function checkIfHTML($msg)
1594 {
1595 if (!preg_match('/^[\s\t]*<html/i', $msg)) {
1596 $out = "<html><head><title></title>";
1597 if (!empty($this->styleCSS)) {
1598 $out .= $this->styleCSS;
1599 }
1600 $out .= "</head><body";
1601 if (!empty($this->bodyCSS)) {
1602 $out .= $this->bodyCSS;
1603 }
1604 $out .= ">";
1605 $out .= $msg;
1606 $out .= "</body></html>";
1607 } else {
1608 $out = $msg;
1609 }
1610
1611 return $out;
1612 }
1613
1619 public function buildCSS()
1620 {
1621 if (!empty($this->css)) {
1622 // Style CSS
1623 $this->styleCSS = '<style type="text/css">';
1624 $this->styleCSS .= 'body {';
1625
1626 if ($this->css['bgcolor']) {
1627 $this->styleCSS .= ' background-color: '.$this->css['bgcolor'].';';
1628 $this->bodyCSS .= ' bgcolor="'.$this->css['bgcolor'].'"';
1629 }
1630 if ($this->css['bgimage']) {
1631 // TODO recuperer cid
1632 $this->styleCSS .= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
1633 }
1634 $this->styleCSS .= '}';
1635 $this->styleCSS .= '</style>';
1636 }
1637 }
1638
1639
1640 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1646 public function write_smtpheaders()
1647 {
1648 // phpcs:enable
1649 $out = "";
1650 $host = dol_getprefix('email');
1651
1652 // Sender
1653 //$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
1654 $out .= "From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1655 if (getDolGlobalString('MAIN_MAIL_SENDMAIL_FORCE_BA')) {
1656 $out .= "To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1657 }
1658 if (!getDolGlobalString('MAIN_MAIL_NO_RETURN_PATH_FOR_MODE_MAIL')) {
1659 // Return-Path is important because it is used by SPF. Some command line MTA overwrites the Return-Path, even if already in the
1660 // 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.
1661 // Return-Path is used for bounced emails. If not set (most cases), the From is used.
1662 $out .= "Return-Path: ".$this->getValidAddress($this->addr_from, 1, 1).$this->eol2;
1663 }
1664 if (isset($this->reply_to) && $this->reply_to) {
1665 $out .= "Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1666 }
1667 if (isset($this->errors_to) && $this->errors_to) {
1668 $out .= "Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1669 }
1670
1671 // Receiver
1672 if (isset($this->addr_cc) && $this->addr_cc) {
1673 $out .= "Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1674 }
1675 if (isset($this->addr_bcc) && $this->addr_bcc) {
1676 $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 ?
1677 }
1678
1679 // Delivery receipt
1680 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1681 $out .= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1682 }
1683
1684 //$out.= "X-Priority: 3".$this->eol2;
1685 $out .= 'Date: '.date("r").$this->eol2;
1686
1687 $trackid = $this->trackid;
1688 if ($trackid) {
1689 $this->msgid = time().'.'.mt_rand(100, 999).'.phpmail-dolibarr-'.$trackid.'@'.$host;
1690 $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2; // Uppercase seems replaced by phpmail
1691 $out .= 'X-Dolibarr-TRACKID: '.$trackid.'@'.$host.$this->eol2;
1692 } else {
1693 $this->msgid = time().'.'.mt_rand(100, 999).'.phpmail@'.$host;
1694 $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2;
1695 }
1696
1697 // Add 'In-Reply-To:' header with the Message-Id we answer
1698 if (!empty($this->in_reply_to)) {
1699 $out .= 'In-Reply-To: <'.$this->in_reply_to.'>'.$this->eol2;
1700 }
1701 // Add 'References:' header with list of all Message-ID in thread history
1702 if (!empty($this->references)) {
1703 $out .= 'References: '.$this->references.$this->eol2;
1704 }
1705
1706 if (!empty($_SERVER['REMOTE_ADDR'])) {
1707 $out .= "X-RemoteAddr: ".$_SERVER['REMOTE_ADDR'].$this->eol2;
1708 }
1709 $out .= "X-Mailer: Dolibarr version ".DOL_VERSION." (using php mail)".$this->eol2;
1710 $out .= "Mime-Version: 1.0".$this->eol2;
1711
1712 //$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
1713
1714 $out .= "Content-Type: multipart/mixed;".$this->eol2." boundary=\"".$this->mixed_boundary."\"".$this->eol2;
1715 if (!getDolGlobalString("MAIN_EMAIL_DISABLE_ADD_CONTENT_ENCODING_8BIT")) {
1716 $out .= "Content-Transfer-Encoding: 8bit".$this->eol2; // TODO Seems to be ignored. Header is 7bit once received.
1717 }
1718
1719 dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out, LOG_DEBUG);
1720 return $out;
1721 }
1722
1723
1724 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1732 public function write_mimeheaders($filename_list, $mimefilename_list)
1733 {
1734 // phpcs:enable
1735 $out = "";
1736
1737 if (is_array($filename_list) && is_array($mimefilename_list)) {
1738 $filename_list_size = count($filename_list);
1739 for ($i = 0; $i < $filename_list_size; $i++) {
1740 if ($filename_list[$i]) {
1741 if ($mimefilename_list[$i]) {
1742 $filename_list[$i] = $mimefilename_list[$i];
1743 }
1744 $out .= "X-attachments: $filename_list[$i]".$this->eol2;
1745 }
1746 }
1747 }
1748
1749 dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1750 return $out;
1751 }
1752
1753 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1760 public function write_body($msgtext)
1761 {
1762 // phpcs:enable
1763 global $conf;
1764
1765 $out = '';
1766
1767 $out .= "--".$this->mixed_boundary.$this->eol;
1768
1769 if ($this->atleastoneimage) {
1770 $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1771 $out .= $this->eol;
1772 $out .= "--".$this->alternative_boundary.$this->eol;
1773 }
1774
1775 // Make RFC821 Compliant, replace bare linefeeds
1776 $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $msgtext); // PCRE modifier /s means new lines are common chars
1777 if (getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA')) {
1778 $strContent = preg_replace("/\r\n/si", "\n", $strContent); // PCRE modifier /s means new lines are common chars
1779 }
1780
1781 $strContentAltText = '';
1782 if ($this->msgishtml) {
1783 // Similar code to forge a text from html is also in smtps.class.php
1784 $strContentAltText = preg_replace("/<br\s*[^>]*>/", " ", $strContent);
1785 // TODO We could replace <img ...> with [Filename.ext] like Gmail do.
1786 $strContentAltText = html_entity_decode(strip_tags($strContentAltText)); // Remove any HTML tags
1787 $strContentAltText = trim(wordwrap($strContentAltText, 75, !getDolGlobalString('MAIN_FIX_FOR_BUGGED_MTA') ? "\r\n" : "\n"));
1788
1789 // Check if html header already in message, if not complete the message
1790 $strContent = $this->checkIfHTML($strContent); // This add a header and a body including custom CSS to the HTML content
1791 }
1792
1793 // Make RFC2045 Compliant, split lines
1794 //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems ko if not used on a base64 content
1795 // TODO Encode main content into base64 and use the chunk_split, or quoted-printable
1796 $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.
1797
1798 if ($this->msgishtml) {
1799 if ($this->atleastoneimage) {
1800 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1801 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1802 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol; // Add plain text message
1803 $out .= "--".$this->alternative_boundary.$this->eol;
1804 $out .= "Content-Type: multipart/related;".$this->eol." boundary=\"".$this->related_boundary."\"".$this->eol;
1805 $out .= $this->eol;
1806 $out .= "--".$this->related_boundary.$this->eol;
1807 }
1808
1809 if (!$this->atleastoneimage && $strContentAltText && getDolGlobalString('MAIN_MAIL_USE_MULTI_PART')) { // Add plain text message part before html part
1810 $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1811 $out .= $this->eol;
1812 $out .= "--".$this->alternative_boundary.$this->eol;
1813 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1814 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1815 $out .= $this->eol.$strContentAltText.$this->eol;
1816 $out .= "--".$this->alternative_boundary.$this->eol;
1817 }
1818
1819 $out .= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1820 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol; // TODO Use base64
1821 $out .= $this->eol.$strContent.$this->eol;
1822
1823 if (!$this->atleastoneimage && $strContentAltText && getDolGlobalString('MAIN_MAIL_USE_MULTI_PART')) { // Add plain text message part after html part
1824 $out .= "--".$this->alternative_boundary."--".$this->eol;
1825 }
1826 } else {
1827 $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1828 //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1829 $out .= $this->eol.$strContent.$this->eol;
1830 }
1831
1832 $out .= $this->eol;
1833
1834 // Encode images
1835 if ($this->atleastoneimage) {
1836 $out .= $this->write_images($this->images_encoded);
1837 // always end related and end alternative after inline images
1838 $out .= "--".$this->related_boundary."--".$this->eol;
1839 $out .= $this->eol."--".$this->alternative_boundary."--".$this->eol;
1840 $out .= $this->eol;
1841 }
1842
1843 return $out;
1844 }
1845
1846 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1856 private function write_files($filename_list, $mimetype_list, $mimefilename_list, $cidlist)
1857 {
1858 // phpcs:enable
1859 $out = '';
1860
1861 $filename_list_size = count($filename_list);
1862 for ($i = 0; $i < $filename_list_size; $i++) {
1863 if ($filename_list[$i]) {
1864 dol_syslog("CMailFile::write_files: i=$i ".$filename_list[$i]);
1865 $encoded = $this->_encode_file($filename_list[$i]);
1866 if ($encoded !== -1) {
1867 if ($mimefilename_list[$i]) {
1868 $filename_list[$i] = $mimefilename_list[$i];
1869 }
1870 if (!$mimetype_list[$i]) {
1871 $mimetype_list[$i] = "application/octet-stream";
1872 }
1873
1874 // Skip files that have a CID (they will be added as inline images instead)
1875 // @phpstan-ignore-next-line
1876 if (!empty($cidlist) && is_array($cidlist) && isset($cidlist[$i]) && $cidlist[$i] !== null && $cidlist[$i] !== '') {
1877 continue; // Skip this file as it will be processed as inline image
1878 }
1879
1880 $out .= "--".$this->mixed_boundary.$this->eol;
1881
1882 $out .= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
1883 $out .= "Content-Type: ".$mimetype_list[$i]."; name=\"".$filename_list[$i]."\"".$this->eol;
1884 $out .= "Content-Transfer-Encoding: base64".$this->eol;
1885 $out .= "Content-Description: ".$filename_list[$i].$this->eol;
1886 $out .= $this->eol;
1887 $out .= $encoded;
1888 $out .= $this->eol;
1889 //$out.= $this->eol;
1890 } else {
1891 return $encoded;
1892 }
1893 }
1894 }
1895
1896 return $out;
1897 }
1898
1899
1900 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1907 public function write_images($images_list)
1908 {
1909 // phpcs:enable
1910 $out = '';
1911
1912 if (is_array($images_list)) {
1913 foreach ($images_list as $img) {
1914 dol_syslog("CMailFile::write_images: ".$img["name"]);
1915
1916 $out .= "--".$this->related_boundary.$this->eol; // always related for an inline image
1917 $out .= "Content-Type: ".$img["content_type"]."; name=\"".$img["name"]."\"".$this->eol;
1918 $out .= "Content-Transfer-Encoding: base64".$this->eol;
1919 $out .= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
1920 $out .= "Content-ID: <".$img["cid"].">".$this->eol;
1921 $out .= $this->eol;
1922 $out .= $img["image_encoded"];
1923 $out .= $this->eol;
1924 }
1925 }
1926
1927 return $out;
1928 }
1929
1930
1931 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1939 public function check_server_port($host, $port)
1940 {
1941 // phpcs:enable
1942 global $conf;
1943
1944 $_retVal = 0;
1945 $timeout = 5; // Timeout in seconds
1946
1947 if (function_exists('fsockopen')) {
1948 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
1949 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
1950 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
1951 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
1952 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE';
1953 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE';
1954 $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
1955 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
1956 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
1957
1958 if (!empty($this->sendcontext)) {
1959 $smtpContextKey = strtoupper($this->sendcontext);
1960 $smtpContextSendMode = getDolGlobalString('MAIN_MAIL_SENDMODE_'.$smtpContextKey);
1961 if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
1962 $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
1963 $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
1964 $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
1965 $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
1966 $keyforsmtpauthtype = 'MAIN_MAIL_SMTPS_AUTH_TYPE_'.$smtpContextKey;
1967 $keyforsmtpoauthservice = 'MAIN_MAIL_SMTPS_OAUTH_SERVICE_'.$smtpContextKey;
1968 $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
1969 $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
1970 $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
1971 }
1972 }
1973
1974 // If we use SSL/TLS
1975 if (getDolGlobalString($keyfortls) && function_exists('openssl_open')) {
1976 $host = 'ssl://'.$host;
1977 }
1978 // tls smtp start with no encryption
1979 //if (getDolGlobalString('MAIN_MAIL_EMAIL_STARTTLS') && function_exists('openssl_open')) $host='tls://'.$host;
1980
1981 dol_syslog("Try socket connection to host=".$host." port=".$port." timeout=".$timeout);
1982 //See if we can connect to the SMTP server
1983 $errno = 0;
1984 $errstr = '';
1985 if ($socket = @fsockopen(
1986 $host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
1987 $port, // which Port number to use
1988 $errno, // actual system level error
1989 $errstr, // and any text that goes with the error
1990 $timeout // timeout for reading/writing data over the socket
1991 )) {
1992 // Windows still does not have support for this timeout function
1993 if (function_exists('stream_set_timeout')) {
1994 stream_set_timeout($socket, $timeout, 0);
1995 }
1996
1997 dol_syslog("Now we wait for answer 220");
1998
1999 // Check response from Server
2000 if ($_retVal = $this->server_parse($socket, "220")) {
2001 $_retVal = $socket;
2002 } else {
2003 $this->error = ($this->error ? $this->error." - " : "")."Succeed in opening socket but answer 220 not received";
2004 }
2005 } else {
2006 $this->error = utf8_check('Error '.$errno.' - '.$errstr) ? 'Error '.$errno.' - '.$errstr : mb_convert_encoding('Error '.$errno.' - '.$errstr, 'UTF-8', 'ISO-8859-1');
2007 }
2008 }
2009
2010 return $_retVal;
2011 }
2012
2013 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2022 public function server_parse($socket, $response)
2023 {
2024 // phpcs:enable
2025 $_retVal = true; // Indicates if Object was created or not
2026 $server_response = '';
2027
2028 while (substr($server_response, 3, 1) != ' ') {
2029 if (!($server_response = fgets($socket, 256))) {
2030 $this->error = "Couldn't get mail server response codes";
2031 return false;
2032 }
2033 }
2034
2035 if (!(substr($server_response, 0, 3) == $response)) {
2036 $this->error = "Ran into problems sending Mail.\r\nResponse: $server_response";
2037 $_retVal = false;
2038 }
2039
2040 return $_retVal;
2041 }
2042
2049 private function findHtmlImages($images_dir)
2050 {
2051 // Build the array of image extensions
2052 $extensions = array_keys($this->image_types);
2053
2054 // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
2055 // For example when:
2056 // <img alt="" src="/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/picture.jpg" style="height:356px; width:1040px" />
2057 $matches = array();
2058 preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
2059
2060 if (!empty($matches) && !empty($matches[1])) {
2061 $i = 0;
2062 // We are interested in $matches[1] only (the second set of parenthesis into regex)
2063 foreach ($matches[1] as $full) {
2064 $full = urldecode($full);
2065
2066 $regs = array();
2067 if (preg_match('/file=([A-Za-z0-9_\-\/ ]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) { // If xxx is 'file=aaa'
2068 $img = $regs[1];
2069
2070 if (file_exists($images_dir.'/'.$img)) {
2071 // Image path in src
2072 $src = preg_quote($full, '/');
2073 // Image full path
2074 $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
2075 // Image name
2076 $this->html_images[$i]["name"] = $img;
2077 // Content type
2078 $regext = array();
2079 if (preg_match('/^.+\.(\w{3,4})$/', $img, $regext)) {
2080 $ext = strtolower($regext[1]);
2081 $this->html_images[$i]["content_type"] = $this->image_types[$ext];
2082 }
2083 // cid
2084 $this->html_images[$i]["cid"] = dol_hash($this->html_images[$i]["fullpath"], 'md5'); // Force md5 hash (does not contain special chars)
2085 // type
2086 $this->html_images[$i]["type"] = 'cidfromurl';
2087
2088 $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
2089 }
2090 $i++;
2091 }
2092 }
2093
2094 if (!empty($this->html_images)) {
2095 $inline = array();
2096
2097 $i = 0;
2098
2099 foreach ($this->html_images as $img) {
2100 $fullpath = $images_dir.'/'.$img["name"];
2101
2102 // If duplicate images are embedded, they may show up as attachments, so remove them.
2103 if (!in_array($fullpath, $inline)) {
2104 // Read image file
2105 if ($image = file_get_contents($fullpath)) {
2106 // On garde que le nom de l'image
2107 $regs = array();
2108 preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img["name"], $regs);
2109 $imgName = $regs[1];
2110 $this->images_encoded[$i]['name'] = $imgName;
2111 $this->images_encoded[$i]['fullpath'] = $fullpath;
2112 $this->images_encoded[$i]['content_type'] = $img["content_type"];
2113 $this->images_encoded[$i]['cid'] = $img["cid"];
2114 // Encodage de l'image
2115 $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
2116 $inline[] = $fullpath;
2117 }
2118 }
2119 $i++;
2120 }
2121 } else {
2122 return -1;
2123 }
2124
2125 return 1;
2126 } else {
2127 return 0;
2128 }
2129 }
2130
2138 private function findHtmlImagesIsSrcData($images_dir)
2139 {
2140 global $conf;
2141
2142 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2143
2144 // Build the array of image extensions
2145 $extensions = array_keys($this->image_types);
2146
2147 if (empty($images_dir)) {
2148 //$images_dir = $conf->admin->dir_output.'/temp/'.uniqid('cmailfile');
2149 $images_dir = $conf->admin->dir_output.'/temp/cmailfile';
2150 }
2151
2152 if ($images_dir && !dol_is_dir($images_dir)) {
2153 dol_mkdir($images_dir, DOL_DATA_ROOT);
2154 }
2155
2156 // Uncomment this for debug
2157 /*
2158 global $dolibarr_main_data_root;
2159 $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
2160 $fp = fopen($outputfile, "w+");
2161 fwrite($fp, $this->html);
2162 fclose($fp);
2163 */
2164
2165 // We search (into mail body this->html), if we find some strings like "... file=xxx.img"
2166 // For example when:
2167 // <img alt="" src="/src="data:image....;base64,...." />
2168 $matches = array();
2169 preg_match_all('/src="data:image\/('.implode('|', $extensions).');base64,([^"]+)"/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
2170
2171 if (!empty($matches) && !empty($matches[1])) {
2172 if (empty($images_dir)) {
2173 // No temp directory provided, so we are not able to support conversion of data:image into physical images.
2174 $this->errors[] = 'NoTempDirProvidedInCMailConstructorSoCantConvertDataImgOnDisk';
2175 return -1;
2176 }
2177
2178 $i = count($this->html_images);
2179 foreach ($matches[1] as $key => $ext) {
2180 // We save the image to send in disk
2181 $filecontent = $matches[2][$key];
2182
2183 $cid = 'cid000'.dol_hash($filecontent, 'md5'); // The id must not change if image is same
2184
2185 $destfiletmp = $images_dir.'/'.$cid.'.'.$ext;
2186
2187 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)
2188 dol_syslog("write the cid file ".$destfiletmp);
2189 $fhandle = @fopen($destfiletmp, 'w');
2190 if ($fhandle) {
2191 $nbofbyteswrote = fwrite($fhandle, base64_decode($filecontent));
2192 fclose($fhandle);
2193 dolChmod($destfiletmp);
2194 } else {
2195 $this->errors[] = "Failed to open file '".$destfiletmp."' for write";
2196 return -2;
2197 }
2198 }
2199
2200 if (file_exists($destfiletmp)) {
2201 // Image full path
2202 $this->html_images[$i]["fullpath"] = $destfiletmp;
2203 // Image name
2204 $this->html_images[$i]["name"] = basename($destfiletmp);
2205 // Content type
2206 $this->html_images[$i]["content_type"] = $this->image_types[strtolower($ext)];
2207 // cid
2208 $this->html_images[$i]["cid"] = $cid;
2209 // type
2210 $this->html_images[$i]["type"] = 'cidfromdata';
2211
2212 $this->html = str_replace('src="data:image/'.$ext.';base64,'.$filecontent.'"', 'src="cid:'.$this->html_images[$i]["cid"].'"', $this->html);
2213 }
2214 $i++;
2215 }
2216
2217 // Also add cidfromdata images to images_encoded array so they are sent as inline images
2218 foreach ($this->html_images as $img) {
2219 if ($img['type'] == 'cidfromdata') {
2220 $image_content = file_get_contents($img['fullpath']);
2221 if ($image_content !== false) {
2222 $idx = count($this->images_encoded);
2223 $this->images_encoded[$idx]['name'] = $img['name'];
2224 $this->images_encoded[$idx]['fullpath'] = $img['fullpath'];
2225 $this->images_encoded[$idx]['content_type'] = $img['content_type'];
2226 $this->images_encoded[$idx]['cid'] = $img['cid'];
2227 $this->images_encoded[$idx]['type'] = 'cidfromdata';
2228 $this->images_encoded[$idx]['image_encoded'] = chunk_split(base64_encode($image_content), 68, $this->eol);
2229 }
2230 }
2231 }
2232
2233 return 1;
2234 } else {
2235 return 0;
2236 }
2237 }
2238
2254 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
2255 {
2256 $ret = '';
2257
2258 $arrayaddress = (!empty($address) ? explode(',', $address) : array());
2259
2260 // Boucle sur chaque composant de l'address
2261 $i = 0;
2262 foreach ($arrayaddress as $val) {
2263 $regs = array();
2264 if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
2265 $name = trim($regs[1]);
2266 $email = trim($regs[2]);
2267 } else {
2268 $name = '';
2269 $email = trim($val);
2270 }
2271
2272 if ($email) {
2273 $i++;
2274
2275 $newemail = '';
2276 if ($format == 5) {
2277 $newemail = $name ? $name : $email;
2278 $newemail = '<a href="mailto:'.$email.'">'.$newemail.'</a>';
2279 }
2280 if ($format == 4) {
2281 $newemail = $name ? $name : $email;
2282 }
2283 if ($format == 2) {
2284 $newemail = $email;
2285 }
2286 if ($format == 1 || $format == 3) {
2287 $newemail = '<'.$email.'>';
2288 }
2289 if ($format == 0 || $format == 3) {
2290 if (getDolGlobalString('MAIN_MAIL_NO_FULL_EMAIL')) {
2291 $newemail = '<'.$email.'>';
2292 } elseif (!$name) {
2293 $newemail = '<'.$email.'>';
2294 } else {
2295 $newemail = ($format == 3 ? '"' : '').($encode ? self::encodetorfc2822($name) : $name).($format == 3 ? '"' : '').' <'.$email.'>';
2296 //$newemail = (($format == 3 && !$encode) ? '"' : '').($encode ? self::encodetorfc2822($name) : $name).(($format == 3 && !$encode) ? '"' : '').' <'.$email.'>';
2297 }
2298 }
2299
2300 $ret = ($ret ? $ret.',' : '').$newemail;
2301
2302 // Stop if we have too much records
2303 if ($maxnumberofemail && $i >= $maxnumberofemail) {
2304 if (count($arrayaddress) > $maxnumberofemail) {
2305 $ret .= '...';
2306 }
2307 break;
2308 }
2309 }
2310 }
2311
2312 return $ret;
2313 }
2314
2322 public static function getArrayAddress($address)
2323 {
2324 $ret = array();
2325
2326 $arrayaddress = explode(',', $address);
2327
2328 // Boucle sur chaque composant de l'address
2329 foreach ($arrayaddress as $val) {
2330 $regs = array();
2331 if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
2332 $name = trim($regs[1]);
2333 $email = trim($regs[2]);
2334 } else {
2335 $name = null;
2336 $email = trim($val);
2337 }
2338
2339 $ret[$email] = getDolGlobalString('MAIN_MAIL_NO_FULL_EMAIL') ? null : $name;
2340 }
2341
2342 return $ret;
2343 }
2344}
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.