Sunday, May 18, 2008

Automatic Email Reporting with Attachment

Ada user yg butuh sistem untuk pengiriman email otomatis
dengan mengandalkan mr Google, dapet deh berbagai referensi untuk bikin email scheduler berikut:

ni untuk email.bat nya: tinggal ditulis di notepad, trs di save as *.bat
cd D:\php\ > folder dimana php berada
copy E:\ > file yg mo di attach
cd D:\php > lokasi php.exe
php D:\emailQC\mail.php > script buat ngirim

na ini isi file mail.php:

$to = "XXX";
$from = "XXX";
$cc = "XXX";
$bcc = "XXX";
$subject = "Load Assay Data";
$message = "\rDear all, \r\r Cuman ngetes pengiriman email dengan scheduler ni.... \r\rRegards,"; //

$fileatt="YYY";
$fileatt_type = $_FILES["YYY"]['type'];
$fileatt_name = "YYY";
$headers = "From: ".$from."\r\n";
$headers .= "Cc: ".$cc."\r\n";
$headers .= "Bcc: ".$bcc."\r\n";

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$message = "Email dengan attachment dan MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .$message . "\n\n";
$data = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" .
"Content-Type: application/vnd.ms-excel\n" .
"Content-Disposition: attachment; filename=$fileatt_name\n".
"Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" ."--{$mime_boundary}--\n";

$ok = @mail($to, $subject, $message, $headers);
?>

2 comments:

Arie Aj said...

$fileatt="YYY";
$fileatt_type = $_FILES["YYY"]['type'];

ini file kemana ya maksudnya???

trs file *.batnya buat apanya ya??

duweek said...

YYY itu variabel nama file yang di attach...
klo bat, itu file yg dipanggil scheduler buat ngerunning skrip nya...