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