dolibarr 22.0.5
mailtemplate.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024-2025 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26// Just for display errors in editor
27ini_set('display_errors', 1);
28
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', '1'); // Disables token renewal
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', '1');
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', '1');
37}
38if (!defined('NOREQUIREAJAX')) {
39 define('NOREQUIREAJAX', '1');
40}
41require_once '../../main.inc.php';
42require_once '../lib/files.lib.php';
43require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
44require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
45require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';
46
57/*
58 * Actions
59 */
60
61// None
62
63
64/*
65 * View
66 */
67
69
70// TODO Replace with ID of template
71if (GETPOSTISSET('template')) {
72 $templatefile = DOL_DOCUMENT_ROOT.'/install/doctemplates/maillayout/'.dol_sanitizeFileName(GETPOST('template')).'.html';
73
74 $content = file_get_contents($templatefile);
75
76 if ($content === false) {
77 print 'Failed to load template '.dol_escape_htmltag(GETPOST('template'));
78 exit;
79 }
80
81 // Define $urlwithroot
82 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
83 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
84 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
85
86
87 $specificSubstitutionArray = array(
88 '__TITLEOFMAILHOLDER__' => $langs->trans('TitleOfMailHolder'),
89 '__CONTENTOFMAILHOLDER__' => 'Lorem ipsum ...',
90 '__GRAY_RECTANGLE__' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAIAAABM5OhcAAABGklEQVR4nO3SwQ3AIBDAsNLJb3SWIEJC9gR5ZM3MB6f9twN4k7FIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIvEBtxYAkgpLmAeAAAAAElFTkSuQmCC',
91 '__LAST_NEWS__' => $langs->trans('LastNews'),
92 '__LIST_PRODUCTS___' => $langs->trans('ListProducts'),
93 '__SUBJECT__' => GETPOST('subject'),
94 // vars for company
95 '__MYCOMPANY_ADDRESS__' => $mysoc->getFullAddress(0, '<br>', 1, ''),
96 '__MYCOMPANY_EMAIL__' => $mysoc->email,
97 '__MYCOMPANY_PHONE_PRO__' => $mysoc->phone_pro,
98 '__MYCOMPANY_PHONE_MOBILE__' => $mysoc->phone_mobile,
99 '__MYCOMPANY_FAX__' => $mysoc->fax,
100 );
101
102 $listsocialnetworks = '';
103 // TODO Add a column imgsrcdata into llx_c_socialnetworks to store the src data for image of the social network in black on a white background.
104 /*
105 foreach($mysoc->socialnetworks as $snkey => $snval) {
106 $listsocialnetworks .= $snkey;
107 }
108 */
109 $specificSubstitutionArray['__MYCOMPANY_SOCIAL_NETWORKS__'] = $listsocialnetworks;
110
111 if (!empty($mysoc->logo) && dol_is_file($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
112 $specificSubstitutionArray['__LOGO_URL__'] = $urlwithroot.'/viewimage.php?modulepart=mycompany&file='.urlencode('logos/'.$mysoc->logo);
113 } else {
114 $specificSubstitutionArray['__LOGO_URL__'] = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAIAAABM5OhcAAABGklEQVR4nO3SwQ3AIBDAsNLJb3SWIEJC9gR5ZM3MB6f9twN4k7FIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIvEBtxYAkgpLmAeAAAAAElFTkSuQmCC';
115 }
116
117 $specificSubstitutionArray['__USERSIGNATURE__'] = empty($user->signature) ? '' : $user->signature;
118
119 if (GETPOST('fromtype') == 'user') {
120 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = empty($user->signature) ? '' : $user->signature;
121 } elseif (GETPOST('fromtype') == 'company') {
122 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = $mysoc->name.' '.$mysoc->email;
123 } elseif (GETPOST('fromtype') == 'main_from') {
124 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = $mysoc->name.' '.getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
125 } else {
126 // GETPOST('fromtype') is senderprofile_x_y (x = ID profile)
127 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = 'TODO Read database to get the signature of the profile';
128 }
129
130
131 // Must replace
132 // __SUBJECT__, __CONTENTOFMAILHOLDER__, __USERSIGNATURE__, __NEWS_LIST__, __PRODUCTS_LIST__
133 foreach ($specificSubstitutionArray as $key => $val) {
134 $content = str_replace($key, $val, $content);
135 }
136
137 // Parse all strings __(...)__ to replace with the translated value $langs->trans("...")
138 $langs->load("other");
139 $content = preg_replace_callback(
140 '/__\‍((.+)\‍)__/',
145 function ($matches) {
146 global $langs;
147 return $langs->trans($matches[1]);
148 },
149 $content);
150
151
152 $selectedPostsStr = GETPOST('selectedPosts', 'alpha');
153 $selectedPosts = explode(',', $selectedPostsStr);
154
155 if (is_array($selectedPosts) && !empty($selectedPosts)) {
156 $newsList = '';
157
158 foreach ($selectedPosts as $postId) {
159 $post = getNewsDetailsById($postId);
160
161 $newsList .= '<div style="display: flex; align-items: flex-start; justify-content: flex-start; width: 100%; max-width: 800px; margin-top: 20px;margin-bottom: 50px; padding: 20px;">
162 <div style="flex-grow: 1; margin-right: 30px; max-width: 600px; margin-left: 100px;">
163 <h2 style="margin: 0; font-size: 1.5em;">' . htmlentities(empty($post['title']) ? '' : $post['title']) . '</h2>
164 <p style="margin: 10px 0; color: #555;">' . htmlentities(empty($post['description']) ? '' : $post['description']) . '</p>
165 <span style="display: block; margin-bottom: 5px; color: #888;">Created By: <strong>' . htmlentities(empty($post['user_fullname']) ? '' : $post['user_fullname']) . '</strong></span>
166 <br>
167 <span style="display: block; color: #888;">' . dol_print_date((empty($post['date_creation']) ? dol_now() : $post['date_creation']), 'daytext', 'tzserver', $langs) . '</span>
168 </div>
169 <div style="flex-shrink: 0; margin-left: 100px; float: right;">
170 ' . (!empty($post['image']) ? '<img alt="Image" width="130px" height="130px" style="border-radius: 10px;" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' . htmlentities($post['image']) . '">' : '<img alt="Gray rectangle" width="130px" height="130px" style="border-radius: 10px;" src="__GRAY_RECTANGLE__">') . '
171 </div>
172 </div>';
173 }
174
175 $content = str_replace('__NEWS_LIST__', $newsList, $content);
176 } else {
177 $content = str_replace('__NEWS_LIST__', 'No articles selected', $content);
178 }
179
180 print $content;
181} else {
182 print 'No template ID provided or expired token';
183}
global $dolibarr_main_url_root
getNewsDetailsById($postId)
Retrieves the details of a news post by its ID.
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79