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