Enviar notes als alumnes, fitxer CSV
De Wikijoan
Per parsejar des de PHP un fitxer CSV:
Descarrego: parsecsv-0.3.2.zip
D'altra banda, necessito PHPMailer i configurar-lo per enviar mails a través d'un compte de gmail.
El projecte està a /var/www/parsecsv-0.3.2/examples, i aquí és on localitzo els fitxers.
El fitxer CSV que vull enviar: notes_alumnes.csv:
nom;mail;ex1;final alu 1;joanqc@gmail.com;8.3;4 alu 2;joanqc@gmail.com;6.0;7 alu 3;jquinta2@xtec.cat;7.2;4.3
script enviar_notes_mail.php:
<?php //barrejo el codi amb test_smtp_basic.php //error_reporting(E_ALL); error_reporting(E_STRICT); require_once('../../PHPMailer_5.2.4/class.phpmailer.php'); require_once('../parsecsv.lib.php'); $csv = new parseCSV(); $csv->auto('notes_alumnes.csv'); $csv->delimiter = ";"; ?> <?php foreach ($csv->data as $key => $row): $mail = new PHPMailer(); $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "tls"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets the SMTP server $mail->Port = 587; // set the SMTP port for the GMAIL server $mail->Username = "joanqc@gmail.com"; // SMTP account username $mail->Password = "********"; // SMTP account password $mail->SetFrom('joanqc@gmail.com', 'Joan Quintana'); $mail->Subject = "Notes de la UF1"; $i=0; foreach ($row as $value): //echo $value; $i++; if ($i==1) { $nom = $value; } else if ($i==2) { $address = $value; } else if ($i==3) { $nota1 = $value; } else if ($i==4) { $nota2 = $value; } endforeach; $body = "Hola "; $body .= $nom."<br /><br />"; $body .= "T'envio les notes de la UF1.<br />"; $body .= "La nota que vas treure a l'examen: "; $body .= $nota1."<br />"; $body .= "La nota final de la UF: "; $body .= $nota2."<br /><br />"; $body .= "Atentament,<br />"; $body .= "Joan Quintana<br />"; $body .= "tutor 2n DAW<br />"; echo $body; //echo $nom; //echo $address; //echo $nota1; //echo $nota2; $mail->MsgHTML($body); $mail->AddAddress($address, $nom); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } endforeach; ?>
creat per Joan Quintana Compte, desembre 2013