Mail in html con allegato non coesistono?

Marco Zabeo

Nuovo Utente
26 Nov 2014
2
0
0
<html>
<head>
<title>Modulo contatti</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body topmargin="50" margineight="50">
<table width="400" align="center">
<tr>
<td>
<?PHP
// IDENTIFICA UN POTENZIALE ERRORE
$errore = 0;


// Manda mail a
$to = $_POST['to'];

// Mittente

$from = $_POST['from'];

// Controlla ESPRESSIONE REGOLARE DELL'EMAIL
$re_1 = ereg("^[^@ ]+@[^@ ]+.+[^@ ]+$", $from);
$re_2 = ereg("^[^@ ]+@[^@ ]+.+[^@ ]+$", $to);



// RECUPERA I DATI DAL FORM

$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$indirizzo = $_POST['indirizzo'];
$citta = $_POST['citta'];
$azienda = $_POST['azienda'];
$tel = $_POST['tel'];
$fax = $_POST['fax'];






// Valorizzo le variabili relative all'allegato
$allegato = $_FILES['allegato']['tmp_name'];
$allegato_type = $_FILES['allegato']['type'];
$allegato_name = $_FILES['allegato']['name'];

// Creo 2 variabili che riempirò più avanti...
$headers = "From: " . $from;
$msg = "";

// Verifico se il file è stato caricato correttamente via HTTP
// In caso affermativo proseguo nel lavoro...
if (is_uploaded_file($allegato))
{
// Apro e leggo il file allegato
$file = fopen($allegato,'rb');
$data = fread($file, filesize($allegato));
fclose($file);

// Adatto il file al formato MIME base64 usando base64_encode
$data = chunk_split(base64_encode($data));

// Genero il "separatore"
// Serve per dividere, appunto, le varie parti del messaggio.
// Nel nostro caso separerà la parte testuale dall'allegato
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Aggiungo le intestazioni necessarie per l'allegato
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";

// Definisco il tipo di messaggio (MIME/multi-part)
$msg .= "This is a multi-part message in MIME format.\n\n";

// Metto il separatore
$msg .= "--{$mime_boundary}\n";

// Questa è la parte "testuale" del messaggio
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= $corpo . "\n\n";

// Metto il separatore
$msg .= "--{$mime_boundary}\n";

// Aggiungo l'allegato al messaggio
$msg .= "Content-Disposition: attachment;\n";
$msg .= " filename=\"{$allegato_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n\n";
$msg .= $data . "\n\n";

// chiudo con il separatore
$msg .= "--{$mime_boundary}--\n";
}
else
{
$msg = $corpo;
}


// CONTROLLA I CAMPI
if ($nome == "") { $errore = 1; }
elseif ($re_1 == FALSE) { $errore = 2; }
elseif ($cognome == "") { $errore = 3; }
elseif ($re_2 == FALSE) { $errore = 4; }

// SE I CAMPI SONO STATI COMPILATI CORRETTAMENTE...
else
{
// FORMATTA LA MAIL





$corpo = '
<html>
<head>
</head>

<body>
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td colspan="2">';
$corpo .= '<br><br>';


$corpo .= '</td></tr>';
$corpo .= '<tr><td width="50%" valign="top">';
$corpo .= 'Colazione da tiffany';



$corpo .= '<br><br>';
$corpo .= '</td></tr>';
$corpo .= '</table>
</body>
</html>';


$mail_in_html .= "MIME-Version: 1.0\r\n";
$mail_in_html .= "Content-type: text/html; charset=UTF-8\r\n";
$mail_in_html .= "From: $from\r\n";
$mail_in_html .= "BCC:$to\r\n";

if (mail($to,"Imbarcazione $cantiere",$msg,$mail_in_html))

// INVIA LA MAIL CONTROLLANDO CHE L'INVIO VADA A BUON FINE
{
$errore = 0;
}
else
{
$errore = 5;
}
}


?>

<html>
<head>
<title>Invia ad un amico</title>
</head>
<body>

<p align="center">
<?PHP
switch ($errore)
{
case 0;
print "Segnalazione inviata con successo";
break;

case 1;
print "Inserisci il tuo nome";
break;

case 2;
print "Errore mail destinatario";
break;

case 3;
print "Inserisci il cognome";
break;

case 4;
print "Inserisci la tua mail";
break;

case 5;
print "Il server non è riuscito ad inviare la mail";
break;
}
?>
<br><br>
<a href="javascript:window.history.back();">Indietro</a>
</p>

</td>
</tr>
</table>

</body>
</html>
 

cory

