endrasgusti

Untitled

Nov 7th, 2017
427
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require '../phpmailer/class.phpmailer.php';
  2.  
  3. // ------------------------------------------------
  4. // setting dulu php mailer nya,
  5. // nilai nya ndak akan berubah sepanjang script.
  6. // jadi ndak perlu ditaruh di dalam perulangan while.
  7. // cukup sekali aja setting nya.
  8. // ------------------------------------------------
  9. $mail = new PHPMailer(true);
  10. $mail->IsSMTP();                      // tell the class to use SMTP
  11. $mail->SMTPAuth = true;               // enable SMTP authentication
  12. $mail->SMTPSecure = 'tls';
  13. $mail->Port = 587;                    // set the SMTP server port
  14. $mail->Host = 'smtp.gmail.com';       // SMTP server
  15. $mail->Username = 'xxxxxx@gmail.com'; // SMTP server username
  16. $mail->Password = 'xxxxxxx';         // SMTP server password
  17. //$mail->IsSendmail();                // tell the class to use Sendmail
  18. $mail->AddReplyTo('xxxxx@gmail.com');
  19. $mail->From = 'xxxxx@gmail.com';
  20. $mail->FromName ='xxxxx';
  21. $mail->Subject = 'report sales';
  22. //$mail->AltBody = “To view the message, please use an HTML compatible email viewer!”;
  23. //optional, comment out and test
  24. //$mail->WordWrap = 80;               // set word wrap
  25. $mail->IsHTML(true);                  // send as HTML
  26.  
  27.  
  28. // ------------------------------------------------
  29. // generate body jadikan fungsi.
  30. // tinggal panggil.
  31. // tapi kalo bingung, ndak pake fungsi ya ndak apa.
  32. // ------------------------------------------------
  33. function generateBody($body='',$val=array(),$reset=0) {
  34.   if($reset==1) {
  35.     $body='<tr><td>No</td><td>nama</td><td>email</td><td>kegiatan</td></tr>';
  36.   }
  37.   else if($reset==2) {
  38.     $body='<table border="1">'.$body.'</table>';
  39.   }
  40.   else {
  41.     $body.=$body.
  42.     '<tr><td>'.$val['no'].'</td><td>'.$val['nama'].'</td><td>'.$val['email'].'</td><td>'.$val['kegiatan'].'</td></tr>';
  43.   }
  44.   return $body;
  45. }
  46.  
  47. // ------------------------------------------------
  48. $send=false;
  49. $i=1;
  50. $email='';
  51. $body='';
  52. $body=generateBody($body,array(),1);          // reset $body, isi cuma header table
  53.  
  54. while($row_spc=mysql_fetch_assoc($spc)) {
  55.   if($email!=$row_spc['email']) {
  56.     if($i!=1) {
  57.       $body=generateBody($body,array(),2);    // tutup $body table
  58.       $mail->AddAddress($email);              // set alamat email tujuan
  59.       $mail->MsgHTML($body);                  // set isi email
  60.       $mail->Send();                          // kirim email
  61.       $send=true;
  62.       echo $body.'<br>';
  63.       echo 'Message has been sent to : '.$email.'<br>';
  64.       $body=generateBody($body,array(),1);    // reset $body, isi cuma header table
  65.     }
  66.     $row_spc['no']=$i;
  67.     $body=generateBody($body,$row_spc);       // isi $body dengan data lengkap
  68.     $send=false;
  69.     $email=$row_spc['email'];
  70.     $i++;
  71.   }
  72.   else {
  73.     $row_spc['no']='';
  74.     $row_spc['nama']='';
  75.     $row_spc['email']='';
  76.     $body=generateBody($body,$row_spc);      // isi $body dengan hanya data kegiatan
  77.     $send=false;
  78.   }
  79. }
  80.  
  81. if(!$send) {                                 // periksa ada pesan terakhir yg belum dikirim
  82.   $body=generateBody($body,array(),2);       // tutup $body table
  83.   $mail->AddAddress($email);                 // set alamat email tujuan
  84.   $mail->MsgHTML($body);                     // set isi email
  85.   $mail->Send();                             // kirim email
  86.   echo $body.'<br>';
  87.   echo 'Message has been sent to : '.$email.'<br>';
  88. }
  89. ?>
  90. </p>
  91. <p>&nbsp;</p>
RAW Paste Data