- Home
- Categorie
- Coding e Sistemistica
- PHP
- Form contatti formattato alla ricezione
-
Se usi PHP per l'invio della email ti consiglio PHPMailer, per i campi ti basta un semplice form php con i campi che desideri. Con l'azione post del pulsante troverai nelle variabili $_POST i dati che puoi riutilizzare come meglio credi.
-
Ok grazie. Sto seguendo il tuo consiglio ed ho scaricato PHPMailer Version 2.0 dal sito sourceforge.net/projects/phpmailer/files/phpmailer for php4/v2.0.0/.
Successivamente ho trovato una guida su come utilizzare PHPMailer: mrwebmaster.it/php/articoli/inviare-email-classe-phpmailer_631.html
Alla Pagina 3 della guida,con il titolo "Inviare email in formato HTML con PHPMailer", suggerisce questo codice:
<?require "phpmailer/class.phpmailer.php";$messaggio = new PHPmailer();$messaggio->IsSMTP();//settiamo su true il metodo che indica alla classe //il formato HTML$messaggio->IsHTML(true);$messaggio->Host='Host SMTP';//intestazioni e corpo dell'email$messaggio->From='[email protected]';$messaggio->AddAddress('[email protected]');$messaggio->AddReplyTo('[email protected]'); $messaggio->Subject='Prova formato HTML';//inseriamo i tag HTML e i CSS per formattare il messaggio$messaggio->Body = '<html>
<body>
<head>
<style>';$messaggio->Body .= '.up{background-color:#FF0000;color:#000000;font-size:12px}';$messaggio->Body .= '.down{color:#FF0000;text-align:left;font-size:15px}';$messaggio->Body .= '</style></head>';$messaggio->Body .= '<center><table><tr><td class="up">Ciao!!</td></tr>';$messaggio->Body .= '<tr><td class="down">ciao!!!</td></tr></table></center>';$messaggio->Body .= '</body></html>';//parte relativa all'invioif(!$messaggio->Send()){ echo $messaggio->ErrorInfo; }else{ echo 'Email inviata correttamente!';}$messaggio->SmtpClose();unset($messaggio);?>Adesso,quest'ultimo, deve essere scritto nella mia pagina index.html, dove metterò i text box opportuni per l'utilizzo del form contatti, oppure deve essere inserito in un file di configurazione di PHPMailer?
-
Intanto il file deve avere estensione .php altrimenti non eseguirà il codice server.
Ecco un esempio velocePagina index.html
[HTML]
<form method="POST" action="mailManager.php" enctype="multipart/form-data">
<table>
<tr>
<td>Nome</td>
<td><input class="text" type="text" name="nome" size="20"></td>
</tr>
<tr>
<td>Cognome</td>
<td><input class="text" type="text" name="cognome" size="20"></td>
</tr>
<tr>
<td>Email</td>
<td><input class="text" type="text" name="email" size="20"></td>
</tr>
<tr>
<td>Messaggio</td>
<td><textarea rows="2" name="messaggio" cols="20"></textarea></td>
</tr>
<tr>
<td>File</td>
<td><input type="file" name="allegato"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Invia" name="sub"><input type="reset" value="Annulla" name="res"></td>
</tr>
</table>
</form>
[/HTML]pagina **mailManager.php
**[PHP]
<?php
require_once('class.phpmailer.php');
//upload dell'allegato
if (!move_uploaded_file($_FILES['allegato']['tmp_name'], $_FILES['allegato']['name']))
{
echo "Errore nel caricamento dell'immagine";
}
else{
//invio mail
$mail = new PHPMailer();
$mail->SetFrom($_POST['email'],$_POST['nome']."".$_POST['cognome']); //mittente
$mail->AddAddress("[email protected]", "Nome destinatario"); //destinatario
$mail->Subject = "Invio mail da form"; //oggetto
$mail->Body = $_POST['messaggio']; //corpo del messaggio
$mail->AddAttachment($_FILES['allegato']['name']); //allegato appena caricato sul serverif(!$mail->Send()) { echo "Messaggio non inviato! <p>"; echo "Errore: " . $mail->ErrorInfo; exit; } echo "Message inviato con successo"; }
?>
[/PHP]
****
-
Ho copiato il codice che mi hai postato e questi sono i file che ho creato:
**index.html
**
[HTML]<html> <head> <title> Prova php mailer </php></title> </head> <body> <form method="POST" action="mailManager.php" enctype="multipart/form-data"> <table> <tr> <td>Nome</td> <td><input class="text" type="text" name="nome" size="20"></td> </tr> <tr> <td>Cognome</td> <td><input class="text" type="text" name="cognome" size="20"></td> </tr> <tr> <td>Email</td> <td><input class="text" type="text" name="email" size="20"></td> </tr> <tr> <td>Messaggio</td> <td><textarea rows="2" name="messaggio" cols="20"></textarea></td> </tr> <tr> <td>File</td> <td><input type="file" name="allegato"></td> </tr> <tr> <td> </td> <td><input type="submit" value="Invia" name="sub"><input type="reset" value="Annulla" name="res"></td> </tr> </table> </form> </body></html>[/HTML]**mailerManager.php
**[PHP]<?php require_once('phpmailer/class.phpmailer.php'); //upload dell'allegato if (!move_uploaded_file($_FILES['allegato']['tmp_name'], $_FILES['allegato']['name'])) { echo "Errore nel caricamento dell'immagine"; } else{ //invio mail $mail = new PHPMailer(); $mail->SetFrom($_POST['email'],$_POST['nome']."".$_POST['cognome']); //mittente $mail->AddAddress("[email protected]", "Marco Tornani"); //destinatario $mail->Subject = "Invio mail da form"; //oggetto $mail->Body = $_POST['messaggio']; //corpo del messaggio $mail->AddAttachment($_FILES['allegato']['name']); //allegato appena caricato sul server
if(!$mail->Send()) { echo "Messaggio non inviato! <p>"; echo "Errore: " . $mail->ErrorInfo; exit; } echo "Message inviato con successo"; }
?>[/PHP]Ho caricato tutto sul mio server. Dopo aver compilato tutti i campi ed aver caricato l'immagine *wedding_head_table_4.jpg, *al momento che premo invio mi compare questo errore:
Warning: move_uploaded_file(wedding_head_table_4.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied inD:\Inetpub\webs\esseaserviceit\mailManager.php on line **4
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\PHP\upload\php2348.tmp' to 'wedding_head_table_4.jpg' in**D:\Inetpub\webs\esseaserviceit\mailManager.php on line **4
Errore nel caricamento dell'immagine**NE SAI QUALCOSA???
**
-
Prima di passare a oltre ti pongo la domanda:
Serve che l'utente carichi un file?
Se si è un problema di permessi che dovresti vedere sulla cartella temporanea C:\PHP\upload\php2348.tmp
Se no ti basta togliere la parte di attach e togliere il componente input "file" su index.html.Ultima domanda intetpub mi sa tanto di IIS, per caso son solo prove locali o stai facendo andare PHP sotto server windows?
-
Si,ho bisogno anche che carichi un immagine. Sto provando il tutto su un server windows... Quindi devo dare i permessi alla cartella upload. Provo un pò e poi ti faccio sapere. **Grazie **
-
Adesso che ho cancellato,sia nel file.html che nel file.php,il codice che riguardava l'inserimento di un file, mi da il seguente errore: **Fatal error: Call to undefined method PHPMailer::SetFrom() in **D:\Inetpub\webs\esseaserviceit\mailManager.php on line **7
******Ti posto anche il codice del file mailManager.php attuale:
[PHP]<?php
require_once('phpmailer/class.phpmailer.php');//invio mail $mail = new PHPMailer(); $mail->SetFrom($_POST['email'],$_POST['nome']."".$_POST['cognome']); //mittente $mail->AddAddress("[email protected]", "Marco Tornani"); //destinatario $mail->Subject = "Invio mail da form"; //oggetto $mail->Body = $_POST['messaggio']; //corpo del messaggio $mail->AddAttachment($_FILES['allegato']['name']); //allegato appena caricato sul server if(!$mail->Send()) { echo "Messaggio non inviato! <p>"; echo "Errore: " . $mail->ErrorInfo; exit; } echo "Message inviato con successo";
?>
[/PHP]
-
L'errore dice che la classe [LEFT]PHPMailer [/LEFT]non contiene il metodo [LEFT]SetFrom()[/LEFT] dovresti verificare se nel file [LEFT]phpmailer/class.phpmailer.php[/LEFT] trovi tale metodo (che è di base di phpmailer, quindi c'è forse qualcos'altro.)
Inoltre nel file php che mi hai incollato c'è ancora il codice riguardante l'allegato. [LEFT][/LEFT]
-
Buongiorno,
Grazie per il consiglio. Il mio file class.phpmailer.php non contiene il metodo SetFrom() ma il metodo *var $From,*dove ho messo il mio indirizzo. E' la solita cosa?
Mi sono accorto anche che la variabile Host,che dovrebbe contenere l'host smtp è scritta così: *var $Host = 'localhost'; *Io non so cosa metterci avendo un account gmail.Questa è la parte di configurazione SMTP nel file class.phpmailer.php:
*
[PHP]///////////////////////////////////////////////// // PROPERTIES FOR SMTP /////////////////////////////////////////////////
/* * Sets the SMTP hosts. All hosts must be separated by a * semicolon. You can also specify a different port * for each host by using this format: [hostname:port] * (e.g. "smtp1.example.com:25;smtp2.example.com"). * Hosts will be tried in order. * @var string / var $Host = 'localhost';
/* * Sets the default SMTP server port. * @var int / var $Port = 25;
/* * Sets the SMTP HELO of the message (Default is $Hostname). * @var string / var $Helo = '';
/* * Sets connection prefix. * Options are "", "ssl" or "tls" * @var string / var $SMTPSecure = "";
/* * Sets SMTP authentication. Utilizes the Username and Password variables. * @var bool / var $SMTPAuth = false;
/* * Sets SMTP username. * @var string / var $Username = '';
/* * Sets SMTP password. * @var string / var $Password = '';
/* * Sets the SMTP server timeout in seconds. This function will not * work with the win32 version. * @var int / var $Timeout = 10;
/* * Sets SMTP class debugging on or off. * @var bool / var $SMTPDebug = false;
/* * Prevents the SMTP connection from being closed after each mail * sending. If this is set to true then to close the connection * requires an explicit call to SmtpClose(). * @var bool / var $SMTPKeepAlive = false;
/* * Provides the ability to have the TO field process individual * emails, instead of sending to entire TO addresses * @var bool */ var $SingleTo = false;[/PHP]
-
Zambros! ho risolto il problema Fatal error: Call to undefined method PHPMailer::SetFrom() inD:\Inetpub\webs\esseaserviceit\mailManager.php on line **7, ****aggiornando la versione di PHPMailer alla 5.1 che ho trovato a questo link sourceforge.net/projects/phpmailer/files/phpmailer for php5_6/PHPMailer v5.1/. Purtroppo non ho risolto del tutto il mio problema perchè adesso mi da questo errore SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host. **Puoi aiutarmi? Io vorrei che mi arrivino le informazioni compilate nel form, alla mia posta su gmail.
-
Ecco
L'errore è semplicemente dato dal login che fai con SMTP dovresti mettere i valori giusti:Esempio GMAIL
$Host= "smtp.gmail.com";
$port = "465";
$SMTPSecure = "ssl";
$SMTPAuth = true;
$Username = "[email protected]";
$Password= "TUAPASSWORD";