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