dolibarr 24.0.0-beta
send.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
3 * Copyright (C) 2020 Andreu Bisquerra Gaya <jove@bisquerra.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025-2026 MDW <mdeweerd@users.noreply.github.com>
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
27//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER', '1'); // Not disabled cause need to load personalized language
28//if (! defined('NOREQUIREDB')) define('NOREQUIREDB', '1'); // Not disabled cause need to load personalized language
29//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
30//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
31if (!defined('NOTOKENRENEWAL')) {
32 define('NOTOKENRENEWAL', '1');
33}
34if (!defined('NOREQUIREMENU')) {
35 define('NOREQUIREMENU', '1');
36}
37if (!defined('NOREQUIREHTML')) {
38 define('NOREQUIREHTML', '1');
39}
40if (!defined('NOREQUIREAJAX')) {
41 define('NOREQUIREAJAX', '1');
42}
43
44// Load Dolibarr environment
45require '../main.inc.php'; // Load $user and permissions
53require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
54require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
55
56$facid = GETPOSTINT('facid');
57$action = GETPOST('action', 'aZ09');
58$email = GETPOST('email', 'alpha');
59
60if (!$user->hasRight('takepos', 'run')) {
62}
63
64$langs->loadLangs(array("main", "bills", "cashdesk"));
65
66$invoice = new Facture($db);
67$invoice->fetch($facid);
68$customer = new Societe($db);
69$customer->fetch($invoice->socid);
70
71$error = 0;
72
73
74/*
75 * Actions
76 */
77
78if ($action == "send" && $user->hasRight('takepos', 'run')) {
79 top_httphead('text/html');
80
81 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
82 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
83 $formmail = new FormMail($db);
84 $outputlangs = new Translate('', $conf);
85 $model_id = getDolGlobalInt('TAKEPOS_EMAIL_TEMPLATE_INVOICE');
86 $arraydefaultmessage = $formmail->getEMailTemplate($db, 'facture_send', $user, $outputlangs, $model_id);
87
88 // Subject
89 $subject = $arraydefaultmessage->topic;
90
91 $receipt = '';
92
93 $joinFile = [];
94 $joinFileName = [];
95 $joinFileMime = [];
96
97 if (!getDolGlobalString('TAKEPOS_SEND_INVOICE_AS_PDF')) {
98 $nojs = 1; // used by include of takepos/receipt.php
99
100 ob_start(); // turn on output receipt
101 include DOL_DOCUMENT_ROOT.'/takepos/receipt.php';
102 $receipt = ob_get_contents(); // get the contents of the output buffer
103 ob_end_clean();
104 } else {
105 if ($arraydefaultmessage->joinfiles == 1 && !empty($invoice->last_main_doc)) {
106 // TODO
107 // Generate a new customer using the email, switch invoice to the new customer
108 // TODO Ask also name/lastname to complete the customer card
109 $joinFile[] = DOL_DATA_ROOT.'/'.$invoice->last_main_doc;
110 $joinFileName[] = basename($invoice->last_main_doc);
111 $joinFileMime[] = dol_mimetype(DOL_DATA_ROOT.'/'.$invoice->last_main_doc);
112 }
113 }
114
115 // From / To
116 $sendto = $email;
117 $from = $mysoc->email;
118
119 // Content
120 $msg = "<html>";
121 $msg .= $arraydefaultmessage->content;
122 if ($receipt) {
123 $msg .= "<br>";
124 $msg .= $receipt;
125 }
126 $msg .= "</html>";
127
128 // Send email
129 $mail = new CMailFile($subject, $sendto, $from, $msg, $joinFile, $joinFileMime, $joinFileName, '', '', 0, 1, '', '', '', '', '', '', DOL_DATA_ROOT.'/documents/takepos/temp');
130
131 if ($mail->error || !empty($mail->errors)) {
132 setEventMessages($mail->error, $mail->errors, 'errors');
133
134 print 'Failed to send email: '.$mail->error;
135
136 http_response_code(500);
137 } else {
138 $result = $mail->sendfile();
139 if ($result) {
140 $triggersendname = 'BILL_SENTBYMAIL';
141 $object = $invoice;
142 $object->context['email_from'] = $from;
143 $object->context['email_to'] = $sendto;
144 $object->context['email_msgid'] = $mail->msgid;
145
146 // Same code as in actions_sendmail.inc.php
147 // If sending email for invoice, we increase the counter of invoices sent by email
148 $sql = "UPDATE ".MAIN_DB_PREFIX."facture SET email_sent_counter = email_sent_counter + 1";
149 $sql .= " WHERE rowid = ".((int) $object->id);
150
151 $resql = $db->query($sql);
152 if ($resql) {
153 $object->email_sent_counter += 1;
154 }
155 // }
156
157 $result = $object->call_trigger($triggersendname, $user); // @phan-suppress-current-line PhanPossiblyUndeclaredGlobalVariable
158 if ($result < 0) {
159 $error++;
160 }
161 if ($error) {
162 setEventMessages($object->error, $object->errors, 'errors');
163 }
164
165 if (!$error) {
166 print 'Mail successfully sent to '.$sendto.' with subject '.$subject;
167 print "\n";
168 print 'If template ask to join file, it may include the file '.implode(',', $joinFile);
169 } else {
170 http_response_code(500);
171 }
172 } else {
173 print 'Failed to send email: '.$mail->error;
174
175 http_response_code(500);
176 }
177 }
178
179 exit;
180}
181
182
183/*
184 * View
185 */
186
187$arrayofcss = array('/takepos/css/pos.css.php');
188$arrayofjs = array();
189$head = '';
190
191top_htmlhead($head, '', 0, 0, $arrayofjs, $arrayofcss);
192
193?>
194<body class="center">
195
196<script>
197function SendMail() {
198 $.ajax({
199 type: "GET",
200 data: { token: '<?php echo currentToken(); ?>' },
201 url: '<?php print DOL_URL_ROOT.'/takepos/send.php?action=send&token='.newToken().'&facid='.((int) $facid).'&email='; ?>' + $("#email").val(),
202 success: function(response) {
203 console.log("Email sent");
204 alert("Email sent");
205 },
206 error: function(xhr, status, error) {
207 console.log("Failed to send email");
208 alert("Failed to send email : " + error);
209 }
210 });
211 parent.$.colorbox.close();
212}
213
214</script>
215
216<div class="center">
217<center>
218<center>
219<input type="email" id="email" name="email" style="width:60%;font-size: 200%;" value="<?php echo $customer->email; ?>"></center>
220</center>
221</div>
222<br>
223<div class="center">
224
225<button type="button" class="calcbutton" onclick="SendMail()"><?php print $langs->trans("SendTicket"); ?></button>
226
227</div>
228
229</body>
230</html>
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage invoices.
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:168
global $mysoc
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:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
multi select button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button
top_htmlhead($head, $title='', $disablejs=0, $disablehead=0, $arrayofjs=array(), $arrayofcss=array(), $disableforlogin=0, $disablenofollow=0, $disablenoindex=0)
Output html header of a page.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:133
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.