dolibarr 24.0.0-beta
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';
51require_once '../lib/files.lib.php';
52require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
53require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
54require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';
55require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
56
57$langs->load("mails");
58
59/*
60 * Actions
61 */
62
63// None
64
65
66/*
67 * View
68 */
69
71
72// TODO Replace with ID of template
73if (GETPOSTISSET('template')) {
74 $templatefile = DOL_DOCUMENT_ROOT.'/install/doctemplates/maillayout/'.dol_sanitizeFileName(GETPOST('template')).'.html';
75
76 $content = file_get_contents($templatefile);
77
78 if ($content === false) {
79 print 'Failed to load template '.dol_escape_htmltag(GETPOST('template'));
80 exit;
81 }
82
83 // Define $urlwithroot
84 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
85 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
86 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
87
88 $mycompanyaddress = $mysoc->getFullAddress(0, ',', 1, '');
89
90 $specificSubstitutionArray = array(
91 '__TITLEOFMAILHOLDER__' => $langs->trans('TitleOfMailHolder'),
92 '__CONTENTOFMAILHOLDER__' => $langs->trans("ContentOfMailHolder"),
93 '__GRAY_RECTANGLE__' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAIAAABM5OhcAAABGklEQVR4nO3SwQ3AIBDAsNLJb3SWIEJC9gR5ZM3MB6f9twN4k7FIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIvEBtxYAkgpLmAeAAAAAElFTkSuQmCC',
94 '__LAST_NEWS__' => $langs->trans('LastNews'),
95 '__LIST_PRODUCTS___' => $langs->trans('ListProducts'),
96 '__SUBJECT__' => GETPOST('subject'),
97 // vars for company
98 '__MYCOMPANY_NAME__' => $mysoc->name,
99 '__MYCOMPANY_ADDRESS__' => $mycompanyaddress,
100 '__MYCOMPANY_EMAIL__' => $mysoc->email,
101 '__MYCOMPANY_PHONE__' => $mysoc->phone,
102 '__MYCOMPANY_PHONE_MOBILE__' => $mysoc->phone_mobile,
103 '__MYCOMPANY_FAX__' => $mysoc->fax,
104
105 '__MYCOMPANY_ADDRESS_WITH_PICTO__' => $mycompanyaddress ? '<img src="data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAiIHN0cm9rZS13aWR0aD0iNCIgdmlld0JveD0iMCAwIDI0IDI0IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxwYXRoIGQ9Ik0xMiAyMmM1LjUyIDAgMTAtNC40OCAxMC0xMFMxNy41MiAyIDEyIDIgMiA2LjQ4IDIgMTJzNC40OCAxMCAxMCAxMHoiLz4KICA8Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSIzIi8+Cjwvc3ZnPg==" style="height: 20px; width: 20px; display: inline-block; vertical-align: middle;"> ' . $mycompanyaddress : '',
106 '__MYCOMPANY_EMAIL_WITH_PICTO__' => $mysoc->email ? '<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCIgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0Ij4KICA8cmVjdCB4PSI0IiB5PSIxMiIgd2lkdGg9IjU2IiBoZWlnaHQ9IjQwIiByeD0iNCIgcnk9IjQiIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMCIgc3Ryb2tlLXdpZHRoPSI0Ii8+CiAgPHBvbHlsaW5lIHBvaW50cz0iNiwxNCAzMiwzNiA1OCwxNCIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwIiBzdHJva2Utd2lkdGg9IjQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4KPC9zdmc+" style="height: 20px; width: 20px; display: inline-block; vertical-align: middle;"> ' . $mysoc->email : '',
107 '__MYCOMPANY_PHONE_PRO_WITH_PICTO__' => $mysoc->phone ? '<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIHY3LjAuMCBieSBAYm9udGF3ZXNvbWUgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbSBMaWNlbnNlIC0gaHR0cHM6Ly9mb250YXdlc29tZS5jb20vbGljZW5zZS9mcmVlIENvcHlyaWdodCAyMDI1IEZvbnRjb25zLCBJbmMuLS0+PHBhdGggZD0iTTE2MC4yIDI1QzE1Mi4zIDYuMSAxMzEuNy0zLjkgMTEyLjEgMS40bC01LjUgMS41Yy02NC42IDE3LjYtMTE5LjggODAuMi0xMDMuNyAxNTYuNCAzNy4xIDE3NSA3NC44IDMxMi43IDM0OS44IDM0OS44IDc2LjMgMTYuMiAxMzguOC0zOS4xIDE1Ni40LTEwMy43bDEuNS01LjVjNS40LTE5LjctNC43LTQwLjMtMjMuNS00OC4xbC05Ny4zLTQwLjVjLTE2LjUtNi45LTM1LjYtMi4xLTQ3IDExLjhsLTM4LjYgNDcuMkMyMzMuOSAzMzUuNCAxNzcuMyAyNzcgMTQ0LjggMjA1LjNMMTg5IDE2OS4zYzEzLjktMTEuMyAxOC42LTMwLjQgMTEuOC00N0wxNjAuMiAyNXoiLz48L3N2Zz4=" style="height: 20px; width: 20px; display: inline-block; vertical-align: middle;"> ' . $mysoc->phone : '',
108 );
109
110 $listsocialnetworks = '';
111 // 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.
112 /*
113 foreach($mysoc->socialnetworks as $snkey => $snval) {
114 $listsocialnetworks .= $snkey;
115 }
116 */
117 $specificSubstitutionArray['__MYCOMPANY_SOCIAL_NETWORKS__'] = $listsocialnetworks;
118
119 if (!empty($mysoc->logo) && dol_is_file($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
120 $specificSubstitutionArray['__LOGO_URL__'] = $urlwithroot.'/viewimage.php?modulepart=mycompany&file='.urlencode('logos/'.$mysoc->logo);
121 } else {
122 $specificSubstitutionArray['__LOGO_URL__'] = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABkCAIAAABM5OhcAAABGklEQVR4nO3SwQ3AIBDAsNLJb3SWIEJC9gR5ZM3MB6f9twN4k7FIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIuEsUgYi4SxSBiLhLFIGIvEBtxYAkgpLmAeAAAAAElFTkSuQmCC';
123 }
124
125 $specificSubstitutionArray['__USERSIGNATURE__'] = empty($user->signature) ? '' : $user->signature;
126
127 if (GETPOST('fromtype') == 'user') {
128 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = empty($user->signature) ? '' : $user->signature;
129 } elseif (GETPOST('fromtype') == 'company') {
130 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = $mysoc->name.' '.$mysoc->email;
131 } elseif (GETPOST('fromtype') == 'main_from') {
132 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = $mysoc->name.' '.getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
133 } else {
134 // GETPOST('fromtype') is senderprofile_x_y (x = ID profile)
135 $specificSubstitutionArray['__SENDEREMAIL_SIGNATURE__'] = 'TODO Read database to get the signature of the profile';
136 }
137
138
139 // Must replace
140 // __SUBJECT__, __CONTENTOFMAILHOLDER__, __USERSIGNATURE__, __NEWS_LIST__, __PRODUCTS_LIST__
141 foreach ($specificSubstitutionArray as $key => $val) {
142 $content = str_replace($key, $val, $content);
143 }
144
145 // Parse all strings __(...)__ to replace with the translated value $langs->trans("...")
146 $langs->load("other");
147 $content = preg_replace_callback(
148 '/__\‍((.+)\‍)__/',
153 function ($matches) {
154 global $langs;
155 return $langs->trans($matches[1]);
156 },
157 $content);
158
159
160 $template = GETPOST('template', 'alpha');
161 // Get list of selected news or products
162 $selectedIdsStr = GETPOST('selectedPosts', 'alpha');
163 //$selectedPosts = array();
164 $selectedIds = json_decode($selectedIdsStr);
165 if (is_numeric($selectedIds)) {
166 $selectedIds = array($selectedIds);
167 }
168 /*if (is_array($selectedPostsStr)) {
169 $selectedPosts = explode(',', $selectedPostsStr);
170 }*/
171 // Remove entries that are empty
172 /*
173 foreach($selectedPosts as $key => $val) {
174 var_dump(empty($val));
175 if (empty($val)) {
176 unset($selectedPosts[$key]);
177 }
178 } */
179
180 if (!empty($selectedIds) && !empty($template) && is_array($selectedIds) ) {
181 $newsList = '';
182 $productList = '';
183 foreach ($selectedIds as $Id) {
184 if ($template == "news") {
185 $post = getNewsDetailsById($Id);
186
187 $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;">
188 <div style="flex-grow: 1; margin-right: 30px; max-width: 600px; margin-left: 100px;">
189 <h2 style="margin: 0; font-size: 1.5em;">' . (empty($post['title']) ? '' : dol_htmlentitiesbr($post['title'])) . '</h2>
190 <p style="margin: 10px 0; color: #555;">' . (empty($post['description']) ? '' : dol_htmlentitiesbr($post['description'])) . '</p>
191 <span style="display: block; margin-bottom: 5px; color: #888;">Created By: <strong>' . dol_htmlentitiesbr(empty($post['user_fullname']) ? '' : $post['user_fullname']) . '</strong></span>
192 <br>
193 <span style="display: block; color: #888;">' . dol_print_date((empty($post['date_creation']) ? dol_now() : $post['date_creation']), 'daytext', 'tzserver', $langs) . '</span>
194 </div>
195 <div style="flex-shrink: 0; margin-left: 100px; float: right;">
196 ' . (!empty($post['image']) ? '<img alt="Image" width="130px" height="130px" style="border-radius: 10px;" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=medias&file=' . dol_htmlentitiesbr($post['image']) . '">' : '<img alt="Gray rectangle" width="130px" height="130px" style="border-radius: 10px;" src="__GRAY_RECTANGLE__">') . '
197 </div>
198 </div>';
199 } elseif ($template == "product") {
200 $product = getProductForEmailTemplate($Id);
201
202 $productList .= '<div style="width:100%; padding: 20px;">
203 <div>
204 ' . (!empty($product['image']) ? dol_htmlentitiesbr($product['image']) : '<img alt="Gray rectangle" width="130px" height="130px" style="border-radius: 10px;" src="__GRAY_RECTANGLE__">') . '
205 </div>
206 <div>
207 <h2>'.(dol_htmlentitiesbr($product["ref"])).(empty($product["label"]) ? '' : ' - '.dol_htmlentitiesbr($product["label"])).'</h2>
208 <p style="margin: 10px 0; color: #555;">'. (empty($product['description']) ? '' : dol_htmlentitiesbr($product['description'])) .'</p>
209 </div>
210 </div>
211 ';
212 }
213 }
214
215 $content = str_replace('__NEWS_LIST__', $newsList, $content);
216 $content = str_replace('__PRODUCT_SELECTED__', $productList, $content);
217 } else {
218 $content = str_replace('__NEWS_LIST__', $langs->trans("SelectSomeArticlesOrEnterYourOwnContent"), $content);
219 $content = str_replace('__PRODUCT_SELECTED__', $langs->trans("SelectOneArticleOrEnterYourOwnContent"), $content);
220 }
221
222 foreach ($specificSubstitutionArray as $key => $val) {
223 $content = str_replace($key, $val, $content);
224 }
225
226 print $content;
227} else {
228 print 'No template ID provided or expired token';
229}
global $dolibarr_main_url_root
global $mysoc
getNewsDetailsById($postId)
Retrieves the details of a news post by its ID.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='gmt')
Return date for now.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_htmlentitiesbr($stringtoencode, $nl2brmode=0, $pagecodefrom='UTF-8', $removelasteolbr=1)
This function is called to encode a string into a HTML string but differs from htmlentities because a...
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.
getProductForEmailTemplate($id)
Retrieve and return product for mail template.