Continuo a postare e mie funzioni php, casomai servissero a qualcuno, ma soprattutto per spammare il planet SpriTe e php-izzare maggiormante demo
Con questo metodo si può creare un formToMail scrivendo semplicemente il form html con names e values.
chiamando la funzione, al posting viene inviato il messaggio chiamando una sola funzione, funzione che compone il body del messaggio semplicemente unendo names e values esistenti. Se il campo contiene "(r)", viene automaticamente mostrato un messaggio di errore "campo X non riempito" con tasto "indietro".
esempio.php
<?
if ($_POST)
if (formMail($_POST,
array( "from"=> $_POST['email'] ,
//mittente: quello inserito
"to" => "destinaz@email.com",
"oggetto" => "form to mail"
)
))
exit();
?>
<form action=stesspagina.php method=post>
nome *<input value="" name="nome(r)" />
cognome <input value="" name="cognome" />
email *<input value="" name="email(r)" />
<input type="submit">
<!– fine esempio.php –>
Dopo il submit, se non ci sono errori, viene composta inviata una email html con body:
Nome = luigi
Cognome = rossi
Email: luigirossi@email.com
la funzione da includere:
function formMail($POST, $PARAMS = array())
{
/*array( "from"=> "preventivi@pulirapida.it",
"to" => "santi@pulirapida.it",
"oggetto" => "Richiesta preventivo da pulirapida.it" */
//metti (r) nel nome del campo per renderlo obbligatorio
if (!$POST)
return true;
$errori=array();
foreach($POST as $k => $v) //se contiene (r) ma non un valore…
{
$v = (get_magic_quotes_gpc()) ? stripslashes( $v) : $v;
if (strstr($k, "(r)") && !$v)
$errori[] = sprintf ("<b>%s</b> non %s", formatcampo($k), strncmp($k, "appr", 4)==0 ? "approvato/a" : "inserito/a" );
}
//stampo eventuali errori
if (count($errori))
{
?><h4>MODULO NON INVIATO !</h4>
<p>Si sono verificati i seguenti errori:</p>
<ul><li><? print implode("</li><li>", $errori); ?></li></ul>
<p><a href="javascript:history.back()">Torna indietro</a> e correggi gli errori</p><?
}
else
{
$body = "";
foreach($POST as $k => $v) //se contiene (r) ma non un valore…
{
$v = (get_magic_quotes_gpc()) ? stripslashes( $v) : $v;
if (!strstr($k, "(nomail)") && $v)
$body .= sprintf("<b>%s</b>: %s<br />", formatcampo($k), nl2br(htmlentities($v)) );
}
$headers = "From: ".$PARAMS['from']."\nReply-to: ".$PARAMS['from']."\nContent-Type: text/html";
$merrori = 0;
if (!is_array($PARAMS['to']))
$PARAMS['to'] = array_pop($PARAMS['to']);
foreach($PARAMS['to'] as $to)
{
if (!mail($to, $PARAMS['oggetto'], $body,$headers))
$merrori++;
}
if ($merrori==0)
{
?><h4>MODULO INVIATO !</h4>
<hr /><?
print $PARAMS['oggetto']."<hr>".$body;
return true;
}
else
{
?><h4>Impossibile inviare il modulo</h4>
copiare il testo seguente e inviarlo per mail a [...]
<hr /> <?
print $PARAMS['oggetto']."<hr>".$body;
}
}
return false;
}
function formatcampo($s)
{
$s = str_replace("(r)","",$s);
$s = str_replace("(nomail)","",$s);
$s = str_replace("_"," ",$s);
//non serve htmldecode, so che le k sono camp scritti da me
return ucfirst($s);
}



Leave a comment