dolibarr  16.0.5
CMailFile.class.php
Go to the documentation of this file.
1 <?php
38 class CMailFile
39 {
40  public $sendcontext;
41  public $sendmode;
42  public $sendsetup;
43 
47  public $subject;
48  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).
49  // Sender: Who send the email ("Sender" has sent emails on behalf of "From").
50  // 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.
51  // Return-Path: Email where to send bounds.
52  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)
53  public $errors_to; // Errors-To: Email where to send errors.
54  public $addr_to;
55  public $addr_cc;
56  public $addr_bcc;
57  public $trackid;
58 
59  public $mixed_boundary;
60  public $related_boundary;
61  public $alternative_boundary;
62  public $deliveryreceipt;
63 
64  public $atleastonefile;
65 
66  public $msg;
67  public $eol;
68  public $eol2;
69 
73  public $error = '';
74 
75  public $smtps; // Contains SMTPs object (if this method is used)
76  public $phpmailer; // Contains PHPMailer object (if this method is used)
77 
81  public $css;
83  public $styleCSS;
85  public $bodyCSS;
86 
87  public $msgid;
88  public $headers;
89  public $message;
93  public $filename_list = array();
97  public $mimetype_list = array();
101  public $mimefilename_list = array();
102 
103  // Image
104  public $html;
105  public $image_boundary;
106  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).
107  public $html_images = array();
108  public $images_encoded = array();
109  public $image_types = array(
110  'gif' => 'image/gif',
111  'jpg' => 'image/jpeg',
112  'jpeg' => 'image/jpeg',
113  'jpe' => 'image/jpeg',
114  'bmp' => 'image/bmp',
115  'png' => 'image/png',
116  'tif' => 'image/tiff',
117  'tiff' => 'image/tiff',
118  );
119 
120 
142  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 = '')
143  {
144  global $conf, $dolibarr_main_data_root, $user;
145 
146  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);
147  dol_syslog("CMailFile::CMailfile: subject=".$subject.", deliveryreceipt=".$deliveryreceipt.", msgishtml=".$msgishtml, LOG_DEBUG);
148 
149  // Clean values of $mimefilename_list
150  if (is_array($mimefilename_list)) {
151  foreach ($mimefilename_list as $key => $val) {
152  $mimefilename_list[$key] = dol_string_unaccent($mimefilename_list[$key]);
153  }
154  }
155 
156  $this->sendcontext = $sendcontext;
157 
158  // Define this->sendmode
159  $this->sendmode = '';
160  if (!empty($this->sendcontext)) {
161  $smtpContextKey = strtoupper($this->sendcontext);
162  $keyForSMTPSendMode = 'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
163  $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ? '' : $conf->global->{$keyForSMTPSendMode};
164  if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
165  $this->sendmode = $smtpContextSendMode;
166  }
167  }
168  if (empty($this->sendmode)) {
169  $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE : 'mail');
170  }
171 
172  // We define end of line (RFC 821).
173  $this->eol = "\r\n";
174  // We define end of line for header fields (RFC 822bis section 2.3 says header must contains \r\n).
175  $this->eol2 = "\r\n";
176  if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
177  $this->eol = "\n";
178  $this->eol2 = "\n";
179  $moreinheader = str_replace("\r\n", "\n", $moreinheader);
180  }
181 
182  // On defini mixed_boundary
183  $this->mixed_boundary = "multipart_x.".time().".x_boundary";
184 
185  // On defini related_boundary
186  $this->related_boundary = 'mul_'.dol_hash(uniqid("dolibarr2"), 3); // Force md5 hash (does not contains special chars)
187 
188  // On defini alternative_boundary
189  $this->alternative_boundary = 'mul_'.dol_hash(uniqid("dolibarr3"), 3); // Force md5 hash (does not contains special chars)
190 
191  if (empty($subject)) {
192  dol_syslog("CMailFile::CMailfile: Try to send an email with empty subject");
193  $this->error = 'ErrorSubjectIsRequired';
194  return;
195  }
196  if (empty($msg)) {
197  dol_syslog("CMailFile::CMailfile: Try to send an email with empty body");
198  $msg = '.'; // Avoid empty message (with empty message content, you will see a multipart structure)
199  }
200 
201  // Detect if message is HTML (use fast method)
202  if ($msgishtml == -1) {
203  $this->msgishtml = 0;
204  if (dol_textishtml($msg)) {
205  $this->msgishtml = 1;
206  }
207  } else {
208  $this->msgishtml = $msgishtml;
209  }
210 
211  global $dolibarr_main_url_root;
212 
213  // Define $urlwithroot
214  $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
215  $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
216  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
217 
218  // Replace relative /viewimage to absolute path
219  $msg = preg_replace('/src="'.preg_quote(DOL_URL_ROOT, '/').'\/viewimage\.php/ims', 'src="'.$urlwithroot.'/viewimage.php', $msg, -1);
220 
221  if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
222  $this->msgishtml = 1; // To force to send everything with content type html.
223  }
224 
225  // Detect images
226  if ($this->msgishtml) {
227  $this->html = $msg;
228 
229  $findimg = 0;
230  if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
231  $findimg = $this->findHtmlImages($dolibarr_main_data_root.'/medias');
232  }
233 
234  // Define if there is at least one file
235  if ($findimg) {
236  foreach ($this->html_images as $i => $val) {
237  if ($this->html_images[$i]) {
238  $this->atleastoneimage = 1;
239  dol_syslog("CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i]['name'], LOG_DEBUG);
240  }
241  }
242  }
243  }
244 
245  // Define if there is at least one file
246  if (is_array($filename_list)) {
247  foreach ($filename_list as $i => $val) {
248  if ($filename_list[$i]) {
249  $this->atleastonefile = 1;
250  dol_syslog("CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].", mimetype_list[$i]=".$mimetype_list[$i]." mimefilename_list[$i]=".$mimefilename_list[$i], LOG_DEBUG);
251  }
252  }
253  }
254 
255  // Add auto copy to if not already in $to (Note: Adding bcc for specific modules are also done from pages)
256  // For example MAIN_MAIL_AUTOCOPY_TO can be 'email@example.com, __USER_EMAIL__, ...'
257  if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
258  $listofemailstoadd = explode(',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
259  foreach ($listofemailstoadd as $key => $val) {
260  $emailtoadd = $listofemailstoadd[$key];
261  if (trim($emailtoadd) == '__USER_EMAIL__') {
262  if (!empty($user) && !empty($user->email)) {
263  $emailtoadd = $user->email;
264  } else {
265  $emailtoadd = '';
266  }
267  }
268  if ($emailtoadd && preg_match('/'.preg_quote($emailtoadd, '/').'/i', $to)) {
269  $emailtoadd = ''; // Email already in the "To"
270  }
271  if ($emailtoadd) {
272  $listofemailstoadd[$key] = $emailtoadd;
273  } else {
274  unset($listofemailstoadd[$key]);
275  }
276  }
277  if (!empty($listofemailstoadd)) {
278  $addr_bcc .= ($addr_bcc ? ', ' : '').join(', ', $listofemailstoadd);
279  }
280  }
281 
282  $this->subject = $subject;
283  $this->addr_to = $to;
284  $this->addr_from = $from;
285  $this->msg = $msg;
286  $this->filename_list = $filename_list;
287  $this->mimetype_list = $mimetype_list;
288  $this->mimefilename_list = $mimefilename_list;
289  $this->addr_cc = $addr_cc;
290  $this->addr_bcc = $addr_bcc;
291  $this->deliveryreceipt = $deliveryreceipt;
292  if (empty($replyto)) {
293  $replyto = $from;
294  }
295  $this->reply_to = $replyto;
296  $this->errors_to = $errors_to;
297  $this->trackid = $trackid;
298  $this->filename_list = $filename_list;
299  $this->mimetype_list = $mimetype_list;
300  $this->mimefilename_list = $mimefilename_list;
301 
302  if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
303  $this->addr_to = $conf->global->MAIN_MAIL_FORCE_SENDTO;
304  $this->addr_cc = '';
305  $this->addr_bcc = '';
306  }
307 
308  $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
309  if (!empty($this->sendcontext)) {
310  $smtpContextKey = strtoupper($this->sendcontext);
311  $keyForSMTPSendMode = 'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
312  $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ? '' : $conf->global->{$keyForSMTPSendMode};
313  if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
314  $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
315  }
316  }
317 
318  dol_syslog("CMailFile::CMailfile: sendmode=".$this->sendmode." addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
319 
320  // We set all data according to choosed sending method.
321  // We also set a value for ->msgid
322  if ($this->sendmode == 'mail') {
323  // Use mail php function (default PHP method)
324  // ------------------------------------------
325 
326  $smtp_headers = "";
327  $mime_headers = "";
328  $text_body = "";
329  $files_encoded = "";
330 
331  // Define smtp_headers (this also set ->msgid)
332  $smtp_headers = $this->write_smtpheaders();
333  if (!empty($moreinheader)) {
334  $smtp_headers .= $moreinheader; // $moreinheader contains the \r\n
335  }
336 
337  // Define mime_headers
338  $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
339 
340  if (!empty($this->html)) {
341  if (!empty($css)) {
342  $this->css = $css;
343  $this->buildCSS(); // Build a css style (mode = all) into this->styleCSS and this->bodyCSS
344  }
345 
346  $msg = $this->html;
347  }
348 
349  // Define body in text_body
350  $text_body = $this->write_body($msg);
351 
352  // Add attachments to text_encoded
353  if (!empty($this->atleastonefile)) {
354  $files_encoded = $this->write_files($filename_list, $mimetype_list, $mimefilename_list);
355  }
356 
357  // We now define $this->headers and $this->message
358  $this->headers = $smtp_headers.$mime_headers;
359  // On nettoie le header pour qu'il ne se termine pas par un retour chariot.
360  // This avoid also empty lines at end that can be interpreted as mail injection by email servers.
361  $this->headers = preg_replace("/([\r\n]+)$/i", "", $this->headers);
362 
363  //$this->message = $this->eol.'This is a message with multiple parts in MIME format.'.$this->eol;
364  $this->message = 'This is a message with multiple parts in MIME format.'.$this->eol;
365  $this->message .= $text_body.$files_encoded;
366  $this->message .= "--".$this->mixed_boundary."--".$this->eol;
367  } elseif ($this->sendmode == 'smtps') {
368  // Use SMTPS library
369  // ------------------------------------------
370 
371  require_once DOL_DOCUMENT_ROOT.'/core/class/smtps.class.php';
372  $smtps = new SMTPs();
373  $smtps->setCharSet($conf->file->character_set_client);
374 
375  // Encode subject if required.
376  $subjecttouse = $this->subject;
377  if (!ascii_check($subjecttouse)) {
378  $subjecttouse = $this->encodetorfc2822($subjecttouse);
379  }
380 
381  $smtps->setSubject($subjecttouse);
382  $smtps->setTO($this->getValidAddress($this->addr_to, 0, 1));
383  $smtps->setFrom($this->getValidAddress($this->addr_from, 0, 1));
384  $smtps->setTrackId($this->trackid);
385  $smtps->setReplyTo($this->getValidAddress($this->reply_to, 0, 1));
386 
387  if (!empty($moreinheader)) {
388  $smtps->setMoreInHeader($moreinheader);
389  }
390 
391  if (!empty($this->html)) {
392  if (!empty($css)) {
393  $this->css = $css;
394  $this->buildCSS();
395  }
396  $msg = $this->html;
397  $msg = $this->checkIfHTML($msg);
398  }
399 
400  // Replace . alone on a new line with .. to avoid to have SMTP interpret this as end of message
401  $msg = preg_replace('/(\r|\n)\.(\r|\n)/ims', '\1..\2', $msg);
402 
403  if ($this->msgishtml) {
404  $smtps->setBodyContent($msg, 'html');
405  } else {
406  $smtps->setBodyContent($msg, 'plain');
407  }
408 
409  if ($this->atleastoneimage) {
410  foreach ($this->images_encoded as $img) {
411  $smtps->setImageInline($img['image_encoded'], $img['name'], $img['content_type'], $img['cid']);
412  }
413  }
414 
415  if (!empty($this->atleastonefile)) {
416  foreach ($filename_list as $i => $val) {
417  $content = file_get_contents($filename_list[$i]);
418  $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i]);
419  }
420  }
421 
422  $smtps->setCC($this->addr_cc);
423  $smtps->setBCC($this->addr_bcc);
424  $smtps->setErrorsTo($this->errors_to);
425  $smtps->setDeliveryReceipt($this->deliveryreceipt);
426  if (!empty($conf->global->$keyforsslseflsigned)) {
427  $smtps->setOptions(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true)));
428  }
429 
430  $host = dol_getprefix('email');
431  $this->msgid = time().'.SMTPs-dolibarr-'.$this->trackid.'@'.$host;
432 
433  $this->smtps = $smtps;
434  } elseif ($this->sendmode == 'swiftmailer') {
435  // Use Swift Mailer library
436  $host = dol_getprefix('email');
437 
438  require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
439 
440  // egulias autoloader lib
441  require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/autoload.php';
442 
443  require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
444 
445  // Create the message
446  //$this->message = Swift_Message::newInstance();
447  $this->message = new Swift_Message();
448  //$this->message = new Swift_SignedMessage();
449  // Adding a trackid header to a message
450  $headers = $this->message->getHeaders();
451  $headers->addTextHeader('X-Dolibarr-TRACKID', $this->trackid.'@'.$host);
452  $this->msgid = time().'.swiftmailer-dolibarr-'.$this->trackid.'@'.$host;
453  $headerID = $this->msgid;
454  $msgid = $headers->get('Message-ID');
455  $msgid->setId($headerID);
456  $headers->addIdHeader('References', $headerID);
457  // TODO if (! empty($moreinheader)) ...
458 
459  // Give the message a subject
460  try {
461  $result = $this->message->setSubject($this->subject);
462  } catch (Exception $e) {
463  $this->errors[] = $e->getMessage();
464  }
465 
466  // Set the From address with an associative array
467  //$this->message->setFrom(array('john@doe.com' => 'John Doe'));
468  if (!empty($this->addr_from)) {
469  try {
470  if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
471  // Prevent email spoofing for smtp server with a strict configuration
472  $regexp = '/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i'; // This regular expression extracts all emails from a string
473  $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
474  $adressEmailFrom = reset($adressEmailFrom);
475  if ($emailMatchs !== false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
476  $result = $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
477  } else {
478  $result = $this->message->setFrom($this->getArrayAddress($this->addr_from));
479  }
480  } else {
481  $result = $this->message->setFrom($this->getArrayAddress($this->addr_from));
482  }
483  } catch (Exception $e) {
484  $this->errors[] = $e->getMessage();
485  }
486  }
487 
488  // Set the To addresses with an associative array
489  if (!empty($this->addr_to)) {
490  try {
491  $result = $this->message->setTo($this->getArrayAddress($this->addr_to));
492  } catch (Exception $e) {
493  $this->errors[] = $e->getMessage();
494  }
495  }
496 
497  if (!empty($this->reply_to)) {
498  try {
499  $result = $this->message->SetReplyTo($this->getArrayAddress($this->reply_to));
500  } catch (Exception $e) {
501  $this->errors[] = $e->getMessage();
502  }
503  }
504 
505  try {
506  $result = $this->message->setCharSet($conf->file->character_set_client);
507  } catch (Exception $e) {
508  $this->errors[] = $e->getMessage();
509  }
510 
511  if (!empty($this->html)) {
512  if (!empty($css)) {
513  $this->css = $css;
514  $this->buildCSS();
515  }
516  $msg = $this->html;
517  $msg = $this->checkIfHTML($msg);
518  }
519 
520  if ($this->atleastoneimage) {
521  foreach ($this->images_encoded as $img) {
522  //$img['fullpath'],$img['image_encoded'],$img['name'],$img['content_type'],$img['cid']
523  $attachment = Swift_Image::fromPath($img['fullpath']);
524  // embed image
525  $imgcid = $this->message->embed($attachment);
526  // replace cid by the one created by swiftmail in html message
527  $msg = str_replace("cid:".$img['cid'], $imgcid, $msg);
528  }
529  }
530 
531  if ($this->msgishtml) {
532  $this->message->setBody($msg, 'text/html');
533  // And optionally an alternative body
534  $this->message->addPart(html_entity_decode(strip_tags($msg)), 'text/plain');
535  } else {
536  $this->message->setBody($msg, 'text/plain');
537  // And optionally an alternative body
538  $this->message->addPart(dol_nl2br($msg), 'text/html');
539  }
540 
541  if (!empty($this->atleastonefile)) {
542  foreach ($filename_list as $i => $val) {
543  //$this->message->attach(Swift_Attachment::fromPath($filename_list[$i],$mimetype_list[$i]));
544  $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
545  if (!empty($mimefilename_list[$i])) {
546  $attachment->setFilename($mimefilename_list[$i]);
547  }
548  $this->message->attach($attachment);
549  }
550  }
551 
552  if (!empty($this->addr_cc)) {
553  $this->message->setCc($this->getArrayAddress($this->addr_cc));
554  }
555  if (!empty($this->addr_bcc)) {
556  $this->message->setBcc($this->getArrayAddress($this->addr_bcc));
557  }
558  //if (! empty($this->errors_to)) $this->message->setErrorsTo($this->getArrayAddress($this->errors_to));
559  if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
560  $this->message->setReadReceiptTo($this->getArrayAddress($this->addr_from));
561  }
562  } else {
563  // Send mail method not correctly defined
564  // --------------------------------------
565  $this->error = 'Bad value for sendmode';
566  }
567  }
568 
569 
575  public function sendfile()
576  {
577  global $conf, $db, $langs;
578 
579  $errorlevel = error_reporting();
580  //error_reporting($errorlevel ^ E_WARNING); // Desactive warnings
581 
582  $res = false;
583 
584  if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
585  require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
586  $hookmanager = new HookManager($db);
587  $hookmanager->initHooks(array('mail'));
588 
589  $parameters = array();
590  $action = '';
591  $reshook = $hookmanager->executeHooks('sendMail', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
592  if ($reshook < 0) {
593  $this->error = "Error in hook maildao sendMail ".$reshook;
594  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
595 
596  return $reshook;
597  }
598  if ($reshook == 1) { // Hook replace standard code
599  return true;
600  }
601 
602  $sendingmode = $this->sendmode;
603  if ($this->sendcontext == 'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode == 'mail') {
604  // List of sending methods
605  $listofmethods = array();
606  $listofmethods['mail'] = 'PHP mail function';
607  //$listofmethods['simplemail']='Simplemail class';
608  $listofmethods['smtps'] = 'SMTP/SMTPS socket library';
609 
610  // 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.
611  // You ensure that every user is using its own SMTP server when using the mass emailing module.
612  $linktoadminemailbefore = '<a href="'.DOL_URL_ROOT.'/admin/mails.php">';
613  $linktoadminemailend = '</a>';
614  $this->error = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
615  $this->errors[] = $langs->trans("MailSendSetupIs", $listofmethods[$sendingmode]);
616  $this->error .= '<br>'.$langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
617  $this->errors[] = $langs->trans("MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv("MAIN_MAIL_SENDMODE"), $listofmethods['smtps']);
618  if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
619  $this->error .= '<br>'.$langs->trans("MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
620  $this->errors[] = $langs->trans("MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
621  }
622  return false;
623  }
624 
625  // Check number of recipient is lower or equal than MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL
626  if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
627  $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
628  }
629  $tmparray1 = explode(',', $this->addr_to);
630  if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
631  $this->error = 'Too much recipients in to:';
632  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
633  return false;
634  }
635  if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
636  $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
637  }
638  $tmparray2 = explode(',', $this->addr_cc);
639  if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
640  $this->error = 'Too much recipients in cc:';
641  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
642  return false;
643  }
644  if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
645  $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
646  }
647  $tmparray3 = explode(',', $this->addr_bcc);
648  if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
649  $this->error = 'Too much recipients in bcc:';
650  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
651  return false;
652  }
653  if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
654  $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
655  }
656  if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
657  $this->error = 'Too much recipients in to:, cc:, bcc:';
658  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
659  return false;
660  }
661 
662  $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
663  $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
664  $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
665  $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
666  $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
667  $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
668  $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
669  if (!empty($this->sendcontext)) {
670  $smtpContextKey = strtoupper($this->sendcontext);
671  $keyForSMTPSendMode = 'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
672  $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ? '' : $conf->global->{$keyForSMTPSendMode};
673  if (!empty($smtpContextSendMode) && $smtpContextSendMode != 'default') {
674  $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
675  $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
676  $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
677  $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
678  $keyfortls = 'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
679  $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
680  $keyforsslseflsigned = 'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
681  }
682  }
683 
684  // Action according to choosed sending method
685  if ($this->sendmode == 'mail') {
686  // Use mail php function (default PHP method)
687  // ------------------------------------------
688  dol_syslog("CMailFile::sendfile addr_to=".$this->addr_to.", subject=".$this->subject, LOG_DEBUG);
689  dol_syslog("CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
690  //dol_syslog("CMailFile::sendfile message=\n".$message);
691 
692  // If Windows, sendmail_from must be defined
693  if (isset($_SERVER["WINDIR"])) {
694  if (empty($this->addr_from)) {
695  $this->addr_from = 'robot@example.com';
696  }
697  @ini_set('sendmail_from', $this->getValidAddress($this->addr_from, 2));
698  }
699 
700  // Force parameters
701  //dol_syslog("CMailFile::sendfile conf->global->".$keyforsmtpserver."=".$conf->global->$keyforsmtpserver." cpnf->global->".$keyforsmtpport."=".$conf->global->$keyforsmtpport, LOG_DEBUG);
702  if (!empty($conf->global->$keyforsmtpserver)) {
703  ini_set('SMTP', $conf->global->$keyforsmtpserver);
704  }
705  if (!empty($conf->global->$keyforsmtpport)) {
706  ini_set('smtp_port', $conf->global->$keyforsmtpport);
707  }
708 
709  $res = true;
710  if ($res && !$this->subject) {
711  $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Subject is empty";
712  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
713  $res = false;
714  }
715  $dest = $this->getValidAddress($this->addr_to, 2);
716  if ($res && !$dest) {
717  $this->error = "Failed to send mail with php mail to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')."<br>Recipient address '$dest' invalid";
718  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
719  $res = false;
720  }
721 
722  if ($res) {
723  $additionnalparam = ''; // By default
724  if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
725  // le "Return-Path" (retour des messages bounced) dans les header ne fonctionne pas avec tous les MTA
726  // Le forcage de la valeur grace à l'option -f de sendmail est donc possible si la constante MAIN_MAIL_ALLOW_SENDMAIL_F est definie.
727  // Having this variable defined may create problems with some sendmail (option -f refused)
728  // Having this variable not defined may create problems with some other sendmail (option -f required)
729  $additionnalparam .= ($additionnalparam ? ' ' : '').(!empty($conf->global->MAIN_MAIL_ERRORS_TO) ? '-f'.$this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO, 2) : ($this->addr_from != '' ? '-f'.$this->getValidAddress($this->addr_from, 2) : ''));
730  }
731  if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) { // To force usage of -ba option. This option tells sendmail to read From: or Sender: to setup sender
732  $additionnalparam .= ($additionnalparam ? ' ' : '').'-ba';
733  }
734 
735  if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
736  $additionnalparam .= ($additionnalparam ? ' ' : '').'-U '.$additionnalparam; // Use -U to add additionnal params
737  }
738 
739  $linuxlike = 1;
740  if (preg_match('/^win/i', PHP_OS)) {
741  $linuxlike = 0;
742  }
743  if (preg_match('/^mac/i', PHP_OS)) {
744  $linuxlike = 0;
745  }
746 
747  dol_syslog("CMailFile::sendfile: mail start".($linuxlike ? '' : " HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port')).", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
748 
749  $this->message = stripslashes($this->message);
750 
751  if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
752  $this->dump_mail();
753  }
754 
755  // Encode subject if required.
756  $subjecttouse = $this->subject;
757  if (!ascii_check($subjecttouse)) {
758  $subjecttouse = $this->encodetorfc2822($subjecttouse);
759  }
760 
761  if (!empty($additionnalparam)) {
762  $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
763  } else {
764  $res = mail($dest, $subjecttouse, $this->message, $this->headers);
765  }
766 
767  if (!$res) {
768  $langs->load("errors");
769  $this->error = "Failed to send mail with php mail";
770  if (!$linuxlike) {
771  $this->error .= " to HOST=".ini_get('SMTP').", PORT=".ini_get('smtp_port'); // This values are value used only for non linuxlike systems
772  }
773  $this->error .= ".<br>";
774  $this->error .= $langs->trans("ErrorPhpMailDelivery");
775  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
776  } else {
777  dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
778  }
779  }
780 
781  if (isset($_SERVER["WINDIR"])) {
782  @ini_restore('sendmail_from');
783  }
784 
785  // Restore parameters
786  if (!empty($conf->global->$keyforsmtpserver)) {
787  ini_restore('SMTP');
788  }
789  if (!empty($conf->global->$keyforsmtpport)) {
790  ini_restore('smtp_port');
791  }
792  } elseif ($this->sendmode == 'smtps') {
793  if (!is_object($this->smtps)) {
794  $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport."<br>Constructor of object CMailFile was not initialized without errors.";
795  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
796  return false;
797  }
798 
799  // Use SMTPS library
800  // ------------------------------------------
801  $this->smtps->setTransportType(0); // Only this method is coded in SMTPs library
802 
803  // Clean parameters
804  if (empty($conf->global->$keyforsmtpserver)) {
805  $conf->global->$keyforsmtpserver = ini_get('SMTP');
806  }
807  if (empty($conf->global->$keyforsmtpport)) {
808  $conf->global->$keyforsmtpport = ini_get('smtp_port');
809  }
810 
811  // If we use SSL/TLS
812  $server = $conf->global->$keyforsmtpserver;
813  $secure = '';
814  if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
815  $secure = 'ssl';
816  }
817  if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
818  $secure = 'tls';
819  }
820  $server = ($secure ? $secure.'://' : '').$server;
821 
822  $port = $conf->global->$keyforsmtpport;
823 
824  $this->smtps->setHost($server);
825  $this->smtps->setPort($port); // 25, 465...;
826 
827  $loginid = '';
828  $loginpass = '';
829  if (!empty($conf->global->$keyforsmtpid)) {
830  $loginid = $conf->global->$keyforsmtpid;
831  $this->smtps->setID($loginid);
832  }
833  if (!empty($conf->global->$keyforsmtppw)) {
834  $loginpass = $conf->global->$keyforsmtppw;
835  $this->smtps->setPW($loginpass);
836  }
837 
838  $res = true;
839  $from = $this->smtps->getFrom('org');
840  if ($res && !$from) {
841  $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - Sender address '$from' invalid";
842  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
843  $res = false;
844  }
845  $dest = $this->smtps->getTo();
846  if ($res && !$dest) {
847  $this->error = "Failed to send mail with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - Recipient address '$dest' invalid";
848  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
849  $res = false;
850  }
851 
852  if ($res) {
853  if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
854  $this->smtps->setDebug(true);
855  }
856 
857  $result = $this->smtps->sendMsg();
858  //print $result;
859 
860  if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
861  $this->dump_mail();
862  }
863 
864  $result = $this->smtps->getErrors();
865  if (empty($this->error) && empty($result)) {
866  dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
867  $res = true;
868  } else {
869  if (empty($this->error)) {
870  $this->error = $result;
871  }
872  dol_syslog("CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.", PORT=".$conf->global->$keyforsmtpport." - ".$this->error, LOG_ERR);
873  $res = false;
874  }
875  }
876  } elseif ($this->sendmode == 'swiftmailer') {
877  // Use Swift Mailer library
878  // ------------------------------------------
879  require_once DOL_DOCUMENT_ROOT.'/includes/swiftmailer/lib/swift_required.php';
880 
881  // Clean parameters
882  if (empty($conf->global->$keyforsmtpserver)) {
883  $conf->global->$keyforsmtpserver = ini_get('SMTP');
884  }
885  if (empty($conf->global->$keyforsmtpport)) {
886  $conf->global->$keyforsmtpport = ini_get('smtp_port');
887  }
888 
889  // If we use SSL/TLS
890  $server = $conf->global->$keyforsmtpserver;
891  $secure = '';
892  if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
893  $secure = 'ssl';
894  }
895  if (!empty($conf->global->$keyforstarttls) && function_exists('openssl_open')) {
896  $secure = 'tls';
897  }
898 
899  $this->transport = new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
900 
901  if (!empty($conf->global->$keyforsmtpid)) {
902  $this->transport->setUsername($conf->global->$keyforsmtpid);
903  }
904  if (!empty($conf->global->$keyforsmtppw)) {
905  $this->transport->setPassword($conf->global->$keyforsmtppw);
906  }
907  if (!empty($conf->global->$keyforsslseflsigned)) {
908  $this->transport->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false)));
909  }
910  //$smtps->_msgReplyTo = 'reply@web.com';
911 
912  // Switch content encoding to base64 - avoid the doubledot issue with quoted-printable
913  $contentEncoderBase64 = new Swift_Mime_ContentEncoder_Base64ContentEncoder();
914  $this->message->setEncoder($contentEncoderBase64);
915 
916  // Create the Mailer using your created Transport
917  $this->mailer = new Swift_Mailer($this->transport);
918 
919  // DKIM SIGN
920  if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
921  $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
922  $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
923  $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
924  $signer = new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
925  $this->message->attachSigner($signer->ignoreHeader('Return-Path'));
926  }
927 
928  if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
929  // To use the ArrayLogger
930  $this->logger = new Swift_Plugins_Loggers_ArrayLogger();
931  // Or to use the Echo Logger
932  //$this->logger = new Swift_Plugins_Loggers_EchoLogger();
933  $this->mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($this->logger));
934  }
935  // send mail
936  try {
937  $result = $this->mailer->send($this->message, $failedRecipients);
938  } catch (Exception $e) {
939  $this->error = $e->getMessage();
940  }
941  if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
942  $this->dump_mail();
943  }
944 
945  $res = true;
946  if (!empty($this->error) || !$result) {
947  if (!empty($failedRecipients)) {
948  $this->error = 'Transport failed for the following addresses: "' . join('", "', $failedRecipients) . '".';
949  }
950  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
951  $res = false;
952  } else {
953  dol_syslog("CMailFile::sendfile: mail end success", LOG_DEBUG);
954  }
955  } else {
956  // Send mail method not correctly defined
957  // --------------------------------------
958 
959  return 'Bad value for sendmode';
960  }
961 
962  $parameters = array();
963  $action = '';
964  $reshook = $hookmanager->executeHooks('sendMailAfter', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
965  if ($reshook < 0) {
966  $this->error = "Error in hook maildao sendMailAfter ".$reshook;
967  dol_syslog("CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
968 
969  return $reshook;
970  }
971  } else {
972  $this->error = 'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
973  dol_syslog("CMailFile::sendfile: ".$this->error, LOG_WARNING);
974  }
975 
976  error_reporting($errorlevel); // Reactive niveau erreur origine
977 
978  return $res;
979  }
980 
987  public static function encodetorfc2822($stringtoencode)
988  {
989  global $conf;
990  return '=?'.$conf->file->character_set_client.'?B?'.base64_encode($stringtoencode).'?=';
991  }
992 
993  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1000  private function _encode_file($sourcefile)
1001  {
1002  // phpcs:enable
1003  $newsourcefile = dol_osencode($sourcefile);
1004 
1005  if (is_readable($newsourcefile)) {
1006  $contents = file_get_contents($newsourcefile); // Need PHP 4.3
1007  $encoded = chunk_split(base64_encode($contents), 76, $this->eol); // 76 max is defined into http://tools.ietf.org/html/rfc2047
1008  return $encoded;
1009  } else {
1010  $this->error = "Error: Can't read file '".$sourcefile."' into _encode_file";
1011  dol_syslog("CMailFile::encode_file: ".$this->error, LOG_ERR);
1012  return -1;
1013  }
1014  }
1015 
1016 
1017  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1025  public function dump_mail()
1026  {
1027  // phpcs:enable
1028  global $conf, $dolibarr_main_data_root;
1029 
1030  if (@is_writeable($dolibarr_main_data_root)) { // Avoid fatal error on fopen with open_basedir
1031  $outputfile = $dolibarr_main_data_root."/dolibarr_mail.log";
1032  $fp = fopen($outputfile, "w");
1033 
1034  if ($this->sendmode == 'mail') {
1035  fputs($fp, $this->headers);
1036  fputs($fp, $this->eol); // This eol is added by the mail function, so we add it in log
1037  fputs($fp, $this->message);
1038  } elseif ($this->sendmode == 'smtps') {
1039  fputs($fp, $this->smtps->log); // this->smtps->log is filled only if MAIN_MAIL_DEBUG was set to on
1040  } elseif ($this->sendmode == 'swiftmailer') {
1041  fputs($fp, $this->logger->dump()); // this->logger is filled only if MAIN_MAIL_DEBUG was set to on
1042  }
1043 
1044  fclose($fp);
1045  if (!empty($conf->global->MAIN_UMASK)) {
1046  @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
1047  }
1048  }
1049  }
1050 
1051 
1058  public function checkIfHTML($msg)
1059  {
1060  if (!preg_match('/^[\s\t]*<html/i', $msg)) {
1061  $out = "<html><head><title></title>";
1062  if (!empty($this->styleCSS)) {
1063  $out .= $this->styleCSS;
1064  }
1065  $out .= "</head><body";
1066  if (!empty($this->bodyCSS)) {
1067  $out .= $this->bodyCSS;
1068  }
1069  $out .= ">";
1070  $out .= $msg;
1071  $out .= "</body></html>";
1072  } else {
1073  $out = $msg;
1074  }
1075 
1076  return $out;
1077  }
1078 
1084  public function buildCSS()
1085  {
1086  if (!empty($this->css)) {
1087  // Style CSS
1088  $this->styleCSS = '<style type="text/css">';
1089  $this->styleCSS .= 'body {';
1090 
1091  if ($this->css['bgcolor']) {
1092  $this->styleCSS .= ' background-color: '.$this->css['bgcolor'].';';
1093  $this->bodyCSS .= ' bgcolor="'.$this->css['bgcolor'].'"';
1094  }
1095  if ($this->css['bgimage']) {
1096  // TODO recuperer cid
1097  $this->styleCSS .= ' background-image: url("cid:'.$this->css['bgimage_cid'].'");';
1098  }
1099  $this->styleCSS .= '}';
1100  $this->styleCSS .= '</style>';
1101  }
1102  }
1103 
1104 
1105  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1111  public function write_smtpheaders()
1112  {
1113  // phpcs:enable
1114  global $conf;
1115  $out = "";
1116 
1117  $host = dol_getprefix('email');
1118 
1119  // Sender
1120  //$out.= "Sender: ".getValidAddress($this->addr_from,2)).$this->eol2;
1121  $out .= "From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1122  if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
1123  $out .= "To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1124  }
1125  // 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.
1126  $out .= "Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1127  if (isset($this->reply_to) && $this->reply_to) {
1128  $out .= "Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1129  }
1130  if (isset($this->errors_to) && $this->errors_to) {
1131  $out .= "Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1132  }
1133 
1134  // Receiver
1135  if (isset($this->addr_cc) && $this->addr_cc) {
1136  $out .= "Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1137  }
1138  if (isset($this->addr_bcc) && $this->addr_bcc) {
1139  $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 ?
1140  }
1141 
1142  // Delivery receipt
1143  if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1144  $out .= "Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1145  }
1146 
1147  //$out.= "X-Priority: 3".$this->eol2;
1148 
1149  $out .= 'Date: '.date("r").$this->eol2;
1150 
1151  $trackid = $this->trackid;
1152  if ($trackid) {
1153  // References is kept in response and Message-ID is returned into In-Reply-To:
1154  $this->msgid = time().'.phpmail-dolibarr-'.$trackid.'@'.$host;
1155  $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2; // Uppercase seems replaced by phpmail
1156  $out .= 'References: <'.$this->msgid.">".$this->eol2;
1157  $out .= 'X-Dolibarr-TRACKID: '.$trackid.'@'.$host.$this->eol2;
1158  } else {
1159  $this->msgid = time().'.phpmail@'.$host;
1160  $out .= 'Message-ID: <'.$this->msgid.">".$this->eol2;
1161  }
1162 
1163  if (!empty($_SERVER['REMOTE_ADDR'])) {
1164  $out .= "X-RemoteAddr: ".$_SERVER['REMOTE_ADDR'].$this->eol2;
1165  }
1166  $out .= "X-Mailer: Dolibarr version ".DOL_VERSION." (using php mail)".$this->eol2;
1167  $out .= "Mime-Version: 1.0".$this->eol2;
1168 
1169  //$out.= "From: ".$this->getValidAddress($this->addr_from,3,1).$this->eol;
1170 
1171  $out .= "Content-Type: multipart/mixed;".$this->eol2." boundary=\"".$this->mixed_boundary."\"".$this->eol2;
1172  $out .= "Content-Transfer-Encoding: 8bit".$this->eol2; // TODO Seems to be ignored. Header is 7bit once received.
1173 
1174  dol_syslog("CMailFile::write_smtpheaders smtp_header=\n".$out);
1175  return $out;
1176  }
1177 
1178 
1179  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1187  public function write_mimeheaders($filename_list, $mimefilename_list)
1188  {
1189  // phpcs:enable
1190  $mimedone = 0;
1191  $out = "";
1192 
1193  if (is_array($filename_list)) {
1194  $filename_list_size = count($filename_list);
1195  for ($i = 0; $i < $filename_list_size; $i++) {
1196  if ($filename_list[$i]) {
1197  if ($mimefilename_list[$i]) {
1198  $filename_list[$i] = $mimefilename_list[$i];
1199  }
1200  $out .= "X-attachments: $filename_list[$i]".$this->eol2;
1201  }
1202  }
1203  }
1204 
1205  dol_syslog("CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1206  return $out;
1207  }
1208 
1209  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1216  public function write_body($msgtext)
1217  {
1218  // phpcs:enable
1219  global $conf;
1220 
1221  $out = '';
1222 
1223  $out .= "--".$this->mixed_boundary.$this->eol;
1224 
1225  if ($this->atleastoneimage) {
1226  $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1227  $out .= $this->eol;
1228  $out .= "--".$this->alternative_boundary.$this->eol;
1229  }
1230 
1231  // Make RFC821 Compliant, replace bare linefeeds
1232  $strContent = preg_replace("/(?<!\r)\n/si", "\r\n", $msgtext); // PCRE modifier /s means new lines are common chars
1233  if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
1234  $strContent = preg_replace("/\r\n/si", "\n", $strContent); // PCRE modifier /s means new lines are common chars
1235  }
1236 
1237  $strContentAltText = '';
1238  if ($this->msgishtml) {
1239  // Similar code to forge a text from html is also in smtps.class.php
1240  $strContentAltText = preg_replace("/<br\s*[^>]*>/", " ", $strContent);
1241  $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
1242  $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ? "\r\n" : "\n"));
1243 
1244  // Check if html header already in message, if not complete the message
1245  $strContent = $this->checkIfHTML($strContent);
1246  }
1247 
1248  // Make RFC2045 Compliant, split lines
1249  //$strContent = rtrim(chunk_split($strContent)); // Function chunck_split seems ko if not used on a base64 content
1250  // TODO Encode main content into base64 and use the chunk_split, or quoted-printable
1251  $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ? "\r\n" : "\n")); // TODO Using this method creates unexpected line break on text/plain content.
1252 
1253  if ($this->msgishtml) {
1254  if ($this->atleastoneimage) {
1255  $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1256  //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1257  $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol; // Add plain text message
1258  $out .= "--".$this->alternative_boundary.$this->eol;
1259  $out .= "Content-Type: multipart/related;".$this->eol." boundary=\"".$this->related_boundary."\"".$this->eol;
1260  $out .= $this->eol;
1261  $out .= "--".$this->related_boundary.$this->eol;
1262  }
1263 
1264  if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) { // Add plain text message part before html part
1265  $out .= "Content-Type: multipart/alternative;".$this->eol." boundary=\"".$this->alternative_boundary."\"".$this->eol;
1266  $out .= $this->eol;
1267  $out .= "--".$this->alternative_boundary.$this->eol;
1268  $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1269  //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1270  $out .= $this->eol.$strContentAltText.$this->eol;
1271  $out .= "--".$this->alternative_boundary.$this->eol;
1272  }
1273 
1274  $out .= "Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1275  //$out.= "Content-Transfer-Encoding: 7bit".$this->eol; // TODO Use base64
1276  $out .= $this->eol.$strContent.$this->eol;
1277 
1278  if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) { // Add plain text message part after html part
1279  $out .= "--".$this->alternative_boundary."--".$this->eol;
1280  }
1281  } else {
1282  $out .= "Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1283  //$out.= "Content-Transfer-Encoding: 7bit".$this->eol;
1284  $out .= $this->eol.$strContent.$this->eol;
1285  }
1286 
1287  $out .= $this->eol;
1288 
1289  // Encode images
1290  if ($this->atleastoneimage) {
1291  $out .= $this->write_images($this->images_encoded);
1292  // always end related and end alternative after inline images
1293  $out .= "--".$this->related_boundary."--".$this->eol;
1294  $out .= $this->eol."--".$this->alternative_boundary."--".$this->eol;
1295  $out .= $this->eol;
1296  }
1297 
1298  return $out;
1299  }
1300 
1301  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1310  public function write_files($filename_list, $mimetype_list, $mimefilename_list)
1311  {
1312  // phpcs:enable
1313  $out = '';
1314 
1315  $filename_list_size = count($filename_list);
1316  for ($i = 0; $i < $filename_list_size; $i++) {
1317  if ($filename_list[$i]) {
1318  dol_syslog("CMailFile::write_files: i=$i");
1319  $encoded = $this->_encode_file($filename_list[$i]);
1320  if ($encoded >= 0) {
1321  if ($mimefilename_list[$i]) {
1322  $filename_list[$i] = $mimefilename_list[$i];
1323  }
1324  if (!$mimetype_list[$i]) {
1325  $mimetype_list[$i] = "application/octet-stream";
1326  }
1327 
1328  $out .= "--".$this->mixed_boundary.$this->eol;
1329  $out .= "Content-Disposition: attachment; filename=\"".$filename_list[$i]."\"".$this->eol;
1330  $out .= "Content-Type: ".$mimetype_list[$i]."; name=\"".$filename_list[$i]."\"".$this->eol;
1331  $out .= "Content-Transfer-Encoding: base64".$this->eol;
1332  $out .= "Content-Description: ".$filename_list[$i].$this->eol;
1333  $out .= $this->eol;
1334  $out .= $encoded;
1335  $out .= $this->eol;
1336  //$out.= $this->eol;
1337  } else {
1338  return $encoded;
1339  }
1340  }
1341  }
1342 
1343  return $out;
1344  }
1345 
1346 
1347  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1354  public function write_images($images_list)
1355  {
1356  // phpcs:enable
1357  $out = '';
1358 
1359  if (is_array($images_list)) {
1360  foreach ($images_list as $img) {
1361  dol_syslog("CMailFile::write_images: ".$img["name"]);
1362 
1363  $out .= "--".$this->related_boundary.$this->eol; // always related for an inline image
1364  $out .= "Content-Type: ".$img["content_type"]."; name=\"".$img["name"]."\"".$this->eol;
1365  $out .= "Content-Transfer-Encoding: base64".$this->eol;
1366  $out .= "Content-Disposition: inline; filename=\"".$img["name"]."\"".$this->eol;
1367  $out .= "Content-ID: <".$img["cid"].">".$this->eol;
1368  $out .= $this->eol;
1369  $out .= $img["image_encoded"];
1370  $out .= $this->eol;
1371  }
1372  }
1373 
1374  return $out;
1375  }
1376 
1377 
1378  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1386  public function check_server_port($host, $port)
1387  {
1388  // phpcs:enable
1389  global $conf;
1390 
1391  $_retVal = 0;
1392  $timeout = 5; // Timeout in seconds
1393 
1394  if (function_exists('fsockopen')) {
1395  $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER';
1396  $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT';
1397  $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID';
1398  $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW';
1399  $keyfortls = 'MAIN_MAIL_EMAIL_TLS';
1400  $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS';
1401  if ($this->sendcontext == 'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING != 'default') {
1402  $keyforsmtpserver = 'MAIN_MAIL_SMTP_SERVER_EMAILING';
1403  $keyforsmtpport = 'MAIN_MAIL_SMTP_PORT_EMAILING';
1404  $keyforsmtpid = 'MAIN_MAIL_SMTPS_ID_EMAILING';
1405  $keyforsmtppw = 'MAIN_MAIL_SMTPS_PW_EMAILING';
1406  $keyfortls = 'MAIN_MAIL_EMAIL_TLS_EMAILING';
1407  $keyforstarttls = 'MAIN_MAIL_EMAIL_STARTTLS_EMAILING';
1408  }
1409 
1410  // If we use SSL/TLS
1411  if (!empty($conf->global->$keyfortls) && function_exists('openssl_open')) {
1412  $host = 'ssl://'.$host;
1413  }
1414  // tls smtp start with no encryption
1415  //if (! empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS) && function_exists('openssl_open')) $host='tls://'.$host;
1416 
1417  dol_syslog("Try socket connection to host=".$host." port=".$port);
1418  //See if we can connect to the SMTP server
1419  if ($socket = @fsockopen(
1420  $host, // Host to test, IP or domain. Add ssl:// for SSL/TLS.
1421  $port, // which Port number to use
1422  $errno, // actual system level error
1423  $errstr, // and any text that goes with the error
1424  $timeout // timeout for reading/writing data over the socket
1425  )) {
1426  // Windows still does not have support for this timeout function
1427  if (function_exists('stream_set_timeout')) {
1428  stream_set_timeout($socket, $timeout, 0);
1429  }
1430 
1431  dol_syslog("Now we wait for answer 220");
1432 
1433  // Check response from Server
1434  if ($_retVal = $this->server_parse($socket, "220")) {
1435  $_retVal = $socket;
1436  }
1437  } else {
1438  $this->error = utf8_check('Error '.$errno.' - '.$errstr) ? 'Error '.$errno.' - '.$errstr : utf8_encode('Error '.$errno.' - '.$errstr);
1439  }
1440  }
1441  return $_retVal;
1442  }
1443 
1444  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1453  public function server_parse($socket, $response)
1454  {
1455  // phpcs:enable
1456  $_retVal = true; // Indicates if Object was created or not
1457  $server_response = '';
1458 
1459  while (substr($server_response, 3, 1) != ' ') {
1460  if (!($server_response = fgets($socket, 256))) {
1461  $this->error = "Couldn't get mail server response codes";
1462  return false;
1463  }
1464  }
1465 
1466  if (!(substr($server_response, 0, 3) == $response)) {
1467  $this->error = "Ran into problems sending Mail.\r\nResponse: $server_response";
1468  $_retVal = false;
1469  }
1470 
1471  return $_retVal;
1472  }
1473 
1480  public function findHtmlImages($images_dir)
1481  {
1482  // Build the list of image extensions
1483  $extensions = array_keys($this->image_types);
1484 
1485  $matches = array();
1486  preg_match_all('/(?:"|\')([^"\']+\.('.implode('|', $extensions).'))(?:"|\')/Ui', $this->html, $matches); // If "xxx.ext" or 'xxx.ext' found
1487 
1488  if (!empty($matches)) {
1489  $i = 0;
1490  foreach ($matches[1] as $full) {
1491  if (preg_match('/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) { // If xxx is 'file=aaa'
1492  $img = $regs[1];
1493 
1494  if (file_exists($images_dir.'/'.$img)) {
1495  // Image path in src
1496  $src = preg_quote($full, '/');
1497 
1498  // Image full path
1499  $this->html_images[$i]["fullpath"] = $images_dir.'/'.$img;
1500 
1501  // Image name
1502  $this->html_images[$i]["name"] = $img;
1503 
1504  // Content type
1505  if (preg_match('/^.+\.(\w{3,4})$/', $img, $reg)) {
1506  $ext = strtolower($reg[1]);
1507  $this->html_images[$i]["content_type"] = $this->image_types[$ext];
1508  }
1509 
1510  // cid
1511  $this->html_images[$i]["cid"] = dol_hash(uniqid(time()), 3); // Force md5 hash (does not contains special chars)
1512  $this->html = preg_replace("/src=\"$src\"|src='$src'/i", "src=\"cid:".$this->html_images[$i]["cid"]."\"", $this->html);
1513  }
1514  $i++;
1515  }
1516  }
1517 
1518  if (!empty($this->html_images)) {
1519  $inline = array();
1520 
1521  $i = 0;
1522 
1523  foreach ($this->html_images as $img) {
1524  $fullpath = $images_dir.'/'.$img["name"];
1525 
1526  // If duplicate images are embedded, they may show up as attachments, so remove them.
1527  if (!in_array($fullpath, $inline)) {
1528  // Read image file
1529  if ($image = file_get_contents($fullpath)) {
1530  // On garde que le nom de l'image
1531  preg_match('/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img["name"], $regs);
1532  $imgName = $regs[1];
1533 
1534  $this->images_encoded[$i]['name'] = $imgName;
1535  $this->images_encoded[$i]['fullpath'] = $fullpath;
1536  $this->images_encoded[$i]['content_type'] = $img["content_type"];
1537  $this->images_encoded[$i]['cid'] = $img["cid"];
1538  // Encodage de l'image
1539  $this->images_encoded[$i]["image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
1540  $inline[] = $fullpath;
1541  }
1542  }
1543  $i++;
1544  }
1545  } else {
1546  return -1;
1547  }
1548 
1549  return 1;
1550  } else {
1551  return 0;
1552  }
1553  }
1554 
1570  public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
1571  {
1572  global $conf;
1573 
1574  $ret = '';
1575 
1576  $arrayaddress = explode(',', $address);
1577 
1578  // Boucle sur chaque composant de l'adresse
1579  $i = 0;
1580  foreach ($arrayaddress as $val) {
1581  $regs = array();
1582  if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
1583  $name = trim($regs[1]);
1584  $email = trim($regs[2]);
1585  } else {
1586  $name = '';
1587  $email = trim($val);
1588  }
1589 
1590  if ($email) {
1591  $i++;
1592 
1593  $newemail = '';
1594  if ($format == 5) {
1595  $newemail = $name ? $name : $email;
1596  $newemail = '<a href="mailto:'.$email.'">'.$newemail.'</a>';
1597  }
1598  if ($format == 4) {
1599  $newemail = $name ? $name : $email;
1600  }
1601  if ($format == 2) {
1602  $newemail = $email;
1603  }
1604  if ($format == 1 || $format == 3) {
1605  $newemail = '<'.$email.'>';
1606  }
1607  if ($format == 0 || $format == 3) {
1608  if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
1609  $newemail = '<'.$email.'>';
1610  } elseif (!$name) {
1611  $newemail = '<'.$email.'>';
1612  } else {
1613  $newemail = ($format == 3 ? '"' : '').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ? '"' : '').' <'.$email.'>';
1614  }
1615  }
1616 
1617  $ret = ($ret ? $ret.',' : '').$newemail;
1618 
1619  // Stop if we have too much records
1620  if ($maxnumberofemail && $i >= $maxnumberofemail) {
1621  if (count($arrayaddress) > $maxnumberofemail) {
1622  $ret .= '...';
1623  }
1624  break;
1625  }
1626  }
1627  }
1628 
1629  return $ret;
1630  }
1631 
1639  public static function getArrayAddress($address)
1640  {
1641  global $conf;
1642 
1643  $ret = array();
1644 
1645  $arrayaddress = explode(',', $address);
1646 
1647  // Boucle sur chaque composant de l'adresse
1648  foreach ($arrayaddress as $val) {
1649  if (preg_match('/^(.*)<(.*)>$/i', trim($val), $regs)) {
1650  $name = trim($regs[1]);
1651  $email = trim($regs[2]);
1652  } else {
1653  $name = null;
1654  $email = trim($val);
1655  }
1656 
1657  $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name : null;
1658  }
1659 
1660  return $ret;
1661  }
1662 }
CMailFile\write_images
write_images($images_list)
Attach an image to email (mode = 'mail')
Definition: CMailFile.class.php:1354
CMailFile\checkIfHTML
checkIfHTML($msg)
Correct an uncomplete html string.
Definition: CMailFile.class.php:1058
CMailFile\write_smtpheaders
write_smtpheaders()
Create SMTP headers (mode = 'mail')
Definition: CMailFile.class.php:1111
ascii_check
ascii_check($str)
Check if a string is in ASCII.
Definition: functions.lib.php:8475
CMailFile\write_mimeheaders
write_mimeheaders($filename_list, $mimefilename_list)
Create header MIME (mode = 'mail')
Definition: CMailFile.class.php:1187
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
CMailFile\findHtmlImages
findHtmlImages($images_dir)
Seearch images into html message and init array this->images_encoded if found.
Definition: CMailFile.class.php:1480
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:6963
CMailFile\write_files
write_files($filename_list, $mimetype_list, $mimefilename_list)
Attach file to email (mode = 'mail')
Definition: CMailFile.class.php:1310
SMTPs
Class to construct and send SMTP compliant email, even to a secure SMTP server, regardless of platfor...
Definition: smtps.class.php:46
CMailFile
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Definition: CMailFile.class.php:38
CMailFile\getValidAddress
static getValidAddress($address, $format, $encode=0, $maxnumberofemail=0)
Return a formatted address string for SMTP protocol.
Definition: CMailFile.class.php:1570
CMailFile\dump_mail
dump_mail()
Write content of a SMTP request into a dump file (mode = all) Used for debugging.
Definition: CMailFile.class.php:1025
CMailFile\$bodyCSS
$bodyCSS
Defined background directly in body tag.
Definition: CMailFile.class.php:85
CMailFile\__construct
__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='')
CMailFile.
Definition: CMailFile.class.php:142
utf8_check
utf8_check($str)
Check if a string is in UTF8.
Definition: functions.lib.php:8438
CMailFile\write_body
write_body($msgtext)
Return email content (mode = 'mail')
Definition: CMailFile.class.php:1216
dol_hash
dol_hash($chain, $type='0')
Returns a hash of a string.
Definition: security.lib.php:104
Exception
CMailFile\getArrayAddress
static getArrayAddress($address)
Return a formatted array of address string for SMTP protocol.
Definition: CMailFile.class.php:1639
dol_string_unaccent
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
Definition: functions.lib.php:1309
CMailFile\check_server_port
check_server_port($host, $port)
Try to create a socket connection.
Definition: CMailFile.class.php:1386
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CMailFile\server_parse
server_parse($socket, $response)
This function has been modified as provided by SirSir to allow multiline responses when using SMTP Ex...
Definition: CMailFile.class.php:1453
CMailFile\sendfile
sendfile()
Send mail that was prepared by constructor.
Definition: CMailFile.class.php:575
CMailFile\_encode_file
_encode_file($sourcefile)
Read a file on disk and return encoded content for emails (mode = 'mail')
Definition: CMailFile.class.php:1000
CMailFile\buildCSS
buildCSS()
Build a css style (mode = all) into this->styleCSS and this->bodyCSS.
Definition: CMailFile.class.php:1084
dol_textishtml
dol_textishtml($msg, $option=0)
Return if a text is a html content.
Definition: functions.lib.php:7185
CMailFile\$styleCSS
$styleCSS
Defined css style for body background.
Definition: CMailFile.class.php:83
HookManager
Class to manage hooks.
Definition: hookmanager.class.php:30
CMailFile\encodetorfc2822
static encodetorfc2822($stringtoencode)
Encode subject according to RFC 2822 - http://en.wikipedia.org/wiki/MIME#Encoded-Word.
Definition: CMailFile.class.php:987