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