Mengirimkan Email Massal dengan PHP dan MySQLi


Trik ini ditujukan untuk pemula yang berniat untuk menambahkan fitur notifikasi melalui email secara massal kepada seluruh user pada aplikasi PHP yang sedang dikembangkan.

Written by Rizki Wisnuaji on Monday September 28, 2015
Categories: tutorial, programming, comestoarra - Tags: php, mysqli - Comments
Rizki Wisnuaji

Halo para pembaca setia jagocoding.com, kali ini saya mencoba membuat tutorial untuk mengirimkan surel atau email secara massal. Trik ini dapat diimplementasikan jika pembaca berniat untuk menambahkan fitur notifikasi melalui email secara massal kepada seluruh pengguna pada aplikasi PHP/MySQLi yang sedang dikembangkan.

Pertama-tama, buat tabel melalui PHPMYADMIN dengan nama tusers :

CREATE TABLE IF NOT EXISTS `tusers` (
  `IdUser` int(5) NOT NULL AUTO_INCREMENT,
  `Email` varchar(255) COLLATE utf8_bin NOT NULL,
  `Password` varchar(255) COLLATE utf8_bin NOT NULL,
  `FirstName` varchar(255) COLLATE utf8_bin NOT NULL,
  `LastName` varchar(255) COLLATE utf8_bin NOT NULL,
  `Bio` longtext COLLATE utf8_bin,
  `Address` longtext COLLATE utf8_bin,
  `Phone` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `CellPhone` varchar(200) COLLATE utf8_bin DEFAULT NULL,
  `Notes` text COLLATE utf8_bin,
  `Active` int(1) NOT NULL,
  PRIMARY KEY (`IdUser`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;

Selanjutnya, kita akan membuat kode php untuk mendapatkan seluruh email pengguna yang telah tersimpan didalam database.

$sql = "SELECT Email FROM tusers WHERE Active = '1'";
$result = mysqli_query($mysqli, $sql) or die('Error, retrieving User Email list failed. ' . mysqli_error());
 
$emailUsers = array();
  while ($useremails = mysqli_fetch_assoc($result )) {
 $emailUsers [] = $useremails ['Email'];
  }
 
//Pisahkan email user
$allusers = implode(',',$emailUsers );

Berikut adalah contoh penggunaan dalam bentuk form untuk mengirimkan email secara massal :

<?php
if (isset($_POST['submit']) && $_POST['submit'] == 'SendEmail') {
 
  // Validasi Form
  if($_POST['emailSubject'] == "") {
 echo "<script language='javascript'>alert('Please enter the Subject of your Email.');
 window.location = ''</script>";
  } else if($_POST['emailText'] == "") {
 echo "<script language='javascript'>alert('Please enter the text of the Email.');
 window.location = ''</script>";
  } else {
 
 // Kirim Email dalam format HTML
 $emailSubject = htmlentities(clean($_POST['emailSubject']));
 $emailText = htmlentities(clean($_POST['emailText']));
 $AppName = 'JagoCoding.com';
 $SenderEmail = '[email protected]';
 
 $subject = $emailSubject;
 
 $content = '<html><body>';
 $content .= '<h3>'.$subject.'</h3>';
 $content .= '<hr>';
 $content .= '<p>'.$emailText.'</p>';
 $content .= '<hr>';
 $content .= '<p>Thank you,<br>';
 $content .= '</body></html>';
 
 $headers = "From: ".$AppName." <".$SenderEmail.">\r\n";
 $headers .= "Reply-To: ".$SenderEmail."\r\n";
 $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 
 if (mail($allusers, $subject, $content, $headers)) {
 echo "<script language='javascript'>alert('The Email has been sent to all Users.');
 window.location = ''</script>";
 $_POST['emailSubject'] = $_POST['emailText'] = '';
 }
 
  }
}
?>
 
<h3>Send an Email to All Active Users</h3>
<hr />
<form action="" method="post">
  <div class="form-group">
 <label for="emailSubject">Subject</label>
 <input type="text" class="form-control" name="emailSubject" id="emailSubject" value="<?php echo isset($_POST['emailSubject']) ? $_POST['emailSubject'] : ''; ?>" />
  </div>
  <div class="form-group">
 <label for="emailText">Email Text</label>
 <textarea class="form-control" name="emailText" id="emailText" rows="8"><?php echo isset($_POST['emailText']) ? $_POST['emailText'] : ''; ?></textarea>
  </div>
  <button type="input" name="submit" value="SendEmail" class="btn"><i class="icon-check"></i> Send Email</button>
</form>

Semoga Bermanfaat !

http://jagocoding.com/tutorial/324/Mengirimkan_email_massal_dengan_PHP_dan_MySQLi
comments powered by Disqus

— Copyright © 2019 PT. Comestoarra Bentarra Noesantarra —