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