Buongiorno, avrei gentilmente bisogno di inviare tramite email il contenuto di un carrello della spesa, spero che gentilmente qualcuno mi aiuti.
funzioni.php
codice:
<?php
function usaCarrello()
{
$carrello = $_SESSION['carrello'];
if (!$carrello)
{
return 'Il carrello è vuoto.
';
}else{
$prodotti = @explode(',',$carrello);
return 'Ci sono <a href="carrello.php">'.
@count($prodotti). ' prodotti nel carrello.</a>
';
}
}
function mostraCarrello()
{
global $db;
$carrello = $_SESSION['carrello'];
$somma = 0;
if ($carrello)
{
$prodotti = @explode(',',$carrello);
$acquisti = array();
foreach ($prodotti as $prodotto)
{
$acquisti[$prodotto] = (@isset($acquisti[$prodotto])) ? $acquisti[$prodotto] + 1 : 1;
}
$result[] = '<form action="carrello.php?action=aggiorna" method="post" id="cart">';
$result[] = '<table>';
foreach ($acquisti as $id=>$quantita)
{
$sql = 'SELECT * FROM prodotti WHERE id = '.$id;
$res = $db->query($sql);
$f = $res->fetch();
@extract($f);
$result[] = '<tr>';
$result[] = '<td>Cancella</td>';
$result[] = '<td>'.$nome.' by '.$marca.'</td>';
$result[] = '<td>€'.$prezzo.'</td>';
$result[] = '<td><input type="text" name="quantita'.$id.'" value="'.$quantita.'" size="3"></td>';
$result[] = '<td>€'.($prezzo * $quantita).'</td>';
$somma += $prezzo * $quantita;
$result[] = '</tr>';
}
$result[] = '</table>';
$result[] = 'Totale: €'.$somma.'</br>';
$result[] = '<button type="submit">Aggiorna il carrello</button>';
$result[] = '</form>';
}else{
$result[] = 'Il carrello è vuoto.
';
}
return @join('',$result);
}
?>
carrello.php
codice:
<?php
@session_start();
@require('mysql.php');
@require('config.php');
@require('funzioni.php');
$carrello = $_SESSION['carrello'];
if(@isset($_GET['action']))
{
$action = $_GET['action'];
switch ($action)
{
case 'aggiungi':
if ($carrello)
{
$carrello .= ','.$_GET['id'];
}else{
$carrello = $_GET['id'];
}
break;
case 'cancella':
if ($carrello)
{
$prodotti = @explode(',',$carrello);
$acquisto = '';
foreach ($prodotti as $prodotto)
{
if ($_GET['id'] != $prodotto)
{
if ($acquisto != '')
{
$acquisto .= ','.$prodotto;
}else{
$acquisto = $prodotto;
}
}
}
$carrello = $acquisto;
}
break;
case 'aggiorna':
if ($carrello)
{
$acquisto = '';
foreach ($_POST as $key=>$value)
{
if (@stristr($key,'quantita'))
{
$id = @str_replace('quantita','',$key);
$prodotti = ($acquisto != '') ?
@explode(',',$acquisto) : @explode(',',$carrello);
$acquisto = '';
foreach ($prodotti as $prodotto)
{
if ($id != $prodotto)
{
if ($acquisto != '')
{
$acquisto .= ','.$prodotto;
}else{
$acquisto = $prodotto;
}
}
}
for ($i=1;$i<=$value;$i++)
{
if ($acquisto != '')
{
$acquisto .= ','.$id;
}else{
$acquisto = $id;
}
}
}
}
}
$carrello = $acquisto;
break;
}
}
$_SESSION['carrello'] = $carrello;
?>
<html>
<head>
<title>Un carrello della spesa con PHP</title>
</head>
<body>
<h1>Carrello in PHP</h1>
<?php
echo usaCarrello();
?>
<h1>Controlla il numero dei prodotti</h1>
<?php
echo mostraCarrello();
?>
invioemail.php
codice:
<?php
@session_start();
@require('mysql.php');
@require('config.php');
@require('funzioni.php');
$carrello = $_SESSION['carrello'];
// L'INDIRIZZO DEL DESTINATARIO DELLA MAIL
$to = "info@sienaforum.com";
// IL SOGGETTO DELLA MAIL
$subject = "Modulo proveniente dal sito www.sito.it";
// COSTRUZIONE DEL CORPO DEL MESSAGGIO
$body = "Contenuto del modulo:\n\n";
$body = carrello
$body .= "Dati personali ;
nome: " . trim(stripslashes($_POST["nome"])) . "\n";
$body .= "cognome: " . trim(stripslashes($_POST["cognome"])) . "\n";
$body .= "Città: " . trim(stripslashes($_POST["citta"])) . "\n";
$body .= "Oggetto: " . trim(stripslashes($_POST["oggetto"])) . "\n";
$body .= "testo: " . trim(stripslashes($_POST["testo"])) . "\n";
$body .= "mail: " . trim(stripslashes($_POST["mail"])) . "\n";
// INTESTAZIONI SUPPLEMENTARI
$headers = "From: Modulo utenti<INDIRIZZO-COME-SOPRA>";
// INVIO DELLA MAIL
if(@mail($to, $subject, $body, $headers)) {
// SE L'INOLTRO E' ANDATO A BUON FINE...
echo "La mail è stata inoltrata con successo."; }
else {// ALTRIMENTI...
echo "Si sono verificati dei problemi nell'invio della mail."; }
?>
Come faccio ad inviare per email il contenuto del carrello nella sessione?
Grazie mille!