· 6 years ago · Jun 21, 2019, 01:20 PM
1<?php
2 use PHPMailer\PHPMailer\PHPMailer;
3 use PHPMailer\PHPMailer\Exception;
4 // Import PHPMailer classes into the global namespace
5 // These must be at the top of your script, not inside a function
6
7if( (isset($_POST['data']) && $_POST['type'] == "newOrder") ){
8 newOrder();
9}else if( (isset($_POST['n_ordine']) && $_POST['type'] == "logIn") ){
10 logIn();
11}else if( (isset($_POST['type']) && $_POST['type'] == "askOrder") ){
12 sendMail('@gmail.com', $_POST['cedola'], $_POST['emailText'], $_POST['cedola']);
13}
14
15function newOrder(){
16 include 'connect.php';
17 //registrazione
18 $myJSON = json_decode($_POST['data'], true);
19
20 //anagrafica
21 $nome = $myJSON[0]['n'];
22 $cognome = $myJSON[0]['c'];
23 $email = $myJSON[0]['e'];
24 $tel = $myJSON[0]['t'];
25 $metodoRitiro = $myJSON[0]['m'];
26 $dataRitiro = $myJSON[0]['r'];
27 $note = $myJSON[0]['note'];
28
29 //daOrdinare
30 $daOrdinare = $myJSON[0]['p'];
31
32
33 //crea anagrafica
34 $sql = "INSERT INTO anagrafiche (nome, cognome, email, tel, met_ritiro, data_ritiro, note)
35 VALUES ('$nome', '$cognome', '$email', '$tel', '$metodoRitiro', '$dataRitiro', '$note' )";
36
37 if ($link->query($sql) === TRUE) {
38 //
39 $last_id = $link->insert_id;
40
41 $lastID = $last_id;
42 $ordine = 'PPL'.$lastID;
43
44 //crea ordine
45 $sql = "INSERT INTO ordini (id_utente, n_ordine, status)
46 VALUES ('$lastID', '$ordine', 1 )";
47 $link->query($sql);
48
49
50 //crea dettaglio Ordine
51 $sql = "INSERT INTO dettaglio_ordini (id_ordine, a, t, q, imm)
52 VALUES ";
53
54 $strDettaglioOrdine = "";
55
56
57 foreach ($daOrdinare as $key => $object) {
58 $strDettaglioOrdine = $strDettaglioOrdine . "('" . $link->insert_id ."','" .$object['a']."','".
59 $object['t']."','".$object['q']."','".$object['imm']."'),";
60 }
61
62
63 $sql = $sql.$strDettaglioOrdine;
64 $sql = substr($sql, 0, -1);
65 $link->query($sql);
66
67
68
69
70 //leggi dal db l'elenco inserito
71 $sql = "SELECT * FROM anagrafiche
72 LEFT JOIN ordini on anagrafiche.id = ordini.id
73 LEFT JOIN dettaglio_ordini on ordini.id = dettaglio_ordini.id_ordine
74 WHERE anagrafiche.id = $lastID";
75
76
77 if ($result=mysqli_query($link,$sql)){
78
79 $elLibri ='';
80 $bodyMailHeader = '
81 <!DOCTYPE html>
82 <html lang="it">
83 <head>
84 <meta charset="UTF-8">
85 <meta name="viewport" content="width=device-width, initial-scale=1.0">
86 <meta http-equiv="X-UA-Compatible" content="ie=edge">
87 <title>Email PPL</title>
88 </head>
89 <body style=" background-color: #301500;
90 color: white;
91 text-decoration: none;">
92
93 <div style="text-align: center;padding: 10 0 50 0;">
94 <img src="https://www.8.it/assets/email/logo.png" alt="" srcset="">
95 </div>
96 <div style="text-align: left;">
97 ';
98
99 // Fetch one and one row
100 while ($row=mysqli_fetch_row($result)){
101
102 $bodyMail = 'Gentile <b>'. $row[2] .' ' . $row[1].'</b> (recapito telefonico ' . $row[4]. ',<br>
103 città: '. $row[5] .'
104 il tuo numero di prenotazione è <span style="color: darkorange;">'. $row[10]. '</span>,
105 con il ritiro il giorno '. $row[6] . '
106 </div>
107 <hr>
108 <p>Elenco libri in prenotazione:</p>
109 <table >';
110
111 $elLibri .= '<tr>
112 <td scope="row" rowspan="2"><img src="' . $row[17] . '" ></td>
113 <td><b>isbn:</b>'. $row[14] . '<b> qtà:</b>'. $row[16] . '
114 </td>
115 </tr>
116 <tr>
117 <td>'. $row[15] . ' </td></tr>';
118
119 $mail = $row[3];
120 $nome = $row[2];
121 $ordine = $row[10];
122 $note = $row[7];
123 }
124
125 $bodyMailFooter = ' </table>
126 <p> <b>Note:<b> '. $note . '</p>
127 <hr>
128 <p>Potete contattarci in qualunque momento tramite mail <a style="color: chocolate;" href="mailto: passaparolalibri@gmail.com">@gmail.com</a>, tramite telefono o whatsapp: </p>
129
130 <table>
131 <tr>
132 <td>
133 magazzino
134 </td>
135 <td>
136 <a style="color: chocolate;" href="tel:33333333333">33433333333</a>
137
138 </td>
139 <td rowspan="2">
140 <a style="color: chocolate;" href="https://wa.me/+33333333333"><img alt="WhatsApp" title="WhatsApp" src="https://www.8.it/assets/email/whatsapp.png" style="width:32px;padding: 0 0 0 20;"></a>
141 </td>
142 </tr>
143 <tr>
144 <td>
145 piazza
146 </td>
147 <td>
148 <a style="color: chocolate;" href="tel:33333333333">33333333333</a>
149 </td>
150 </tr>
151 </table>
152 </body>
153 </html>';
154
155
156 $bodyMailComplete = $bodyMailHeader . $bodyMail . $elLibri . $bodyMailFooter;
157 echo $bodyMailComplete;
158 sendMail($mail, $nome, $bodyMailComplete, $ordine);
159 }
160
161
162
163 } else {
164 return "Error: " . $sql . "<br>" . $link->error;
165 }
166
167
168 //$export = array($mailHTML, $lastID);
169
170 //echo json_encode($export);
171
172
173 $link->close();
174};
175
176
177
178function sendMail($mailTo, $nome, $bodyMail, $ordine){
179
180
181 require "PHPMailer/PHPMailer.php";
182 require "PHPMailer/Exception.php";
183 require 'PHPMailer/SMTP.php';
184
185
186 $mail = new PHPMailer(true);
187
188 try {
189
190
191 //Server settings
192 //$mail->SMTPDebug = 2; // Enable verbose debug output
193 $mail->isSMTP(); // Set mailer to use SMTP
194 $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
195 $mail->SMTPAuth = true; // Enable SMTP authentication
196 $mail->Username = '@gmail.com'; // SMTP username
197 $mail->Password = 'pass'; // SMTP password
198 $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
199 $mail->Port = 587; // TCP port to connect to
200
201 //Recipients
202 $mail->setFrom('@gmail.com', 'PPL');
203
204 $mail->addAddress('@gmail.com', 'PPL');
205 $mail->addReplyTo($mailTo, $nome);
206 //$mail->addAddress($mailTo, $nome);
207
208
209 $mail->isHTML(true);
210
211 $mail->Subject = "Conferma prenotazione $ordine";
212
213 $mail->Body = $bodyMail;
214
215 //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
216
217 $mail->send();
218 echo 'Message has been sent';
219 } catch (Exception $e) {
220 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
221 }
222
223};