user1250526 user1250526 - 1 year ago 96
PHP Question

PHP Mailer sending newsletter

Wondering if you could guys help me a little bit as I am messing around with PHP mailer, I have this code modified but from here

Sending single emails is working fine through PHP mailer(with a different script), but trying to send to multiple emails now with the script below from a database is currently not working.. can you spot whats wrong with it? although I am wondering if it is actually doing anything with the emails from the database.. I am a bit confused.

The script does succeed and print the names, but does not send any emails out! At least none get received..(not in spam either) any help? Sorry if this is very obvious!

// Grab our config settings

// Grab the FreakMailer class

//set execution time limit to 5 minutes

$safeMode = ( @ini_get("safe_mode") == 'On' || @ini_get("safe_mode") === 1 ) ? TRUE : FALSE;
if ( $safeMode === FALSE ) {
set_time_limit(300); // Sets maximum execution time to 5 minutes (300 seconds)
// ini_set("max_execution_time", "300"); // this does the same as "set_time_limit(300)"

echo "max_execution_time " . ini_get('max_execution_time') . "<br>";

//db connection
$con = mysql_connect("xx","xx","xx");
if (!$con)
die('Could not connect: ' . mysql_error());

mysql_select_db("xx", $con);

// Setup body
$textBody = "Dear {MEMBER_NAME},\n\nTEST";
$htmlBody = "Dear {MEMBER_NAME},<br /><br />TEST";

// instantiate the class
$mailer = new FreakMailer();

// Get the user's Email
$sql = mysql_query("SELECT displayname,email FROM engine4_users2")or die(mysql_error());

//lets reset the time limit of the server everytime an email is sent to bypass maximum
while (1==1) {
set_time_limit(30); // sets (or resets) maximum execution time to 30 seconds)
// .... put code to process in here

while($row = mysql_fetch_object($sql))
// Send the emails in this loop.
$member_name = $row->displayname;

if($row->MailType == 'html')
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $htmlBody);
$mailer->AltBody = str_replace('{MEMBER_NAME}', $member_name, $textBody);
$mailer->Body = str_replace('{MEMBER_NAME}', $member_name, $textBody);
echo "Mail sent to: " . $member_name . "<br />";

usleep(1000000); // sleep for 1 million micro seconds - will not work with Windows servers / PHP4
// sleep(1); // sleep for 1 seconds (use with Windows servers / PHP4
if (1!=1) {


Answer Source

You never set the user's address to the mailer.


before send() is called. Because you're sending the mail within the loop, every user would get a single mail and no other addresses would be seen.

(the docu's example you linked isn't complete as the comments below disclose)

You could alternatively only put an


in the loop and the rest of mailer's code after the loop to send one mail to all, but you'd need one (dummy) address as recipient besides the bcc-adresses to get the mail sent.

if($row->MailType == 'html')

You didn't select MailType in query. Do you have error reporting enabled?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download