Salve a tutti, ho scritto questo codice seguendo varie guide che permette di completare un form premere invio ed inviare la mail aggiungendo ad essa un allegato pdf da me indicato. Il file di partenza per l'inserimento dell'allegato l'ho testato e funziona bene, quando però ho accorpato la mia gestione all'esempio qualcosa non è andata a buon fine infatti pare non funzionare. Vi predo di darmi un aiuto, ecco il codice:
Codice PHP:<?php
// Il file si chiama form.php
if (!isset($_GET['op'])){
$op = 'mostra';
}else{
$op =$_GET['op'];
}
switch ($op){
case 'invia':
// Cambia con la tua mail
$email_to = $_POST['Field2'];
// Ottiene i campi
$email = $_POST['Field21'];
if(!isset($_POST['Field9'])){
$oggetto = "Nessun oggetto";
}else{
$oggetto = $_POST['Field9'] . " Le ha inviato questo messaggio dal sito [url]MIO SITO[/url]";
}
if(!isset($_POST['Field1'])){
$testo = "Nessun testo";
}else{
$testo = $_POST['Field1'];
}
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From:" . $email . "\r\nReply-To:" . $email;
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('download/file.pdf')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $testo; ?>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/pdf; name="file.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $testo; ?>
<?php
//copy current buffer contents into $message variable and delete current output buffer
$testo = ob_get_clean();
if (mail($email_to, $oggetto, $testo, $headers)){
// Modifica qui il messaggio di ringraziamento
echo '<p style="text-align:center;">Grazie... Se i dati inseriti sono corretti riceverai presto una risposta.</p>';
}else{
echo '<p style="text-align:center;">Impossibile inviare la mail, riprovare</p>';
}
}
?>
Grazie 1000 a tutti.



Rispondi quotando