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