Ciao a tutti!
mi sono imbattutto su una necessità di inviare un mail con allegato ad una lista di persone che avevo su un foglio excel così ho fatto uno script che potrebbe esservi utile.
Per farlo funzionare avete bisogno delle seguenti librerie:
- phpexcelreader
- phpmailer
Ecco il codice per il file sendmail.php
<?php
require_once 'Excel/reader.php';
require_once 'class.phpmailer.php';
// ExcelFile($filename, $encoding);
$data = new Spreadsheet_Excel_Reader();
$mail = new PHPMailer();
// Set output Encoding.
$data->setOutputEncoding('CP1251');
$data->read('test.xls');
error_reporting(E_ALL ^ E_NOTICE);
// RENDIAMO LO SCRIPT COMPATIBILE CON LE VERSIONI DI PHP < 4.1.0
if(!isset($_POST)) $_POST = $HTTP_POST_VARS;
if(!isset($_FILES)) $_FILES = $HTTP_POST_FILES;
// RIPULIAMO I VARI CAMPI DEL MODULO
$nomefrom = trim($_POST["nomefrom"]);
$from = trim($_POST["from"]);
$oggetto = trim($_POST["oggetto"]);
$Contenuto = trim($_POST["Contenuto"]);
$attach = $_FILES["allegato"]["tmp_name"];
$file_name = $_FILES["allegato"]["name"];
$file_type = $_FILES["allegato"]["type"];
$file_size = $_FILES["allegato"]["size"];
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$nome= "".$data->sheets[0]['cells'][$i][1]."";
$email= "".$data->sheets[0]['cells'][$i][2]."";
if ($email<> ""){
// prepari il messaggio che vuoi inviare
$mess = "<html>";
$mess .= "<head>";
// puoi allegare anche un style css, basta che passi come url quello assoluto
$mess .= "</head>";
$mess .= "<body>";
$mess .= "<div class=\"unaclassedelfogliodistile\">";
$mess .= "Spett.le $nome
" ;
$mess .= $Contenuto;
$mess .= "</div>";
$mess .= "</body>";
$mess .= "</html>";
$mail->From = $from; // indirizzo da cui proviene la mail
$mail->FromName = $nomefrom;
$mail->AddAddress($email, $nome);
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Timeout=60;
$mail->Subject = $oggetto;
$mail->Body = $mess; // $mess conterrà il messaggio html
$mail->AltBody = "Non puoi vedere il contenuto della mail in quanto il tuo client non lo consente.";
$mail->AddAttachment($attach, $file_name); // optional name
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
else
{
echo " Invio corretto $email
";
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
}
?>
e il codice per il form potrebbe essere:
<form action="sendmail.php" enctype="multipart/form-data" method="POST">
<table cellpadding="0" cellspacing="0">
<tr>
<td>Nome di chi Spedisce </td>
<td><input name="nomefrom" type="text" id="nomefrom" size="40"></td>
</tr>
<tr>
<td>Email di chi Spedisce : </td>
<td>
<input name="from" type="text" id="from" size="40">
</p>
</td>
</tr>
<tr>
<td>Oggetto:</td>
<td><input name="oggetto" type="text" id="oggetto" size="40"></td>
</tr>
<tr>
<td>Allegato:</td><td><input type="file" name="allegato" size="40"></td>
</tr>
<tr>
<td valign="top">Contenuto:</td>
<td>
Spett.le Nome Azienda (è già inserito in automatico) </p>
<textarea name="Contenuto" rows="15" cols="50"></textarea>
</p></td>
</tr>
<tr>
<td height="30" valign="bottom" colspan="2" align="center"><input type="submit" value="Invia la mail">
</tr>
</table>
</form>
ultima cosa il file excel dovrebbe essere strutturato così:
Azienda | Mail |
nome1 | m@m.it |
nome2 | n@n.it |
Spero vi possa servire...
Grazie
VKR