File: vendor/phpmailer/phpmailer/examples/mailing_list.phps

Recommend this page to a friend!
  Classes of Ganesh Kandu  >  kchat  >  vendor/phpmailer/phpmailer/examples/mailing_list.phps  >  Download  
File: vendor/phpmailer/phpmailer/examples/mailing_list.phps
Role: Example script
Content type: text/plain
Description: Example script
Class: kchat
Database driven PHP live chat system
Author: By
Last change:
Date: 2 years ago
Size: 2,297 bytes


Class file image Download




$mail = new PHPMailer;

$body = file_get_contents('contents.html');

$mail->Host = '';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 25;
$mail->Username = '';
$mail->Password = 'yourpassword';
$mail->setFrom('', 'List manager');
$mail->addReplyTo('', 'List manager');

$mail->Subject = "PHPMailer Simple database mailing list test";

//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop
//msgHTML also sets AltBody, but if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

//Connect to the database and select the recipients from your mailing list that have not yet been sent to
//You'll need to alter this to match your database
$mysql = mysqli_connect('localhost', 'username', 'password');
mysqli_select_db($mysql, 'mydb');
$result = mysqli_query($mysql, 'SELECT full_name, email, photo FROM mailinglist WHERE sent = false');

foreach (
$result as $row) { //This iterator syntax only works in PHP 5.4+
$mail->addAddress($row['email'], $row['full_name']);
    if (!empty(
$row['photo'])) {
$mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB

    if (!
$mail->send()) {
"Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
//Abandon sending
} else {
"Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
//Mark it as sent in the DB
"UPDATE mailinglist SET sent = true WHERE email = '" .
mysqli_real_escape_string($mysql, $row['email']) . "'"
// Clear all addresses and attachments for next loop

For more information send a message to info at phpclasses dot org.