1<?php
2/* Copyright (C) 2007-2020 Laurent Destailleur  <eldy@users.sourceforge.net>
3 * Copyright (C) 2009-2012 Regis Houssin        <regis.houssin@inodbox.com>
4 * Copyright (C) 2013	   Juanjo Menent		<jmenent@2byte.es>
5 * Copyright (C) 2016      Jonathan TISSEAU     <jonathan.tisseau@86dev.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21/**
22 *       \file       htdocs/admin/mails.php
23 *       \brief      Page to setup emails sending
24 */
25
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
29
30// Load translation files required by the page
31$langs->loadLangs(array("companies", "products", "admin", "mails", "other", "errors"));
32
33$action = GETPOST('action', 'aZ09');
34$cancel = GETPOST('cancel', 'aZ09');
35
36if (!$user->admin) accessforbidden();
37
38$usersignature = $user->signature;
39// For action = test or send, we ensure that content is not html, even for signature, because this we want a test with NO html.
40
41if ($action == 'test' || $action == 'send')
42{
43	$usersignature = dol_string_nohtmltag($usersignature, 2);
44}
45
46$substitutionarrayfortest = array(
47	'__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
48	'__ID__' => 'RecipientIdRecord',
49	//'__EMAIL__' => 'RecipientEMail',				// Done into actions_sendmails
50	'__CHECK_READ__' => (is_object($object) && is_object($object->thirdparty)) ? '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php?tag='.$object->thirdparty->tag.'&securitykey='.urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY).'" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '',
51	'__USER_SIGNATURE__' => (($user->signature && empty($conf->global->MAIN_MAIL_DO_NOT_USE_SIGN)) ? $usersignature : ''), // Done into actions_sendmails
52	'__LOGIN__' => 'RecipientLogin',
53	'__LASTNAME__' => 'RecipientLastname',
54	'__FIRSTNAME__' => 'RecipientFirstname',
55	'__ADDRESS__'=> 'RecipientAddress',
56	'__ZIP__'=> 'RecipientZip',
57	'__TOWN_'=> 'RecipientTown',
58	'__COUNTRY__'=> 'RecipientCountry'
59);
60complete_substitutions_array($substitutionarrayfortest, $langs);
61
62
63
64/*
65 * Actions
66 */
67
68if ($action == 'update' && !$cancel)
69{
70	if (!$error && !GETPOST("MAIN_MAIL_EMAIL_FROM", 'alphanohtml')) {
71		$error++;
72		setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MAIN_MAIL_EMAIL_FROM")), null, 'errors');
73		$action = 'edit';
74	}
75	if (!$error && !isValidEmail(GETPOST("MAIN_MAIL_EMAIL_FROM", 'alphanohtml'))) {
76		$error++;
77		setEventMessages($langs->trans("ErrorBadEMail", GETPOST("MAIN_MAIL_EMAIL_FROM", 'alphanohtml')), null, 'errors');
78		$action = 'edit';
79	}
80
81	if (!$error) {
82		dolibarr_set_const($db, "MAIN_DISABLE_ALL_MAILS", GETPOST("MAIN_DISABLE_ALL_MAILS", 'int'), 'chaine', 0, '', $conf->entity);
83		dolibarr_set_const($db, "MAIN_MAIL_FORCE_SENDTO", GETPOST("MAIN_MAIL_FORCE_SENDTO", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
84		dolibarr_set_const($db, "MAIN_MAIL_ENABLED_USER_DEST_SELECT", GETPOST("MAIN_MAIL_ENABLED_USER_DEST_SELECT", 'int'), 'chaine', 0, '', $conf->entity);
85		// Send mode parameters
86		dolibarr_set_const($db, "MAIN_MAIL_SENDMODE", GETPOST("MAIN_MAIL_SENDMODE", 'aZ09'), 'chaine', 0, '', $conf->entity);
87		dolibarr_set_const($db, "MAIN_MAIL_SMTP_PORT", GETPOST("MAIN_MAIL_SMTP_PORT", 'int'), 'chaine', 0, '', $conf->entity);
88		dolibarr_set_const($db, "MAIN_MAIL_SMTP_SERVER", GETPOST("MAIN_MAIL_SMTP_SERVER", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
89		dolibarr_set_const($db, "MAIN_MAIL_SMTPS_ID", GETPOST("MAIN_MAIL_SMTPS_ID", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
90		dolibarr_set_const($db, "MAIN_MAIL_SMTPS_PW", GETPOST("MAIN_MAIL_SMTPS_PW", 'none'), 'chaine', 0, '', $conf->entity);
91		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_TLS", GETPOST("MAIN_MAIL_EMAIL_TLS", 'int'), 'chaine', 0, '', $conf->entity);
92		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_STARTTLS", GETPOST("MAIN_MAIL_EMAIL_STARTTLS", 'int'), 'chaine', 0, '', $conf->entity);
93		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED", GETPOST("MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED", 'int'), 'chaine', 0, '', $conf->entity);
94
95		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_DKIM_ENABLED", GETPOST("MAIN_MAIL_EMAIL_DKIM_ENABLED", 'int'), 'chaine', 0, '', $conf->entity);
96		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_DKIM_DOMAIN", GETPOST("MAIN_MAIL_EMAIL_DKIM_DOMAIN", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
97		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_DKIM_SELECTOR", GETPOST("MAIN_MAIL_EMAIL_DKIM_SELECTOR", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
98		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY", GETPOST("MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
99		// Content parameters
100		dolibarr_set_const($db, "MAIN_MAIL_EMAIL_FROM", GETPOST("MAIN_MAIL_EMAIL_FROM", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
101		dolibarr_set_const($db, "MAIN_MAIL_ERRORS_TO", GETPOST("MAIN_MAIL_ERRORS_TO", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
102		dolibarr_set_const($db, "MAIN_MAIL_AUTOCOPY_TO", GETPOST("MAIN_MAIL_AUTOCOPY_TO", 'alphanohtml'), 'chaine', 0, '', $conf->entity);
103		dolibarr_set_const($db, 'MAIN_MAIL_DEFAULT_FROMTYPE', GETPOST('MAIN_MAIL_DEFAULT_FROMTYPE', 'alphanohtml'), 'chaine', 0, '', $conf->entity);
104
105		header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home&leftmenu=setup");
106		exit;
107	}
108}
109
110
111// Actions to send emails
112$id = 0;
113$actiontypecode = ''; // Not an event for agenda
114$triggersendname = ''; // Disable triggers
115$paramname = 'id';
116$mode = 'emailfortest';
117$trackid = (($action == 'testhtml') ? "testhtml" : "test");
118$sendcontext = '';
119include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
120
121if ($action == 'presend' && GETPOST('trackid', 'alphanohtml') == 'test')       $action = 'test';
122if ($action == 'presend' && GETPOST('trackid', 'alphanohtml') == 'testhtml')   $action = 'testhtml';
123
124
125
126
127/*
128 * View
129 */
130
131$form = new Form($db);
132
133$linuxlike = 1;
134if (preg_match('/^win/i', PHP_OS)) {
135	$linuxlike = 0;
136}
137if (preg_match('/^mac/i', PHP_OS)) {
138	$linuxlike = 0;
139}
140
141if (empty($conf->global->MAIN_MAIL_SENDMODE)) $conf->global->MAIN_MAIL_SENDMODE = 'mail';
142$port = !empty($conf->global->MAIN_MAIL_SMTP_PORT) ? $conf->global->MAIN_MAIL_SMTP_PORT : ini_get('smtp_port');
143if (!$port) $port = 25;
144$server = !empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : ini_get('SMTP');
145if (!$server) $server = '127.0.0.1';
146
147
148$wikihelp = 'EN:Setup_EMails|FR:Paramétrage_EMails|ES:Configuración_EMails';
149llxHeader('', $langs->trans("Setup"), $wikihelp);
150
151print load_fiche_titre($langs->trans("EMailsSetup"), '', 'title_setup');
152
153$head = email_admin_prepare_head();
154
155// List of sending methods
156$listofmethods = array();
157$listofmethods['mail'] = 'PHP mail function';
158$listofmethods['smtps'] = 'SMTP/SMTPS socket library';
159if (version_compare(phpversion(), '7.0', '>=')) $listofmethods['swiftmailer'] = 'Swift Mailer socket library';
160
161
162if ($action == 'edit')
163{
164	if ($conf->use_javascript_ajax)
165	{
166		print "\n".'<script type="text/javascript" language="javascript">';
167		print 'jQuery(document).ready(function () {
168                    function initfields()
169                    {
170                        if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'mail\')
171                        {
172							console.log("I choose php mail mode");
173                            jQuery(".drag").hide();
174                            jQuery("#MAIN_MAIL_EMAIL_TLS").val(0);
175                            jQuery("#MAIN_MAIL_EMAIL_TLS").prop("disabled", true);
176                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").val(0);
177                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").prop("disabled", true);
178                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").val(0);
179                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").prop("disabled", true);
180                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").val(0);
181                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").prop("disabled", true);
182                            jQuery("#MAIN_MAIL_EMAIL_DKIM_DOMAIN").prop("disabled", true);
183                            jQuery("#MAIN_MAIL_EMAIL_DKIM_SELECTOR").prop("disabled", true);
184                            jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").prop("disabled", true);
185                            jQuery(".smtp_method").hide();
186                            jQuery(".dkim").hide();
187                            ';
188		if ($linuxlike)
189		{
190			print '
191                            jQuery("#MAIN_MAIL_SMTP_SERVER").hide();
192                            jQuery("#MAIN_MAIL_SMTP_PORT").hide();
193                            jQuery("#smtp_server_mess").show();
194                            jQuery("#smtp_port_mess").show();';
195		} else {
196			print '
197                            jQuery("#MAIN_MAIL_SMTP_SERVER").prop("disabled", true);
198                            jQuery("#MAIN_MAIL_SMTP_PORT").prop("disabled", true);
199							jQuery("#smtp_server_mess").hide();
200                            jQuery("#smtp_port_mess").hide();';
201		}
202		print '
203                        }
204                        if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'smtps\')
205                        {
206							console.log("I choose smtps mode");
207                            jQuery(".drag").show();
208                            jQuery("#MAIN_MAIL_EMAIL_TLS").val('.$conf->global->MAIN_MAIL_EMAIL_TLS.');
209                            jQuery("#MAIN_MAIL_EMAIL_TLS").removeAttr("disabled");
210                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").val('.$conf->global->MAIN_MAIL_EMAIL_STARTTLS.');
211                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").removeAttr("disabled");
212                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").val('.$conf->global->MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED.');
213                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").removeAttr("disabled");
214                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").val(0);
215                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").prop("disabled", true);
216                            jQuery("#MAIN_MAIL_EMAIL_DKIM_DOMAIN").prop("disabled", true);
217                            jQuery("#MAIN_MAIL_EMAIL_DKIM_SELECTOR").prop("disabled", true);
218                            jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").prop("disabled", true);
219                            jQuery("#MAIN_MAIL_EMAIL_DKIM_DOMAIN").hide();
220                            jQuery("#MAIN_MAIL_EMAIL_DKIM_SELECTOR").hide();
221                            jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").hide();
222                            jQuery("#MAIN_MAIL_SMTP_SERVER").removeAttr("disabled");
223                            jQuery("#MAIN_MAIL_SMTP_PORT").removeAttr("disabled");
224                            jQuery("#MAIN_MAIL_SMTP_SERVER").show();
225                            jQuery("#MAIN_MAIL_SMTP_PORT").show();
226                            jQuery("#smtp_server_mess").hide();
227			                jQuery("#smtp_port_mess").hide();
228                            jQuery(".smtp_method").show();
229							jQuery(".dkim").hide();
230						}
231                        if (jQuery("#MAIN_MAIL_SENDMODE").val()==\'swiftmailer\')
232                        {
233							console.log("I choose swiftmailer mode");
234                            jQuery(".drag").show();
235                            jQuery("#MAIN_MAIL_EMAIL_TLS").val('.$conf->global->MAIN_MAIL_EMAIL_TLS.');
236                            jQuery("#MAIN_MAIL_EMAIL_TLS").removeAttr("disabled");
237                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").val('.$conf->global->MAIN_MAIL_EMAIL_STARTTLS.');
238                            jQuery("#MAIN_MAIL_EMAIL_STARTTLS").removeAttr("disabled");
239                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").val('.$conf->global->MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED.');
240                            jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").removeAttr("disabled");
241                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").val('.$conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED.');
242                            jQuery("#MAIN_MAIL_EMAIL_DKIM_ENABLED").removeAttr("disabled");
243                            jQuery("#MAIN_MAIL_EMAIL_DKIM_DOMAIN").removeAttr("disabled");
244                            jQuery("#MAIN_MAIL_EMAIL_DKIM_SELECTOR").removeAttr("disabled");
245                            jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").removeAttr("disabled");
246                            jQuery("#MAIN_MAIL_EMAIL_DKIM_DOMAIN").show();
247                            jQuery("#MAIN_MAIL_EMAIL_DKIM_SELECTOR").show();
248                            jQuery("#MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").show();
249                            jQuery("#MAIN_MAIL_SMTP_SERVER").removeAttr("disabled");
250                            jQuery("#MAIN_MAIL_SMTP_PORT").removeAttr("disabled");
251                            jQuery("#MAIN_MAIL_SMTP_SERVER").show();
252                            jQuery("#MAIN_MAIL_SMTP_PORT").show();
253                            jQuery("#smtp_server_mess").hide();
254                            jQuery("#smtp_port_mess").hide();
255							jQuery(".smtp_method").show();
256                            jQuery(".dkim").show();
257                        }
258                    }
259                    initfields();
260                    jQuery("#MAIN_MAIL_SENDMODE").change(function() {
261                        initfields();
262                    });
263                    jQuery("#MAIN_MAIL_EMAIL_TLS").change(function() {
264						if (jQuery("#MAIN_MAIL_EMAIL_TLS").val() == 1)
265							jQuery("#MAIN_MAIL_EMAIL_STARTTLS").val(0);
266						else
267							jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").val(0);
268					});
269					jQuery("#MAIN_MAIL_EMAIL_STARTTLS").change(function() {
270						if (jQuery("#MAIN_MAIL_EMAIL_STARTTLS").val() == 1)
271							jQuery("#MAIN_MAIL_EMAIL_TLS").val(0);
272						else
273							jQuery("#MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").val(0);
274                    });
275               })';
276		print '</script>'."\n";
277	}
278
279	print '<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
280	print '<input type="hidden" name="token" value="'.newToken().'">';
281	print '<input type="hidden" name="action" value="update">';
282
283	print dol_get_fiche_head($head, 'common', '', -1);
284
285	print '<span class="opacitymedium">'.$langs->trans("EMailsDesc")."</span><br>\n";
286	print "<br>\n";
287
288
289	clearstatcache();
290
291	print '<table class="noborder centpercent">';
292	print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
293
294	// Disable
295	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_MAILS").'</td><td>';
296	print $form->selectyesno('MAIN_DISABLE_ALL_MAILS', $conf->global->MAIN_DISABLE_ALL_MAILS, 1);
297	print '</td></tr>';
298
299	// Force e-mail recipient
300	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_FORCE_SENDTO").'</td><td>';
301	print '<input class="flat" name="MAIN_MAIL_FORCE_SENDTO" size="32" value="'.(!empty($conf->global->MAIN_MAIL_FORCE_SENDTO) ? $conf->global->MAIN_MAIL_FORCE_SENDTO : '').'" />';
302	print '</td></tr>';
303
304	print '</table>';
305
306	print '<br>';
307
308	print '<table class="noborder centpercent">';
309	print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("MAIN_MAIL_SENDMODE").'</td><td></td></tr>';
310
311	// Method
312	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SENDMODE").'</td><td>';
313
314	// SuperAdministrator access only
315	if ((empty($conf->global->MAIN_MODULE_MULTICOMPANY)) || ($user->admin && !$user->entity))
316	{
317		print $form->selectarray('MAIN_MAIL_SENDMODE', $listofmethods, $conf->global->MAIN_MAIL_SENDMODE);
318	} else {
319		$text = $listofmethods[$conf->global->MAIN_MAIL_SENDMODE];
320		if (empty($text)) $text = $langs->trans("Undefined");
321		$htmltext = $langs->trans("ContactSuperAdminForChange");
322		print $form->textwithpicto($text, $htmltext, 1, 'superadmin');
323		print '<input type="hidden" name="MAIN_MAIL_SENDMODE" value="'.$conf->global->MAIN_MAIL_SENDMODE.'">';
324	}
325	print '</td></tr>';
326
327	// Host server
328	print '<tr class="oddeven">';
329	if (!$conf->use_javascript_ajax && $linuxlike && $conf->global->MAIN_MAIL_SENDMODE == 'mail') {
330		print '<td>';
331		print $langs->trans("MAIN_MAIL_SMTP_SERVER_NotAvailableOnLinuxLike");
332		print '</td><td>';
333		print '<span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span>';
334		print '</td>';
335	} else {
336		print '<td>';
337		$mainserver = (!empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : '');
338		$smtpserver = ini_get('SMTP') ?ini_get('SMTP') : $langs->transnoentities("Undefined");
339		if ($linuxlike) print $langs->trans("MAIN_MAIL_SMTP_SERVER_NotAvailableOnLinuxLike");
340		else print $langs->trans("MAIN_MAIL_SMTP_SERVER", $smtpserver);
341		print '</td><td>';
342		// SuperAdministrator access only
343		if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity))
344		{
345			print '<input class="flat minwidth300" id="MAIN_MAIL_SMTP_SERVER" name="MAIN_MAIL_SMTP_SERVER" value="'.$mainserver.'" autocomplete="off">';
346			print '<input type="hidden" id="MAIN_MAIL_SMTP_SERVER_sav" name="MAIN_MAIL_SMTP_SERVER_sav" value="'.$mainserver.'">';
347			print '<span id="smtp_server_mess" class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span>';
348			print ' <span class="opacitymedium smtp_method">'.$langs->trans("SeeLinkToOnlineDocumentation").'</span>';
349		} else {
350			$text = !empty($mainserver) ? $mainserver : $smtpserver;
351			$htmltext = $langs->trans("ContactSuperAdminForChange");
352			print $form->textwithpicto($text, $htmltext, 1, 'superadmin');
353			print '<input type="hidden" id="MAIN_MAIL_SMTP_SERVER" name="MAIN_MAIL_SMTP_SERVER" value="'.$mainserver.'">';
354		}
355		print '</td>';
356	}
357	print '</tr>';
358
359	// Port
360	print '<tr class="oddeven"><td>';
361	if (!$conf->use_javascript_ajax && $linuxlike && $conf->global->MAIN_MAIL_SENDMODE == 'mail')
362	{
363		print $langs->trans("MAIN_MAIL_SMTP_PORT_NotAvailableOnLinuxLike");
364		print '</td><td>';
365		print '<span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span>';
366	} else {
367		$mainport = (!empty($conf->global->MAIN_MAIL_SMTP_PORT) ? $conf->global->MAIN_MAIL_SMTP_PORT : '');
368		$smtpport = ini_get('smtp_port') ?ini_get('smtp_port') : $langs->transnoentities("Undefined");
369		if ($linuxlike) print $langs->trans("MAIN_MAIL_SMTP_PORT_NotAvailableOnLinuxLike");
370		else print $langs->trans("MAIN_MAIL_SMTP_PORT", $smtpport);
371		print '</td><td>';
372		// SuperAdministrator access only
373		if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity))
374		{
375			print '<input class="flat" id="MAIN_MAIL_SMTP_PORT" name="MAIN_MAIL_SMTP_PORT" size="3" value="'.$mainport.'">';
376			print '<input type="hidden" id="MAIN_MAIL_SMTP_PORT_sav" name="MAIN_MAIL_SMTP_PORT_sav" value="'.$mainport.'">';
377			print '<span id="smtp_port_mess" class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span>';
378		} else {
379			$text = (!empty($mainport) ? $mainport : $smtpport);
380			$htmltext = $langs->trans("ContactSuperAdminForChange");
381			print $form->textwithpicto($text, $htmltext, 1, 'superadmin');
382			print '<input type="hidden" id="MAIN_MAIL_SMTP_PORT" name="MAIN_MAIL_SMTP_PORT" value="'.$mainport.'">';
383		}
384	}
385	print '</td></tr>';
386
387	// ID
388	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))))
389	{
390		$mainstmpid = (!empty($conf->global->MAIN_MAIL_SMTPS_ID) ? $conf->global->MAIN_MAIL_SMTPS_ID : '');
391		print '<tr class="drag drop oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_ID").'</td><td>';
392		// SuperAdministrator access only
393		if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity))
394		{
395			print '<input class="flat" name="MAIN_MAIL_SMTPS_ID" size="32" value="'.$mainstmpid.'">';
396		} else {
397			$htmltext = $langs->trans("ContactSuperAdminForChange");
398			print $form->textwithpicto($conf->global->MAIN_MAIL_SMTPS_ID, $htmltext, 1, 'superadmin');
399			print '<input type="hidden" name="MAIN_MAIL_SMTPS_ID" value="'.$mainstmpid.'">';
400		}
401		print '</td></tr>';
402	}
403
404	// PW
405	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))))
406	{
407		$mainsmtppw = (!empty($conf->global->MAIN_MAIL_SMTPS_PW) ? $conf->global->MAIN_MAIL_SMTPS_PW : '');
408		print '<tr class="drag drop oddeven"><td>';
409		print $form->textwithpicto($langs->trans("MAIN_MAIL_SMTPS_PW"), $langs->trans("WithGMailYouCanCreateADedicatedPassword"));
410		print '</td><td>';
411		// SuperAdministrator access only
412		if (empty($conf->multicompany->enabled) || ($user->admin && !$user->entity))
413		{
414			print '<input class="flat" type="password" name="MAIN_MAIL_SMTPS_PW" size="32" value="'.$mainsmtppw.'" autocomplete="off">';
415		} else {
416			$htmltext = $langs->trans("ContactSuperAdminForChange");
417			print $form->textwithpicto($conf->global->MAIN_MAIL_SMTPS_PW, $htmltext, 1, 'superadmin');
418			print '<input type="hidden" name="MAIN_MAIL_SMTPS_PW" value="'.$mainsmtppw.'">';
419		}
420		print '</td></tr>';
421	}
422
423	// TLS
424	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_TLS").'</td><td>';
425	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))))
426	{
427		if (function_exists('openssl_open'))
428		{
429			print $form->selectyesno('MAIN_MAIL_EMAIL_TLS', (!empty($conf->global->MAIN_MAIL_EMAIL_TLS) ? $conf->global->MAIN_MAIL_EMAIL_TLS : 0), 1);
430		} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
431	} else print yn(0).' ('.$langs->trans("NotSupported").')';
432	print '</td></tr>';
433
434	// STARTTLS
435	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_STARTTLS").'</td><td>';
436	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))))
437	{
438		if (function_exists('openssl_open'))
439		{
440			print $form->selectyesno('MAIN_MAIL_EMAIL_STARTTLS', (!empty($conf->global->MAIN_MAIL_EMAIL_STARTTLS) ? $conf->global->MAIN_MAIL_EMAIL_STARTTLS : 0), 1);
441		} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
442	} else print yn(0).' ('.$langs->trans("NotSupported").')';
443	print '</td></tr>';
444
445	// SMTP_ALLOW_SELF_SIGNED
446	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").'</td><td>';
447	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer'))))
448	{
449		if (function_exists('openssl_open'))
450		{
451			print $form->selectyesno('MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED', (!empty($conf->global->MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED) ? $conf->global->MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED : 0), 1);
452		} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
453	} else print yn(0).' ('.$langs->trans("NotSupported").')';
454	print '</td></tr>';
455
456	// DKIM
457	print '<tr class="oddeven dkim"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_ENABLED").'</td><td>';
458	if (!empty($conf->use_javascript_ajax) || (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('swiftmailer'))))
459	{
460		if (function_exists('openssl_open'))
461		{
462			print $form->selectyesno('MAIN_MAIL_EMAIL_DKIM_ENABLED', (!empty($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED) ? $conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED : 0), 1);
463		} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
464	} else print yn(0).' ('.$langs->trans("NotSupported").')';
465	print '</td></tr>';
466
467	// DKIM Domain
468	print '<tr class="oddeven dkim"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_DOMAIN").'</td>';
469	print '<td><input class="flat" id="MAIN_MAIL_EMAIL_DKIM_DOMAIN" name="MAIN_MAIL_EMAIL_DKIM_DOMAIN" size="32" value="'.(!empty($conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN) ? $conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN : '');
470	print '"></td></tr>';
471
472	// DKIM Selector
473	print '<tr class="oddeven dkim"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_SELECTOR").'</td>';
474	print '<td><input class="flat" id="MAIN_MAIL_EMAIL_DKIM_SELECTOR" name="MAIN_MAIL_EMAIL_DKIM_SELECTOR" size="32" value="'.(!empty($conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR) ? $conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR : '');
475	print '"></td></tr>';
476
477	// DKIM PRIVATE KEY
478	print '<tr class="oddeven dkim"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").'</td>';
479	print '<td><textarea id="MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY" name="MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY" rows="15" cols="100">'.(!empty($conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY) ? $conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY : '').'</textarea>';
480	print '</td></tr>';
481
482	print '</table>';
483
484	print '<br>';
485
486	print '<table class="noborder centpercent">';
487	print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("OtherOptions").'</td><td></td></tr>';
488
489	// From
490	print '<tr class="oddeven"><td class="fieldrequired">'.$langs->trans("MAIN_MAIL_EMAIL_FROM", ini_get('sendmail_from') ?ini_get('sendmail_from') : $langs->transnoentities("Undefined")).'</td>';
491	print '<td><input class="flat minwidth200" name="MAIN_MAIL_EMAIL_FROM" value="'.(!empty($conf->global->MAIN_MAIL_EMAIL_FROM) ? $conf->global->MAIN_MAIL_EMAIL_FROM : '');
492	print '"></td></tr>';
493
494	// Default from type
495	$liste = array();
496	$liste['user'] = $langs->trans('UserEmail');
497	$liste['company'] = $langs->trans('CompanyEmail').' ('.(empty($conf->global->MAIN_INFO_SOCIETE_MAIL) ? $langs->trans("NotDefined") : $conf->global->MAIN_INFO_SOCIETE_MAIL).')';
498
499	print '<tr class="oddeven"><td>'.$langs->trans('MAIN_MAIL_DEFAULT_FROMTYPE').'</td><td>';
500	print $form->selectarray('MAIN_MAIL_DEFAULT_FROMTYPE', $liste, $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE, 0);
501	print '</td></tr>';
502
503	// From
504	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ERRORS_TO").'</td>';
505	print '<td><input class="flat" name="MAIN_MAIL_ERRORS_TO" size="32" value="'.(!empty($conf->global->MAIN_MAIL_ERRORS_TO) ? $conf->global->MAIN_MAIL_ERRORS_TO : '');
506	print '"></td></tr>';
507
508	// Autocopy to
509	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_AUTOCOPY_TO").'</td>';
510	print '<td><input class="flat" name="MAIN_MAIL_AUTOCOPY_TO" size="32" value="'.(!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO) ? $conf->global->MAIN_MAIL_AUTOCOPY_TO : '');
511	print '"></td></tr>';
512
513	// Add user to select destinaries list
514	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").'</td><td>';
515	print $form->selectyesno('MAIN_MAIL_ENABLED_USER_DEST_SELECT', $conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT, 1);
516	print '</td></tr>';
517
518	print '</table>';
519
520	print dol_get_fiche_end();
521
522	print '<br><div class="center">';
523	print '<input class="button button-save" type="submit" name="save" value="'.$langs->trans("Save").'">';
524	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
525	print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
526	print '</div>';
527
528	print '</form>';
529} else {
530	print dol_get_fiche_head($head, 'common', '', -1);
531
532	print '<span class="opacitymedium">'.$langs->trans("EMailsDesc")."</span><br>\n";
533	print "<br>\n";
534
535	print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
536	print '<table class="noborder centpercent">';
537	print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
538
539	// Disable
540	print '<tr class="oddeven"><td>'.$langs->trans("MAIN_DISABLE_ALL_MAILS").'</td><td>'.yn($conf->global->MAIN_DISABLE_ALL_MAILS);
541	if (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) print img_warning($langs->trans("Disabled"));
542	print '</td></tr>';
543
544	if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
545		// Force e-mail recipient
546		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_FORCE_SENDTO").'</td><td>'.$conf->global->MAIN_MAIL_FORCE_SENDTO;
547		if (!empty($conf->global->MAIN_MAIL_FORCE_SENDTO))
548		{
549			if (!isValidEmail($conf->global->MAIN_MAIL_FORCE_SENDTO)) print img_warning($langs->trans("ErrorBadEMail"));
550			else print img_warning($langs->trans("RecipientEmailsWillBeReplacedWithThisValue"));
551		}
552		print '</td></tr>';
553	}
554
555	print '</table>';
556	print '</div>';
557
558	if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
559		print '<br>';
560
561		print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
562		print '<table class="noborder centpercent">';
563		print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("MAIN_MAIL_SENDMODE").'</td><td></td></tr>';
564
565		// Method
566		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SENDMODE").'</td><td>';
567		$text = $listofmethods[$conf->global->MAIN_MAIL_SENDMODE];
568		if (empty($text)) $text = $langs->trans("Undefined").img_warning();
569		print $text;
570
571		if ($conf->global->MAIN_MAIL_SENDMODE == 'mail' && empty($conf->global->MAIN_HIDE_WARNING_TO_ENCOURAGE_SMTP_SETUP)) {
572			print $form->textwithpicto('', $langs->trans("WarningPHPMail").'<br>'.$langs->trans("WarningPHPMailA").'<br>'.$langs->trans("WarningPHPMailB").'<br>'.$langs->trans("WarningPHPMailC").'<br><br>'.$langs->trans("WarningPHPMailD"), 1, 'warning');
573		}
574
575		print '</td></tr>';
576
577		// Host server
578		if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail'))
579		{
580			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_SERVER_NotAvailableOnLinuxLike").'</td><td><span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span></td></tr>';
581		} else {
582			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_SERVER", ini_get('SMTP') ?ini_get('SMTP') : $langs->transnoentities("Undefined")).'</td><td>'.(!empty($conf->global->MAIN_MAIL_SMTP_SERVER) ? $conf->global->MAIN_MAIL_SMTP_SERVER : '').'</td></tr>';
583		}
584
585		// Port
586		if ($linuxlike && (isset($conf->global->MAIN_MAIL_SENDMODE) && $conf->global->MAIN_MAIL_SENDMODE == 'mail'))
587		{
588			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_PORT_NotAvailableOnLinuxLike").'</td><td><span class="opacitymedium">'.$langs->trans("SeeLocalSendMailSetup").'</span></td></tr>';
589		} else {
590			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTP_PORT", ini_get('smtp_port') ?ini_get('smtp_port') : $langs->transnoentities("Undefined")).'</td><td>'.(!empty($conf->global->MAIN_MAIL_SMTP_PORT) ? $conf->global->MAIN_MAIL_SMTP_PORT : '').'</td></tr>';
591		}
592
593		// SMTPS ID
594		if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))
595		{
596			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_ID").'</td><td>'.$conf->global->MAIN_MAIL_SMTPS_ID.'</td></tr>';
597		}
598
599		// SMTPS PW
600		if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))
601		{
602			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_SMTPS_PW").'</td><td>'.preg_replace('/./', '*', $conf->global->MAIN_MAIL_SMTPS_PW).'</td></tr>';
603		}
604
605		// TLS
606		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_TLS").'</td><td>';
607		if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))
608		{
609			if (function_exists('openssl_open'))
610			{
611				print yn($conf->global->MAIN_MAIL_EMAIL_TLS);
612			} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
613		} else print '<span class="opacitymedium">'.yn(0).' ('.$langs->trans("NotSupported").')</span>';
614		print '</td></tr>';
615
616		// STARTTLS
617		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_STARTTLS").'</td><td>';
618		if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))
619		{
620			if (function_exists('openssl_open'))
621			{
622				print yn($conf->global->MAIN_MAIL_EMAIL_STARTTLS);
623			} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
624		} else print '<span class="opacitymedium">'.yn(0).' ('.$langs->trans("NotSupported").')</span>';
625		print '</td></tr>';
626
627		// SMTP_ALLOW_SELF_SIGNED
628		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED").'</td><td>';
629		if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('smtps', 'swiftmailer')))
630		{
631			if (function_exists('openssl_open'))
632			{
633				print yn($conf->global->MAIN_MAIL_EMAIL_SMTP_ALLOW_SELF_SIGNED);
634			} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
635		} else print '<span class="opacitymedium">'.yn(0).' ('.$langs->trans("NotSupported").')</span>';
636		print '</td></tr>';
637
638
639		if ($conf->global->MAIN_MAIL_SENDMODE == 'swiftmailer')
640		{
641			// DKIM
642			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_ENABLED").'</td><td>';
643			if (isset($conf->global->MAIN_MAIL_SENDMODE) && in_array($conf->global->MAIN_MAIL_SENDMODE, array('swiftmailer')))
644			{
645				if (function_exists('openssl_open'))
646				{
647					print yn($conf->global->MAIN_MAIL_EMAIL_DKIM_ENABLED);
648				} else print yn(0).' ('.$langs->trans("YourPHPDoesNotHaveSSLSupport").')';
649			} else print yn(0).' ('.$langs->trans("NotSupported").')';
650			print '</td></tr>';
651
652			// Domain
653			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_DOMAIN").'</td>';
654			print '<td>'.$conf->global->MAIN_MAIL_EMAIL_DKIM_DOMAIN;
655			print '</td></tr>';
656
657			// Selector
658			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_SELECTOR").'</td>';
659			print '<td>'.$conf->global->MAIN_MAIL_EMAIL_DKIM_SELECTOR;
660			print '</td></tr>';
661
662			// PRIVATE KEY
663			print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY").'</td>';
664			print '<td>'.$conf->global->MAIN_MAIL_EMAIL_DKIM_PRIVATE_KEY;
665			print '</td></tr>';
666		}
667
668		print '</table>';
669		print '</div>';
670
671		if ($conf->global->MAIN_MAIL_SENDMODE == 'mail' && empty($conf->global->MAIN_HIDE_WARNING_TO_ENCOURAGE_SMTP_SETUP)) {
672			print info_admin($langs->trans("WarningPHPMail").'<br>'.$langs->trans("WarningPHPMailA").'<br>'.$langs->trans("WarningPHPMailB").'<br>'.$langs->trans("WarningPHPMailC").'<br><br>'.$langs->trans("WarningPHPMailD"), 0, 0, 'warning');
673		}
674
675		print '<br>';
676
677		print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
678		print '<table class="noborder centpercent">';
679		print '<tr class="liste_titre"><td class="titlefieldmiddle">'.$langs->trans("OtherOptions").'</td><td></td></tr>';
680
681		// From
682		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_EMAIL_FROM", ini_get('sendmail_from') ?ini_get('sendmail_from') : $langs->transnoentities("Undefined")).'</td>';
683		print '<td>'.$conf->global->MAIN_MAIL_EMAIL_FROM;
684		if (empty($conf->global->MAIN_MAIL_EMAIL_FROM)) print img_warning($langs->trans("Mandatory"));
685		elseif (!isValidEmail($conf->global->MAIN_MAIL_EMAIL_FROM)) print img_warning($langs->trans("ErrorBadEMail"));
686		print '</td></tr>';
687
688		// Default from type
689		$liste = array();
690		$liste['user'] = $langs->trans('UserEmail');
691		$liste['company'] = $langs->trans('CompanyEmail').' ('.(empty($conf->global->MAIN_INFO_SOCIETE_MAIL) ? $langs->trans("NotDefined") : $conf->global->MAIN_INFO_SOCIETE_MAIL).')';
692		$sql = 'SELECT rowid, label, email FROM '.MAIN_DB_PREFIX.'c_email_senderprofile';
693		$sql .= ' WHERE active = 1 AND (private = 0 OR private = '.$user->id.')';
694		$resql = $db->query($sql);
695		if ($resql)
696		{
697			$num = $db->num_rows($resql);
698			$i = 0;
699			while ($i < $num)
700			{
701				$obj = $db->fetch_object($resql);
702				if ($obj)
703				{
704					$liste['senderprofile_'.$obj->rowid] = $obj->label.' <'.$obj->email.'>';
705				}
706				$i++;
707			}
708		} else dol_print_error($db);
709
710		print '<tr class="oddeven"><td>'.$langs->trans('MAIN_MAIL_DEFAULT_FROMTYPE').'</td>';
711		print '<td>';
712		if ($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE === 'robot')
713		{
714			print $langs->trans('RobotEmail');
715		} elseif ($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE === 'user')
716		{
717			print $langs->trans('UserEmail');
718		} elseif ($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE === 'company')
719		{
720			print $langs->trans('CompanyEmail').' '.dol_escape_htmltag('<'.$mysoc->email.'>');
721		} else {
722			$id = preg_replace('/senderprofile_/', '', $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE);
723			if ($id > 0)
724			{
725				include_once DOL_DOCUMENT_ROOT.'/core/class/emailsenderprofile.class.php';
726				$emailsenderprofile = new EmailSenderProfile($db);
727				$emailsenderprofile->fetch($id);
728				print $emailsenderprofile->label.' '.dol_escape_htmltag('<'.$emailsenderprofile->email.'>');
729			}
730		}
731		print '</td></tr>';
732
733		// Errors To
734		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ERRORS_TO").'</td>';
735		print '<td>'.$conf->global->MAIN_MAIL_ERRORS_TO;
736		if (!empty($conf->global->MAIN_MAIL_ERRORS_TO) && !isValidEmail($conf->global->MAIN_MAIL_ERRORS_TO)) print img_warning($langs->trans("ErrorBadEMail"));
737		print '</td></tr>';
738
739		// Autocopy to
740		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_AUTOCOPY_TO").'</td>';
741		print '<td>';
742		if (!empty($conf->global->MAIN_MAIL_AUTOCOPY_TO))
743		{
744			print $conf->global->MAIN_MAIL_AUTOCOPY_TO;
745			if (!isValidEmail($conf->global->MAIN_MAIL_AUTOCOPY_TO)) print img_warning($langs->trans("ErrorBadEMail"));
746		} else {
747			print '&nbsp;';
748		}
749		print '</td></tr>';
750
751		//Add user to select destinaries list
752		print '<tr class="oddeven"><td>'.$langs->trans("MAIN_MAIL_ENABLED_USER_DEST_SELECT").'</td><td>'.yn($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT).'</td></tr>';
753
754		print '</table>';
755		print '</div>';
756	}
757
758	print dol_get_fiche_end();
759
760
761	// Actions button
762	print '<div class="tabsAction">';
763
764	print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>';
765
766	if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
767		if ($conf->global->MAIN_MAIL_SENDMODE != 'mail' || !$linuxlike)
768		{
769			if (function_exists('fsockopen') && $port && $server)
770			{
771				print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testconnect&date='.dol_now().'#formmailaftertstconnect">'.$langs->trans("DoTestServerAvailability").'</a>';
772			}
773		} else {
774			print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("FeatureNotAvailableOnLinux").'">'.$langs->trans("DoTestServerAvailability").'</a>';
775		}
776
777		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test&mode=init#formmailbeforetitle">'.$langs->trans("DoTestSend").'</a>';
778
779		if (!empty($conf->fckeditor->enabled))
780		{
781			print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=testhtml&mode=init#formmailbeforetitle">'.$langs->trans("DoTestSendHTML").'</a>';
782		}
783	}
784
785	print '</div>';
786
787
788	if ($conf->global->MAIN_MAIL_SENDMODE == 'mail' && empty($conf->global->MAIN_FIX_FOR_BUGGED_MTA))
789	{
790		/*
791		 // Warning 1
792		 if ($linuxlike)
793		 {
794		 $sendmailoption=ini_get('mail.force_extra_parameters');
795		 if (empty($sendmailoption) || ! preg_match('/ba/',$sendmailoption))
796		 {
797		 print info_admin($langs->trans("SendmailOptionNotComplete"));
798		 }
799		 }*/
800		// Warning 2
801		print info_admin($langs->trans("SendmailOptionMayHurtBuggedMTA"));
802	}
803
804	if (!in_array($action, array('testconnect', 'test', 'testhtml')))
805	{
806		$text = '';
807		if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
808		{
809			//$text .= $langs->trans("WarningPHPMail"); // To encourage to use SMTPS
810		}
811
812		if ($conf->global->MAIN_MAIL_SENDMODE == 'mail')
813		{
814			if (!empty($conf->global->MAIN_EXTERNAL_MAIL_SPF_STRING_TO_ADD))
815			{
816				// List of string to add in SPF if the setup use the mail method. Example 'include:sendgrid.net include:spf.mydomain.com'
817				$text .= ($text ? '<br><br>' : '').$langs->trans("WarningPHPMailSPF", $conf->global->MAIN_EXTERNAL_MAIL_SPF_STRING_TO_ADD);
818			} else {
819				// MAIN_EXTERNAL_SMTP_CLIENT_IP_ADDRESS is list of IPs where email is sent from. Example: '1.2.3.4, [aaaa:bbbb:cccc:dddd]'.
820				if (!empty($conf->global->MAIN_EXTERNAL_SMTP_CLIENT_IP_ADDRESS))
821				{
822					// List of IP show as record to add in SPF if we use the mail method
823					$text .= ($text ? '<br><br>' : '').$langs->trans("WarningPHPMailSPF", $conf->global->MAIN_EXTERNAL_SMTP_CLIENT_IP_ADDRESS);
824				}
825			}
826		} else {
827			if (!empty($conf->global->MAIN_EXTERNAL_SMTP_CLIENT_IP_ADDRESS))
828			{
829				// List of IP show as record to add as allowed IP if we use the smtp method. Value is '1.2.3.4, [aaaa:bbbb:cccc:dddd]'
830				// TODO Add a key to allow to show the IP/name of server detected dynamically
831				$text .= ($text ? '<br><br>' : '').$langs->trans("WarningPHPMail2", $conf->global->MAIN_EXTERNAL_SMTP_CLIENT_IP_ADDRESS);
832			}
833			if (!empty($conf->global->MAIN_EXTERNAL_SMTP_SPF_STRING_TO_ADD))	// Should be required only if you have preset the Dolibarr to use your own SMTP and you want to warn users to update their domain name to match your SMTP server.
834			{
835				// List of string to add in SPF if we use the smtp method. Example 'include:spf.mydomain.com'
836				$text .= ($text ? '<br><br>' : '').$langs->trans("WarningPHPMailSPF", $conf->global->MAIN_EXTERNAL_SMTP_SPF_STRING_TO_ADD);
837			}
838		}
839
840
841		if ($text) print info_admin($text);
842	}
843
844	// Run the test to connect
845	if ($action == 'testconnect')
846	{
847		print '<div id="formmailaftertstconnect" name="formmailaftertstconnect"></div>';
848		print load_fiche_titre($langs->trans("DoTestServerAvailability"));
849
850		include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
851		$mail = new CMailFile('', '', '', '', array(), array(), array(), '', '', 0, '', '', '', '', $trackid, $sendcontext);
852		$result = $mail->check_server_port($server, $port);
853		if ($result) print '<div class="ok">'.$langs->trans("ServerAvailableOnIPOrPort", $server, $port).'</div>';
854		else {
855			$errormsg = $langs->trans("ServerNotAvailableOnIPOrPort", $server, $port);
856
857			if ($mail->error) {
858				$errormsg .= ' - '.$mail->error;
859			}
860
861			setEventMessages($errormsg, null, 'errors');
862			print $errormsg;
863		}
864		print '<br>';
865	}
866
867	// Show email send test form
868	if ($action == 'test' || $action == 'testhtml')
869	{
870		print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
871		print load_fiche_titre($action == 'testhtml' ? $langs->trans("DoTestSendHTML") : $langs->trans("DoTestSend"));
872
873		print dol_get_fiche_head('');
874
875		// Cree l'objet formulaire mail
876		include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
877		$formmail = new FormMail($db);
878		$formmail->trackid = (($action == 'testhtml') ? "testhtml" : "test");
879		$formmail->fromname = (GETPOSTISSET('fromname') ? $_POST['fromname'] : $conf->global->MAIN_MAIL_EMAIL_FROM);
880		$formmail->frommail = (GETPOSTISSET('frommail') ? $_POST['frommail'] : $conf->global->MAIN_MAIL_EMAIL_FROM);
881		$formmail->fromid = $user->id;
882		$formmail->fromalsorobot = 1;
883		$formmail->fromtype = (GETPOSTISSET('fromtype') ?GETPOST('fromtype', 'aZ09') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
884		$formmail->withfromreadonly = 1;
885		$formmail->withsubstit = 1;
886		$formmail->withfrom = 1;
887		$formmail->witherrorsto = 1;
888		$formmail->withto = (!empty($_POST['sendto']) ? GETPOST('sendto', 'restricthtml') : ($user->email ? $user->email : 1));
889		$formmail->withtocc = (!empty($_POST['sendtocc']) ? GETPOST('sendtocc', 'restricthtml') : 1); // ! empty to keep field if empty
890		$formmail->withtoccc = (!empty($_POST['sendtoccc']) ? GETPOST('sendtoccc', 'restricthtml') : 1); // ! empty to keep field if empty
891		$formmail->withtopic = (GETPOSTISSET('subject') ? GETPOST('subject') : $langs->trans("Test"));
892		$formmail->withtopicreadonly = 0;
893		$formmail->withfile = 2;
894		$formmail->withbody = (GETPOSTISSET('message') ? GETPOST('message', 'restricthtml') : ($action == 'testhtml' ? $langs->transnoentities("PredefinedMailTestHtml") : $langs->transnoentities("PredefinedMailTest")));
895		$formmail->withbodyreadonly = 0;
896		$formmail->withcancel = 1;
897		$formmail->withdeliveryreceipt = 1;
898		$formmail->withfckeditor = ($action == 'testhtml' ? 1 : 0);
899		$formmail->ckeditortoolbar = 'dolibarr_mailings';
900		// Tableau des substitutions
901		$formmail->substit = $substitutionarrayfortest;
902		// Tableau des parametres complementaires du post
903		$formmail->param["action"] = "send";
904		$formmail->param["models"] = "body";
905		$formmail->param["mailid"] = 0;
906		$formmail->param["returnurl"] = $_SERVER["PHP_SELF"];
907
908		// Init list of files
909		if (GETPOST("mode", "aZ09") == 'init')
910		{
911			$formmail->clear_attached_files();
912		}
913
914		print $formmail->get_form('addfile', 'removefile');
915
916		print dol_get_fiche_end();
917
918		// References
919		print '<span class="opacitymedium">'.$langs->trans("EMailsWillHaveMessageID").': ';
920		print dol_escape_htmltag('<timestamp.*@'.dol_getprefix('email').'>');
921		print '</span>';
922	}
923}
924
925// End of page
926llxFooter();
927$db->close();
928