59 public $mixed_boundary;
60 public $related_boundary;
61 public $alternative_boundary;
62 public $deliveryreceipt;
64 public $atleastonefile;
93 public $filename_list = array();
97 public $mimetype_list = array();
101 public $mimefilename_list = array();
105 public $image_boundary;
106 public $atleastoneimage = 0;
107 public $html_images = array();
108 public $images_encoded = array();
109 public $image_types = array(
110 'gif' =>
'image/gif',
111 'jpg' =>
'image/jpeg',
112 'jpeg' =>
'image/jpeg',
113 'jpe' =>
'image/jpeg',
114 'bmp' =>
'image/bmp',
115 'png' =>
'image/png',
116 'tif' =>
'image/tiff',
117 'tiff' =>
'image/tiff',
142 public function __construct($subject, $to, $from, $msg, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $addr_cc =
"", $addr_bcc =
"", $deliveryreceipt = 0, $msgishtml = 0, $errors_to =
'', $css =
'', $trackid =
'', $moreinheader =
'', $sendcontext =
'standard', $replyto =
'')
144 global $conf, $dolibarr_main_data_root, $user;
146 dol_syslog(
"CMailFile::CMailfile: charset=".$conf->file->character_set_client.
" from=$from, to=$to, addr_cc=$addr_cc, addr_bcc=$addr_bcc, errors_to=$errors_to, replyto=$replyto trackid=$trackid sendcontext=$sendcontext", LOG_DEBUG);
147 dol_syslog(
"CMailFile::CMailfile: subject=".$subject.
", deliveryreceipt=".$deliveryreceipt.
", msgishtml=".$msgishtml, LOG_DEBUG);
150 if (is_array($mimefilename_list)) {
151 foreach ($mimefilename_list as $key => $val) {
156 $this->sendcontext = $sendcontext;
159 $this->sendmode =
'';
160 if (!empty($this->sendcontext)) {
161 $smtpContextKey = strtoupper($this->sendcontext);
162 $keyForSMTPSendMode =
'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
163 $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ?
'' : $conf->global->{$keyForSMTPSendMode};
164 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
165 $this->sendmode = $smtpContextSendMode;
168 if (empty($this->sendmode)) {
169 $this->sendmode = (!empty($conf->global->MAIN_MAIL_SENDMODE) ? $conf->global->MAIN_MAIL_SENDMODE :
'mail');
175 $this->eol2 =
"\r\n";
176 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
179 $moreinheader = str_replace(
"\r\n",
"\n", $moreinheader);
183 $this->mixed_boundary =
"multipart_x.".time().
".x_boundary";
186 $this->related_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr2"), 3);
189 $this->alternative_boundary =
'mul_'.dol_hash(uniqid(
"dolibarr3"), 3);
191 if (empty($subject)) {
192 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty subject");
193 $this->error =
'ErrorSubjectIsRequired';
197 dol_syslog(
"CMailFile::CMailfile: Try to send an email with empty body");
202 if ($msgishtml == -1) {
203 $this->msgishtml = 0;
205 $this->msgishtml = 1;
208 $this->msgishtml = $msgishtml;
211 global $dolibarr_main_url_root;
214 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
215 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
219 $msg = preg_replace(
'/src="'.preg_quote(DOL_URL_ROOT,
'/').
'\/viewimage\.php/ims',
'src="'.$urlwithroot.
'/viewimage.php', $msg, -1);
221 if (!empty($conf->global->MAIN_MAIL_FORCE_CONTENT_TYPE_TO_HTML)) {
222 $this->msgishtml = 1;
226 if ($this->msgishtml) {
230 if (!empty($conf->global->MAIN_MAIL_ADD_INLINE_IMAGES_IF_IN_MEDIAS)) {
231 $findimg = $this->findHtmlImages($dolibarr_main_data_root.
'/medias');
236 foreach ($this->html_images as $i => $val) {
237 if ($this->html_images[$i]) {
238 $this->atleastoneimage = 1;
239 dol_syslog(
"CMailFile::CMailfile: html_images[$i]['name']=".$this->html_images[$i][
'name'], LOG_DEBUG);
246 if (is_array($filename_list)) {
247 foreach ($filename_list as $i => $val) {
248 if ($filename_list[$i]) {
249 $this->atleastonefile = 1;
250 dol_syslog(
"CMailFile::CMailfile: filename_list[$i]=".$filename_list[$i].
", mimetype_list[$i]=".$mimetype_list[$i].
" mimefilename_list[$i]=".$mimefilename_list[$i], LOG_DEBUG);
257 if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) {
258 $listofemailstoadd = explode(
',', $conf->global->MAIN_MAIL_AUTOCOPY_TO);
259 foreach ($listofemailstoadd as $key => $val) {
260 $emailtoadd = $listofemailstoadd[$key];
261 if (trim($emailtoadd) ==
'__USER_EMAIL__') {
262 if (!empty($user) && !empty($user->email)) {
263 $emailtoadd = $user->email;
268 if ($emailtoadd && preg_match(
'/'.preg_quote($emailtoadd,
'/').
'/i', $to)) {
272 $listofemailstoadd[$key] = $emailtoadd;
274 unset($listofemailstoadd[$key]);
277 if (!empty($listofemailstoadd)) {
278 $addr_bcc .= ($addr_bcc ?
', ' :
'').join(
', ', $listofemailstoadd);
282 $this->subject = $subject;
283 $this->addr_to = $to;
284 $this->addr_from = $from;
286 $this->filename_list = $filename_list;
287 $this->mimetype_list = $mimetype_list;
288 $this->mimefilename_list = $mimefilename_list;
289 $this->addr_cc = $addr_cc;
290 $this->addr_bcc = $addr_bcc;
291 $this->deliveryreceipt = $deliveryreceipt;
292 if (empty($replyto)) {
295 $this->reply_to = $replyto;
296 $this->errors_to = $errors_to;
297 $this->trackid = $trackid;
298 $this->filename_list = $filename_list;
299 $this->mimetype_list = $mimetype_list;
300 $this->mimefilename_list = $mimefilename_list;
302 if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO)) {
303 $this->addr_to = $conf->global->MAIN_MAIL_FORCE_SENDTO;
305 $this->addr_bcc =
'';
308 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
309 if (!empty($this->sendcontext)) {
310 $smtpContextKey = strtoupper($this->sendcontext);
311 $keyForSMTPSendMode =
'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
312 $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ?
'' : $conf->global->{$keyForSMTPSendMode};
313 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
314 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
318 dol_syslog(
"CMailFile::CMailfile: sendmode=".$this->sendmode.
" addr_bcc=$addr_bcc, replyto=$replyto", LOG_DEBUG);
322 if ($this->sendmode ==
'mail') {
332 $smtp_headers = $this->write_smtpheaders();
333 if (!empty($moreinheader)) {
334 $smtp_headers .= $moreinheader;
338 $mime_headers = $this->write_mimeheaders($filename_list, $mimefilename_list);
340 if (!empty($this->html)) {
350 $text_body = $this->write_body($msg);
353 if (!empty($this->atleastonefile)) {
354 $files_encoded = $this->write_files($filename_list, $mimetype_list, $mimefilename_list);
358 $this->headers = $smtp_headers.$mime_headers;
361 $this->headers = preg_replace(
"/([\r\n]+)$/i",
"", $this->headers);
364 $this->message =
'This is a message with multiple parts in MIME format.'.$this->eol;
365 $this->message .= $text_body.$files_encoded;
366 $this->message .=
"--".$this->mixed_boundary.
"--".$this->eol;
367 } elseif ($this->sendmode ==
'smtps') {
371 require_once DOL_DOCUMENT_ROOT.
'/core/class/smtps.class.php';
372 $smtps =
new SMTPs();
373 $smtps->setCharSet($conf->file->character_set_client);
376 $subjecttouse = $this->subject;
378 $subjecttouse = $this->encodetorfc2822($subjecttouse);
381 $smtps->setSubject($subjecttouse);
382 $smtps->setTO($this->getValidAddress($this->addr_to, 0, 1));
383 $smtps->setFrom($this->getValidAddress($this->addr_from, 0, 1));
384 $smtps->setTrackId($this->trackid);
385 $smtps->setReplyTo($this->getValidAddress($this->reply_to, 0, 1));
387 if (!empty($moreinheader)) {
388 $smtps->setMoreInHeader($moreinheader);
391 if (!empty($this->html)) {
397 $msg = $this->checkIfHTML($msg);
401 $msg = preg_replace(
'/(\r|\n)\.(\r|\n)/ims',
'\1..\2', $msg);
403 if ($this->msgishtml) {
404 $smtps->setBodyContent($msg,
'html');
406 $smtps->setBodyContent($msg,
'plain');
409 if ($this->atleastoneimage) {
410 foreach ($this->images_encoded as $img) {
411 $smtps->setImageInline($img[
'image_encoded'], $img[
'name'], $img[
'content_type'], $img[
'cid']);
415 if (!empty($this->atleastonefile)) {
416 foreach ($filename_list as $i => $val) {
417 $content = file_get_contents($filename_list[$i]);
418 $smtps->setAttachment($content, $mimefilename_list[$i], $mimetype_list[$i]);
422 $smtps->setCC($this->addr_cc);
423 $smtps->setBCC($this->addr_bcc);
424 $smtps->setErrorsTo($this->errors_to);
425 $smtps->setDeliveryReceipt($this->deliveryreceipt);
426 if (!empty($conf->global->$keyforsslseflsigned)) {
427 $smtps->setOptions(array(
'ssl' => array(
'verify_peer' =>
false,
'verify_peer_name' =>
false,
'allow_self_signed' =>
true)));
430 $host = dol_getprefix(
'email');
431 $this->msgid = time().
'.SMTPs-dolibarr-'.$this->trackid.
'@'.$host;
433 $this->smtps = $smtps;
434 } elseif ($this->sendmode ==
'swiftmailer') {
436 $host = dol_getprefix(
'email');
438 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lexer/lib/Doctrine/Common/Lexer/AbstractLexer.php';
441 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/autoload.php';
443 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
447 $this->message =
new Swift_Message();
450 $headers = $this->message->getHeaders();
451 $headers->addTextHeader(
'X-Dolibarr-TRACKID', $this->trackid.
'@'.$host);
452 $this->msgid = time().
'.swiftmailer-dolibarr-'.$this->trackid.
'@'.$host;
453 $headerID = $this->msgid;
454 $msgid = $headers->get(
'Message-ID');
455 $msgid->setId($headerID);
456 $headers->addIdHeader(
'References', $headerID);
461 $result = $this->message->setSubject($this->subject);
463 $this->errors[] = $e->getMessage();
468 if (!empty($this->addr_from)) {
470 if (!empty($conf->global->MAIN_FORCE_DISABLE_MAIL_SPOOFING)) {
472 $regexp =
'/([a-z0-9_\.\-\+])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
473 $emailMatchs = preg_match_all($regexp, $from, $adressEmailFrom);
474 $adressEmailFrom = reset($adressEmailFrom);
475 if ($emailMatchs !==
false && filter_var($conf->global->MAIN_MAIL_SMTPS_ID, FILTER_VALIDATE_EMAIL) && $conf->global->MAIN_MAIL_SMTPS_ID !== $adressEmailFrom) {
476 $result = $this->message->setFrom($conf->global->MAIN_MAIL_SMTPS_ID);
478 $result = $this->message->setFrom($this->getArrayAddress($this->addr_from));
481 $result = $this->message->setFrom($this->getArrayAddress($this->addr_from));
484 $this->errors[] = $e->getMessage();
489 if (!empty($this->addr_to)) {
491 $result = $this->message->setTo($this->getArrayAddress($this->addr_to));
493 $this->errors[] = $e->getMessage();
497 if (!empty($this->reply_to)) {
499 $result = $this->message->SetReplyTo($this->getArrayAddress($this->reply_to));
501 $this->errors[] = $e->getMessage();
506 $result = $this->message->setCharSet($conf->file->character_set_client);
508 $this->errors[] = $e->getMessage();
511 if (!empty($this->html)) {
517 $msg = $this->checkIfHTML($msg);
520 if ($this->atleastoneimage) {
521 foreach ($this->images_encoded as $img) {
523 $attachment = Swift_Image::fromPath($img[
'fullpath']);
525 $imgcid = $this->message->embed($attachment);
527 $msg = str_replace(
"cid:".$img[
'cid'], $imgcid, $msg);
531 if ($this->msgishtml) {
532 $this->message->setBody($msg,
'text/html');
534 $this->message->addPart(html_entity_decode(strip_tags($msg)),
'text/plain');
536 $this->message->setBody($msg,
'text/plain');
538 $this->message->addPart(
dol_nl2br($msg),
'text/html');
541 if (!empty($this->atleastonefile)) {
542 foreach ($filename_list as $i => $val) {
544 $attachment = Swift_Attachment::fromPath($filename_list[$i], $mimetype_list[$i]);
545 if (!empty($mimefilename_list[$i])) {
546 $attachment->setFilename($mimefilename_list[$i]);
548 $this->message->attach($attachment);
552 if (!empty($this->addr_cc)) {
553 $this->message->setCc($this->getArrayAddress($this->addr_cc));
555 if (!empty($this->addr_bcc)) {
556 $this->message->setBcc($this->getArrayAddress($this->addr_bcc));
559 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
560 $this->message->setReadReceiptTo($this->getArrayAddress($this->addr_from));
565 $this->error =
'Bad value for sendmode';
577 global $conf, $db, $langs;
579 $errorlevel = error_reporting();
584 if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
585 require_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
587 $hookmanager->initHooks(array(
'mail'));
589 $parameters = array();
591 $reshook = $hookmanager->executeHooks(
'sendMail', $parameters, $this, $action);
593 $this->error =
"Error in hook maildao sendMail ".$reshook;
594 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
602 $sendingmode = $this->sendmode;
603 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAILING_NO_USING_PHPMAIL) && $sendingmode ==
'mail') {
605 $listofmethods = array();
606 $listofmethods[
'mail'] =
'PHP mail function';
608 $listofmethods[
'smtps'] =
'SMTP/SMTPS socket library';
612 $linktoadminemailbefore =
'<a href="'.DOL_URL_ROOT.
'/admin/mails.php">';
613 $linktoadminemailend =
'</a>';
614 $this->error = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
615 $this->errors[] = $langs->trans(
"MailSendSetupIs", $listofmethods[$sendingmode]);
616 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
617 $this->errors[] = $langs->trans(
"MailSendSetupIs2", $linktoadminemailbefore, $linktoadminemailend, $langs->transnoentitiesnoconv(
"MAIN_MAIL_SENDMODE"), $listofmethods[
'smtps']);
618 if (!empty($conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS)) {
619 $this->error .=
'<br>'.$langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
620 $this->errors[] = $langs->trans(
"MailSendSetupIs3", $conf->global->MAILING_SMTP_SETUP_EMAILS_FOR_QUESTIONS);
626 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL)) {
627 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL = 10;
629 $tmparray1 = explode(
',', $this->addr_to);
630 if (count($tmparray1) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_TO_IN_SAME_EMAIL) {
631 $this->error =
'Too much recipients in to:';
632 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
635 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL)) {
636 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL = 10;
638 $tmparray2 = explode(
',', $this->addr_cc);
639 if (count($tmparray2) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_CC_IN_SAME_EMAIL) {
640 $this->error =
'Too much recipients in cc:';
641 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
644 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL)) {
645 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL = 10;
647 $tmparray3 = explode(
',', $this->addr_bcc);
648 if (count($tmparray3) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_BCC_IN_SAME_EMAIL) {
649 $this->error =
'Too much recipients in bcc:';
650 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
653 if (empty($conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL)) {
654 $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL = 10;
656 if ((count($tmparray1) + count($tmparray2) + count($tmparray3)) > $conf->global->MAIL_MAX_NB_OF_RECIPIENTS_IN_SAME_EMAIL) {
657 $this->error =
'Too much recipients in to:, cc:, bcc:';
658 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_WARNING);
662 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
663 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
664 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
665 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
666 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
667 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
668 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED';
669 if (!empty($this->sendcontext)) {
670 $smtpContextKey = strtoupper($this->sendcontext);
671 $keyForSMTPSendMode =
'MAIN_MAIL_SENDMODE_'.$smtpContextKey;
672 $smtpContextSendMode = empty($conf->global->{$keyForSMTPSendMode}) ?
'' : $conf->global->{$keyForSMTPSendMode};
673 if (!empty($smtpContextSendMode) && $smtpContextSendMode !=
'default') {
674 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_'.$smtpContextKey;
675 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_'.$smtpContextKey;
676 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_'.$smtpContextKey;
677 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_'.$smtpContextKey;
678 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_'.$smtpContextKey;
679 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_'.$smtpContextKey;
680 $keyforsslseflsigned =
'MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED_'.$smtpContextKey;
685 if ($this->sendmode ==
'mail') {
688 dol_syslog(
"CMailFile::sendfile addr_to=".$this->addr_to.
", subject=".$this->subject, LOG_DEBUG);
689 dol_syslog(
"CMailFile::sendfile header=\n".$this->headers, LOG_DEBUG);
693 if (isset($_SERVER[
"WINDIR"])) {
694 if (empty($this->addr_from)) {
695 $this->addr_from =
'robot@example.com';
697 @ini_set(
'sendmail_from', $this->getValidAddress($this->addr_from, 2));
702 if (!empty($conf->global->$keyforsmtpserver)) {
703 ini_set(
'SMTP', $conf->global->$keyforsmtpserver);
705 if (!empty($conf->global->$keyforsmtpport)) {
706 ini_set(
'smtp_port', $conf->global->$keyforsmtpport);
710 if ($res && !$this->subject) {
711 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Subject is empty";
712 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
715 $dest = $this->getValidAddress($this->addr_to, 2);
716 if ($res && !$dest) {
717 $this->error =
"Failed to send mail with php mail to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port').
"<br>Recipient address '$dest' invalid";
718 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
723 $additionnalparam =
'';
724 if (!empty($conf->global->MAIN_MAIL_ALLOW_SENDMAIL_F)) {
729 $additionnalparam .= ($additionnalparam ?
' ' :
'').(!empty($conf->global->MAIN_MAIL_ERRORS_TO) ?
'-f'.$this->getValidAddress($conf->global->MAIN_MAIL_ERRORS_TO, 2) : ($this->addr_from !=
'' ?
'-f'.$this->getValidAddress($this->addr_from, 2) :
''));
731 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
732 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-ba';
735 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_ADDPARAM)) {
736 $additionnalparam .= ($additionnalparam ?
' ' :
'').
'-U '.$additionnalparam;
740 if (preg_match(
'/^win/i', PHP_OS)) {
743 if (preg_match(
'/^mac/i', PHP_OS)) {
747 dol_syslog(
"CMailFile::sendfile: mail start".($linuxlike ?
'' :
" HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port')).
", additionnal_parameters=".$additionnalparam, LOG_DEBUG);
749 $this->message = stripslashes($this->message);
751 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
756 $subjecttouse = $this->subject;
758 $subjecttouse = $this->encodetorfc2822($subjecttouse);
761 if (!empty($additionnalparam)) {
762 $res = mail($dest, $subjecttouse, $this->message, $this->headers, $additionnalparam);
764 $res = mail($dest, $subjecttouse, $this->message, $this->headers);
768 $langs->load(
"errors");
769 $this->error =
"Failed to send mail with php mail";
771 $this->error .=
" to HOST=".ini_get(
'SMTP').
", PORT=".ini_get(
'smtp_port');
773 $this->error .=
".<br>";
774 $this->error .= $langs->trans(
"ErrorPhpMailDelivery");
775 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
777 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
781 if (isset($_SERVER[
"WINDIR"])) {
782 @ini_restore(
'sendmail_from');
786 if (!empty($conf->global->$keyforsmtpserver)) {
789 if (!empty($conf->global->$keyforsmtpport)) {
790 ini_restore(
'smtp_port');
792 } elseif ($this->sendmode ==
'smtps') {
793 if (!is_object($this->smtps)) {
794 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
"<br>Constructor of object CMailFile was not initialized without errors.";
795 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
801 $this->smtps->setTransportType(0);
804 if (empty($conf->global->$keyforsmtpserver)) {
805 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
807 if (empty($conf->global->$keyforsmtpport)) {
808 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
812 $server = $conf->global->$keyforsmtpserver;
814 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
817 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
820 $server = ($secure ? $secure.
'://' :
'').$server;
822 $port = $conf->global->$keyforsmtpport;
824 $this->smtps->setHost($server);
825 $this->smtps->setPort($port);
829 if (!empty($conf->global->$keyforsmtpid)) {
830 $loginid = $conf->global->$keyforsmtpid;
831 $this->smtps->setID($loginid);
833 if (!empty($conf->global->$keyforsmtppw)) {
834 $loginpass = $conf->global->$keyforsmtppw;
835 $this->smtps->setPW($loginpass);
839 $from = $this->smtps->getFrom(
'org');
840 if ($res && !$from) {
841 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Sender address '$from' invalid";
842 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
845 $dest = $this->smtps->getTo();
846 if ($res && !$dest) {
847 $this->error =
"Failed to send mail with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - Recipient address '$dest' invalid";
848 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
853 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
854 $this->smtps->setDebug(
true);
857 $result = $this->smtps->sendMsg();
860 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
864 $result = $this->smtps->getErrors();
865 if (empty($this->error) && empty($result)) {
866 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
869 if (empty($this->error)) {
870 $this->error = $result;
872 dol_syslog(
"CMailFile::sendfile: mail end error with smtps lib to HOST=".$server.
", PORT=".$conf->global->$keyforsmtpport.
" - ".$this->error, LOG_ERR);
876 } elseif ($this->sendmode ==
'swiftmailer') {
879 require_once DOL_DOCUMENT_ROOT.
'/includes/swiftmailer/lib/swift_required.php';
882 if (empty($conf->global->$keyforsmtpserver)) {
883 $conf->global->$keyforsmtpserver = ini_get(
'SMTP');
885 if (empty($conf->global->$keyforsmtpport)) {
886 $conf->global->$keyforsmtpport = ini_get(
'smtp_port');
890 $server = $conf->global->$keyforsmtpserver;
892 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
895 if (!empty($conf->global->$keyforstarttls) && function_exists(
'openssl_open')) {
899 $this->transport =
new Swift_SmtpTransport($server, $conf->global->$keyforsmtpport, $secure);
901 if (!empty($conf->global->$keyforsmtpid)) {
902 $this->transport->setUsername($conf->global->$keyforsmtpid);
904 if (!empty($conf->global->$keyforsmtppw)) {
905 $this->transport->setPassword($conf->global->$keyforsmtppw);
907 if (!empty($conf->global->$keyforsslseflsigned)) {
908 $this->transport->setStreamOptions(array(
'ssl' => array(
'allow_self_signed' =>
true,
'verify_peer' =>
false)));
913 $contentEncoderBase64 =
new Swift_Mime_ContentEncoder_Base64ContentEncoder();
914 $this->message->setEncoder($contentEncoderBase64);
917 $this->mailer =
new Swift_Mailer($this->transport);
920 if ($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) {
921 $privateKey = $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
922 $domainName = $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
923 $selector = $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
924 $signer =
new Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
925 $this->message->attachSigner($signer->ignoreHeader(
'Return-Path'));
928 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
930 $this->logger =
new Swift_Plugins_Loggers_ArrayLogger();
933 $this->mailer->registerPlugin(
new Swift_Plugins_LoggerPlugin($this->logger));
937 $result = $this->mailer->send($this->message, $failedRecipients);
939 $this->error = $e->getMessage();
941 if (!empty($conf->global->MAIN_MAIL_DEBUG)) {
946 if (!empty($this->error) || !$result) {
947 if (!empty($failedRecipients)) {
948 $this->error =
'Transport failed for the following addresses: "' . join(
'", "', $failedRecipients) .
'".';
950 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
953 dol_syslog(
"CMailFile::sendfile: mail end success", LOG_DEBUG);
959 return 'Bad value for sendmode';
962 $parameters = array();
964 $reshook = $hookmanager->executeHooks(
'sendMailAfter', $parameters, $this, $action);
966 $this->error =
"Error in hook maildao sendMailAfter ".$reshook;
967 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
972 $this->error =
'No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS';
973 dol_syslog(
"CMailFile::sendfile: ".$this->error, LOG_WARNING);
976 error_reporting($errorlevel);
990 return '=?'.$conf->file->character_set_client.
'?B?'.base64_encode($stringtoencode).
'?=';
1005 if (is_readable($newsourcefile)) {
1006 $contents = file_get_contents($newsourcefile);
1007 $encoded = chunk_split(base64_encode($contents), 76, $this->eol);
1010 $this->error =
"Error: Can't read file '".$sourcefile.
"' into _encode_file";
1011 dol_syslog(
"CMailFile::encode_file: ".$this->error, LOG_ERR);
1028 global $conf, $dolibarr_main_data_root;
1030 if (@is_writeable($dolibarr_main_data_root)) {
1031 $outputfile = $dolibarr_main_data_root.
"/dolibarr_mail.log";
1032 $fp = fopen($outputfile,
"w");
1034 if ($this->sendmode ==
'mail') {
1035 fputs($fp, $this->headers);
1036 fputs($fp, $this->eol);
1037 fputs($fp, $this->message);
1038 } elseif ($this->sendmode ==
'smtps') {
1039 fputs($fp, $this->smtps->log);
1040 } elseif ($this->sendmode ==
'swiftmailer') {
1041 fputs($fp, $this->logger->dump());
1045 if (!empty($conf->global->MAIN_UMASK)) {
1046 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
1060 if (!preg_match(
'/^[\s\t]*<html/i', $msg)) {
1061 $out =
"<html><head><title></title>";
1062 if (!empty($this->styleCSS)) {
1063 $out .= $this->styleCSS;
1065 $out .=
"</head><body";
1066 if (!empty($this->bodyCSS)) {
1067 $out .= $this->bodyCSS;
1071 $out .=
"</body></html>";
1086 if (!empty($this->css)) {
1088 $this->styleCSS =
'<style type="text/css">';
1089 $this->styleCSS .=
'body {';
1091 if ($this->css[
'bgcolor']) {
1092 $this->styleCSS .=
' background-color: '.$this->css[
'bgcolor'].
';';
1093 $this->bodyCSS .=
' bgcolor="'.$this->css[
'bgcolor'].
'"';
1095 if ($this->css[
'bgimage']) {
1097 $this->styleCSS .=
' background-image: url("cid:'.$this->css[
'bgimage_cid'].
'");';
1099 $this->styleCSS .=
'}';
1100 $this->styleCSS .=
'</style>';
1117 $host = dol_getprefix(
'email');
1121 $out .=
"From: ".$this->getValidAddress($this->addr_from, 3, 1).$this->eol2;
1122 if (!empty($conf->global->MAIN_MAIL_SENDMAIL_FORCE_BA)) {
1123 $out .=
"To: ".$this->getValidAddress($this->addr_to, 0, 1).$this->eol2;
1126 $out .=
"Return-Path: ".$this->getValidAddress($this->addr_from, 0, 1).$this->eol2;
1127 if (isset($this->reply_to) && $this->reply_to) {
1128 $out .=
"Reply-To: ".$this->getValidAddress($this->reply_to, 2).$this->eol2;
1130 if (isset($this->errors_to) && $this->errors_to) {
1131 $out .=
"Errors-To: ".$this->getValidAddress($this->errors_to, 2).$this->eol2;
1135 if (isset($this->addr_cc) && $this->addr_cc) {
1136 $out .=
"Cc: ".$this->getValidAddress($this->addr_cc, 2).$this->eol2;
1138 if (isset($this->addr_bcc) && $this->addr_bcc) {
1139 $out .=
"Bcc: ".$this->getValidAddress($this->addr_bcc, 2).$this->eol2;
1143 if (isset($this->deliveryreceipt) && $this->deliveryreceipt == 1) {
1144 $out .=
"Disposition-Notification-To: ".$this->getValidAddress($this->addr_from, 2).$this->eol2;
1149 $out .=
'Date: '.date(
"r").$this->eol2;
1151 $trackid = $this->trackid;
1154 $this->msgid = time().
'.phpmail-dolibarr-'.$trackid.
'@'.$host;
1155 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1156 $out .=
'References: <'.$this->msgid.
">".$this->eol2;
1157 $out .=
'X-Dolibarr-TRACKID: '.$trackid.
'@'.$host.$this->eol2;
1159 $this->msgid = time().
'.phpmail@'.$host;
1160 $out .=
'Message-ID: <'.$this->msgid.
">".$this->eol2;
1163 if (!empty($_SERVER[
'REMOTE_ADDR'])) {
1164 $out .=
"X-RemoteAddr: ".$_SERVER[
'REMOTE_ADDR'].$this->eol2;
1166 $out .=
"X-Mailer: Dolibarr version ".DOL_VERSION.
" (using php mail)".$this->eol2;
1167 $out .=
"Mime-Version: 1.0".$this->eol2;
1171 $out .=
"Content-Type: multipart/mixed;".$this->eol2.
" boundary=\"".$this->mixed_boundary.
"\"".$this->eol2;
1172 $out .=
"Content-Transfer-Encoding: 8bit".$this->eol2;
1174 dol_syslog(
"CMailFile::write_smtpheaders smtp_header=\n".$out);
1193 if (is_array($filename_list)) {
1194 $filename_list_size = count($filename_list);
1195 for ($i = 0; $i < $filename_list_size; $i++) {
1196 if ($filename_list[$i]) {
1197 if ($mimefilename_list[$i]) {
1198 $filename_list[$i] = $mimefilename_list[$i];
1200 $out .=
"X-attachments: $filename_list[$i]".$this->eol2;
1205 dol_syslog(
"CMailFile::write_mimeheaders mime_header=\n".$out, LOG_DEBUG);
1223 $out .=
"--".$this->mixed_boundary.$this->eol;
1225 if ($this->atleastoneimage) {
1226 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1228 $out .=
"--".$this->alternative_boundary.$this->eol;
1232 $strContent = preg_replace(
"/(?<!\r)\n/si",
"\r\n", $msgtext);
1233 if (!empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA)) {
1234 $strContent = preg_replace(
"/\r\n/si",
"\n", $strContent);
1237 $strContentAltText =
'';
1238 if ($this->msgishtml) {
1240 $strContentAltText = preg_replace(
"/<br\s*[^>]*>/",
" ", $strContent);
1241 $strContentAltText = html_entity_decode(strip_tags($strContentAltText));
1242 $strContentAltText = trim(wordwrap($strContentAltText, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1245 $strContent = $this->checkIfHTML($strContent);
1251 $strContent = rtrim(wordwrap($strContent, 75, empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA) ?
"\r\n" :
"\n"));
1253 if ($this->msgishtml) {
1254 if ($this->atleastoneimage) {
1255 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1257 $out .= $this->eol.($strContentAltText ? $strContentAltText : strip_tags($strContent)).$this->eol;
1258 $out .=
"--".$this->alternative_boundary.$this->eol;
1259 $out .=
"Content-Type: multipart/related;".$this->eol.
" boundary=\"".$this->related_boundary.
"\"".$this->eol;
1261 $out .=
"--".$this->related_boundary.$this->eol;
1264 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1265 $out .=
"Content-Type: multipart/alternative;".$this->eol.
" boundary=\"".$this->alternative_boundary.
"\"".$this->eol;
1267 $out .=
"--".$this->alternative_boundary.$this->eol;
1268 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1270 $out .= $this->eol.$strContentAltText.$this->eol;
1271 $out .=
"--".$this->alternative_boundary.$this->eol;
1274 $out .=
"Content-Type: text/html; charset=".$conf->file->character_set_client.$this->eol;
1276 $out .= $this->eol.$strContent.$this->eol;
1278 if (!$this->atleastoneimage && $strContentAltText && !empty($conf->global->MAIN_MAIL_USE_MULTI_PART)) {
1279 $out .=
"--".$this->alternative_boundary.
"--".$this->eol;
1282 $out .=
"Content-Type: text/plain; charset=".$conf->file->character_set_client.$this->eol;
1284 $out .= $this->eol.$strContent.$this->eol;
1290 if ($this->atleastoneimage) {
1291 $out .= $this->write_images($this->images_encoded);
1293 $out .=
"--".$this->related_boundary.
"--".$this->eol;
1294 $out .= $this->eol.
"--".$this->alternative_boundary.
"--".$this->eol;
1310 public function write_files($filename_list, $mimetype_list, $mimefilename_list)
1315 $filename_list_size = count($filename_list);
1316 for ($i = 0; $i < $filename_list_size; $i++) {
1317 if ($filename_list[$i]) {
1319 $encoded = $this->_encode_file($filename_list[$i]);
1320 if ($encoded >= 0) {
1321 if ($mimefilename_list[$i]) {
1322 $filename_list[$i] = $mimefilename_list[$i];
1324 if (!$mimetype_list[$i]) {
1325 $mimetype_list[$i] =
"application/octet-stream";
1328 $out .=
"--".$this->mixed_boundary.$this->eol;
1329 $out .=
"Content-Disposition: attachment; filename=\"".$filename_list[$i].
"\"".$this->eol;
1330 $out .=
"Content-Type: ".$mimetype_list[$i].
"; name=\"".$filename_list[$i].
"\"".$this->eol;
1331 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1332 $out .=
"Content-Description: ".$filename_list[$i].$this->eol;
1359 if (is_array($images_list)) {
1360 foreach ($images_list as $img) {
1361 dol_syslog(
"CMailFile::write_images: ".$img[
"name"]);
1363 $out .=
"--".$this->related_boundary.$this->eol;
1364 $out .=
"Content-Type: ".$img[
"content_type"].
"; name=\"".$img[
"name"].
"\"".$this->eol;
1365 $out .=
"Content-Transfer-Encoding: base64".$this->eol;
1366 $out .=
"Content-Disposition: inline; filename=\"".$img[
"name"].
"\"".$this->eol;
1367 $out .=
"Content-ID: <".$img[
"cid"].
">".$this->eol;
1369 $out .= $img[
"image_encoded"];
1394 if (function_exists(
'fsockopen')) {
1395 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER';
1396 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT';
1397 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID';
1398 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW';
1399 $keyfortls =
'MAIN_MAIL_EMAIL_TLS';
1400 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS';
1401 if ($this->sendcontext ==
'emailing' && !empty($conf->global->MAIN_MAIL_SENDMODE_EMAILING) && $conf->global->MAIN_MAIL_SENDMODE_EMAILING !=
'default') {
1402 $keyforsmtpserver =
'MAIN_MAIL_SMTP_SERVER_EMAILING';
1403 $keyforsmtpport =
'MAIN_MAIL_SMTP_PORT_EMAILING';
1404 $keyforsmtpid =
'MAIN_MAIL_SMTPS_ID_EMAILING';
1405 $keyforsmtppw =
'MAIN_MAIL_SMTPS_PW_EMAILING';
1406 $keyfortls =
'MAIN_MAIL_EMAIL_TLS_EMAILING';
1407 $keyforstarttls =
'MAIN_MAIL_EMAIL_STARTTLS_EMAILING';
1411 if (!empty($conf->global->$keyfortls) && function_exists(
'openssl_open')) {
1412 $host =
'ssl://'.$host;
1417 dol_syslog(
"Try socket connection to host=".$host.
" port=".$port);
1419 if ($socket = @fsockopen(
1427 if (function_exists(
'stream_set_timeout')) {
1428 stream_set_timeout($socket, $timeout, 0);
1434 if ($_retVal = $this->server_parse($socket,
"220")) {
1438 $this->error =
utf8_check(
'Error '.$errno.
' - '.$errstr) ?
'Error '.$errno.
' - '.$errstr : utf8_encode(
'Error '.$errno.
' - '.$errstr);
1457 $server_response =
'';
1459 while (substr($server_response, 3, 1) !=
' ') {
1460 if (!($server_response = fgets($socket, 256))) {
1461 $this->error =
"Couldn't get mail server response codes";
1466 if (!(substr($server_response, 0, 3) == $response)) {
1467 $this->error =
"Ran into problems sending Mail.\r\nResponse: $server_response";
1483 $extensions = array_keys($this->image_types);
1486 preg_match_all(
'/(?:"|\')([^"\']+\.('.implode(
'|', $extensions).
'))(?:"|\')/Ui', $this->html, $matches);
1488 if (!empty($matches)) {
1490 foreach ($matches[1] as $full) {
1491 if (preg_match(
'/file=([A-Za-z0-9_\-\/]+[\.]?[A-Za-z0-9]+)?$/i', $full, $regs)) {
1494 if (file_exists($images_dir.
'/'.$img)) {
1496 $src = preg_quote($full,
'/');
1499 $this->html_images[$i][
"fullpath"] = $images_dir.
'/'.$img;
1502 $this->html_images[$i][
"name"] = $img;
1505 if (preg_match(
'/^.+\.(\w{3,4})$/', $img, $reg)) {
1506 $ext = strtolower($reg[1]);
1507 $this->html_images[$i][
"content_type"] = $this->image_types[$ext];
1511 $this->html_images[$i][
"cid"] =
dol_hash(uniqid(time()), 3);
1512 $this->html = preg_replace(
"/src=\"$src\"|src='$src'/i",
"src=\"cid:".$this->html_images[$i][
"cid"].
"\"", $this->html);
1518 if (!empty($this->html_images)) {
1523 foreach ($this->html_images as $img) {
1524 $fullpath = $images_dir.
'/'.$img[
"name"];
1527 if (!in_array($fullpath, $inline)) {
1529 if ($image = file_get_contents($fullpath)) {
1531 preg_match(
'/([A-Za-z0-9_-]+[\.]?[A-Za-z0-9]+)?$/i', $img[
"name"], $regs);
1532 $imgName = $regs[1];
1534 $this->images_encoded[$i][
'name'] = $imgName;
1535 $this->images_encoded[$i][
'fullpath'] = $fullpath;
1536 $this->images_encoded[$i][
'content_type'] = $img[
"content_type"];
1537 $this->images_encoded[$i][
'cid'] = $img[
"cid"];
1539 $this->images_encoded[$i][
"image_encoded"] = chunk_split(base64_encode($image), 68, $this->eol);
1540 $inline[] = $fullpath;
1570 public static function getValidAddress($address, $format, $encode = 0, $maxnumberofemail = 0)
1576 $arrayaddress = explode(
',', $address);
1580 foreach ($arrayaddress as $val) {
1582 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
1583 $name = trim($regs[1]);
1584 $email = trim($regs[2]);
1587 $email = trim($val);
1595 $newemail = $name ? $name : $email;
1596 $newemail =
'<a href="mailto:'.$email.
'">'.$newemail.
'</a>';
1599 $newemail = $name ? $name : $email;
1604 if ($format == 1 || $format == 3) {
1605 $newemail =
'<'.$email.
'>';
1607 if ($format == 0 || $format == 3) {
1608 if (!empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL)) {
1609 $newemail =
'<'.$email.
'>';
1611 $newemail =
'<'.$email.
'>';
1613 $newemail = ($format == 3 ?
'"' :
'').($encode ?self::encodetorfc2822($name) : $name).($format == 3 ?
'"' :
'').
' <'.$email.
'>';
1617 $ret = ($ret ? $ret.
',' :
'').$newemail;
1620 if ($maxnumberofemail && $i >= $maxnumberofemail) {
1621 if (count($arrayaddress) > $maxnumberofemail) {
1645 $arrayaddress = explode(
',', $address);
1648 foreach ($arrayaddress as $val) {
1649 if (preg_match(
'/^(.*)<(.*)>$/i', trim($val), $regs)) {
1650 $name = trim($regs[1]);
1651 $email = trim($regs[2]);
1654 $email = trim($val);
1657 $ret[$email] = empty($conf->global->MAIN_MAIL_NO_FULL_EMAIL) ? $name :
null;