Utente Attivo
31 Gen 2014
51
0
6
cory.altervista.org
io uso questo e funziona

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>E-mail con tanti allegati</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){
   $to="[email protected]";
   $subject="E-mail con tanti allegati";
   $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";
	 $mex = stripslashes($_POST['messaggio']);
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
   $headers = "From: $from\r\n" .
   "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";
   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "<h1>{$mex}</h1>\n\n";
   foreach($_FILES as $userfile){
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];
      if (file_exists($tmp_name)){
         if(is_uploaded_file($tmp_name)){
            $file = fopen($tmp_name,'rb');
            $data = fread($file,filesize($tmp_name));
            fclose($file);
            $data = chunk_split(base64_encode($data));
         }
         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
      }
   }
   $message.="--{$mime_boundary}--\n";
   if (@mail($to, $subject, $message, $headers))
      echo "Messaggio Inviato";
   else
      echo "Invio Fallito";
} else {
?>
<p>Invio di email con allegati</p>
<p>e mi arriva buona !!</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" id="form1">
   <p>Mittente: <input type="text" name="fromname" /></p>
   <p>Indirizzo e-mail: <input type="text" name="fromemail" /></p>
   <p>Messaggio: <textarea name="messaggio"></textarea></p>
   <p>File: <input type="file" name="file1" /></p>
   <p>File: <input type="file" name="file2" /></p>
   <p>File: <input type="file" name="file3" /></p>
   <p>File: <input type="file" name="file4" /></p>
   <p><input type="submit" name="Submit" value="Submit" /></p>
</form>
<?php } ?>
</body>
</html>
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.046
150
63
PR
www.borgo-italia.it
ciao
un paio di cose
1) la funzione ereg è deprecata, per verificare un indirizzo email convine usare
PHP:
$err=filter_var($indirizzo_email, FILTER_VALIDATE_EMAIL);//true o false
2) quando ci si fa inviare dei file e bene per sicurezza verificarne il tipo
3) usa phpmailler o simili sono più performanti
 
Discussioni simili
Autore Titolo Forum Risposte Data
filomeni Problema con mail() in html PHP 4
S Form invio mail html, con invio tramite file denominato invio.php PHP 8
M Adattare script di "form mail con upload file" al mio form mail HTML! PHP 7
L Html mail con piu' mails HTML e CSS 9
D Mail in html con una form all'interno? HTML e CSS 2
SolidSnake4 invio mail html con php non va PHP 6
H ASPMail - Invio di mail html con variabili Classic ASP 5
A [HTML] Testo mail non visualizzabile su IOS HTML e CSS 0
A [PHP] Formattazione Mail HTML HTML e CSS 5
Tony Recchia [PHP] Modulo mail HTML PHP 4
Samuele Ronzani Form mail HTML HTML e CSS 7
gandalf1959 Mail in formato HTML PHP 5
S inviare the_title() in una mail tramite form html/php PHP 0
S scrivere e-mail in html Posta Elettronica 6
M Perchè non invia la mail in formato html? PHP 5
A form in php o html per raccolta mail PHP 4
IImanuII Mail in php da testo a html PHP 40
IImanuII Form mail scelta se inviare il testo in html PHP 5
D Problema invio mail [GIA POSTATO ERRONEAMENTE IN HTML] PHP 0
I mail in html PHP 7
T Mail: non funzionano i codici html! PHP 6
B e mail da form HTML HTML e CSS 51
M mail in html PHP 2
M formato mail html Classic ASP 0
felino Mac OS e Client Mail: Stato non in linea Mac e Software 2
I recupera password via mail salva pasword password_hash PHP 2
I resetta password via mail PHP 2
M telecamera Foscam - mancato invio mail ad account gmail IP Cam e Videosorveglianza 0
K Importazione e-mail da account gmail ad un altro account gmail Posta Elettronica 0
R Invio mail con allegati multipli PHP 0
A Mail con funzione mail() riconosciute come spam PHP 9
R Invio mail con allegati da directory PHP 1
R Confrontare il dominio di una mail con una stringa contenente un dominio personalizzato, tutto ciò in php o javascript PHP 0
Daniele_Carrara Problema timeout - invio mail PHP 7
G Con Microsoft Outlook 2016 come si possono cancellare le mail doppie? Posta Elettronica 0
G Evitare che mi continui ad arrivare in alice mail spam Posta Elettronica 2
M configurare mail register.it (con 1000 invii/die) su Outlook Posta Elettronica 0
Marti1! Problema con casella mail cancellata Posta Elettronica 3
U PHP bottone per invio mail o ritorno al form PHP 15
G Invio mail con php da dati prelevati da un database PHP 9
AC1 [PHP] Invio Mail PHP 18
L Problema funzione mail() PHP PHP 3
I [PHP] inviare form con allegato tramite una mail PHP 1
A Libero mail non funziona Posta Elettronica 3
B [PHP] Invio mail automatico dopo compilazione form - db PHP 25
L PopUp tramite PHP se la mail inserita è già registrata nel database PHP 3
G [PHP] Creare mail con allegato nascosto e bottone di richiamo PHP 0
P [PHP] PEAR Mail e Query PHP 3
Cosina Andare a capo nel testo della mail ricevuta dal form php PHP 1
D [PHP] script che invii una mail automatica dopo risposta ad una discussione di un forum PHP 0

Discussioni simili