Jump to content

Using MySQL Queries in phpMailer to Send Mass Email to Certain Users


bhndbrwneyes

Recommended Posts

I have an auctions website and I want to create a feature that is similar to ebay in which users will receive an email if one of the auctions they are watching will end in less than 3 hours. I will be using a cron job to call up this page every 15 minutes. When the cron job executes, I would like it to send 1 email per auction to every user that has that auction on their watchlist if the auction will be ending in 3 hours. I don't know whether it needs to be a separate email for each user or one mass email where their emails are hidden.

 

The 4 tables in my database we are concerned about are "auctions, "users, "watchlists" and "products."

 

I am trying to use the script phpMailer to execute the code because I heard it was the best one. Anways here is what I have so far. I am missing alot because I had no clue what to do.

 

<?php
require("class.phpmailer.php");


$holder = mysql_connect("localhost", "user", "password");
mysql_select_db("database", $holder);

// NEED TO FIX THIS // Need to get ALL of the auction id's where the end time is less than 3 hours and the notification hasn't already been sent //
$auctionid = mysql_query("SELECT id FROM auctions WHERE DATE_ADD(NOW(), INTERVAL 3 HOUR) <= end_time AND notification = 0", $holder);

// get the auction title of  EACH of the auctions selected above which is not stored in the auctions table but in the products table..will be used for body of email /// AGAIN, NEED THIS TO GET ME ALL OF THE NAMES OF AUCTIONS THAT ARE ENDING IN 3 HOURS//
$auctiontitle = mysql_query("SELECT name FROM products LEFT JOIN auctions ON auctions.product_id=products.id WHERE auctions.id = $auctionid", $holder);

// PROBABLY NEED TO FIX THIS // Need to get ALL of the email addresses  who have ANY of the above auction ids on their watchlist //
$email = mysql_query("SELECT email FROM users LEFT JOIN watchlists ON users.id=watchlists.user_id WHERE watchlists.auction_id = $auctionid", $holder);

// Update the auctions table. Turn notification to 1 so the notification for that auction can't be sent again // AGAIN NEED THIS FOR ALL OF THE AUCTIONS ENDING IN 3 HOURS //
$query1="UPDATE auctions SET notification = '1' WHERE id = '$auctionid'";
mysql_query($query1) or die(mysql_error());



$mail = new PHPMailer();

$mail->From = "[email protected]";
$mail->FromName = "Site Name";


// Getting and error message for the foreach but I saw a similar example and this is what I was told to do // NEED THIS TO ADD EACH OF THE EMAIL ADDRESSES INDIVIDUALLY //
foreach ( $email as $recipients ) {
$mail->AddAddress ($recipients);
}
$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Your Watched Auction is Ending Soon";

// Sample Body // WANT TO DISPLAY THE TITLE OF THE AUCTION (NAME OF PRODUCT) FOR THE AUCTION ID USING $auctiontile FROM ABOVE //
$mail->Body    = "Your auction titled $auctiontile is ending soon";

// Same as above //
$mail->AltBody = Your auction titled $auctiontile is ending soon";


if(!$mail->Send())
{
   echo "Message could not be sent.";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.