Batch emailing [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Batch emailing [Solved]

Post by reecec »

Hi,

Sorry for asking many questions but I have a problem on some work.

This script is ment to email over 2000 users

I was using mail() and got a blank page when finished so i changed to this PHP Mailer but i get the same problem

It works for a few emails but just goes blank when doing a batch


When finished its ment to come back to this same page and say how many sent

Any Ideas??


Any replys will be appreciated.


Regards,

Reece

Code: Select all

if(isset($_POST[s1]))
{
	if(empty($_POST[subject]) || empty($_POST[mm]))
	{
		echo "<br><center><font color=red size=2 face=verdana><b>All fields are required!</b></font></center>";
	}
	else
	{
		//get the users list
		$q1 = "select * from dd_newsletter";
		$r1 = mysql_query($q1) or die(mysql_error());
	
		if(mysql_num_rows($r1) > '0')
		{

require("../../phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = "admin@...........com";
$mail->FromName = "Test";
$mail->Host     = "mail............com";
$mail->Mailer   = "smtp";

$mail->IsHTML(true);                               // send as HTML

			$subject = $_POST[subject];

$msg=$_POST[mm];
$msg=str_replace("\\", "", $msg);
$i=0;

$_SESSION['count']=0;

			while($a1 = mysql_fetch_array($r1))
			{
				
				
$to = $a1[nemail];





    $mail->Subject = $subject;

    $mail->Body    = $msg;

    $mail->AddAddress($to);

    if(!$mail->Send())
{
        echo "There has been a mail error sending to " . $to . "<br>";

}

    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();




$_SESSION['count']++;
				$i++;
			}

		}

		if(empty($i))
		{
			$i = "0";
		}

		echo "<br><center>$i messages was sent</center>";
	}
}

//get the users number
$q2 = "select count(*) from dd_newsletter";
$r2 = mysql_query($q2) or die(mysql_error());
$a2 = mysql_fetch_array($r2);

if($a2['0'] > '0'){
	$list_link = "<br><a href=\"newsletter_subscribers.php\" class=\"Nav\">Click here</a> to list the subscribers.";
}
Last edited by reecec on Sun Feb 25, 2007 11:53 am, edited 3 times in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You have to open a socket. PHPMailer can handle sockets but it'd be better to use SwiftMailer.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Blank pages usually indicate you have error reporting turned off. Turn it on.

Code: Select all

error_reporting(E_ALL);
Try using Swift Mailer.... it uses less memory than PHPMailer. It's possible that your issue is memory exhaustion related. Or that the script is timing out after 30 seconds. Turning on error reporting will tell you that.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks both for your comments

Code: Select all

if(isset($_POST[s1]))
{
	if(empty($_POST[subject]) || empty($_POST[mm]))
	{
		echo "<br><center><font color=red size=2 face=verdana><b>All fields are required!</b></font></center>";
	}
	else
	{
		//get the users list
		$q1 = "select * from dd_newsletter";
		$r1 = mysql_query($q1) or die(mysql_error());
	
		if(mysql_num_rows($r1) > '0')
		{


//Load in the components

require('../../Swift/Swift.php');

require('../../Swift/Swift/Connection/SMTP.php');

//Instantiate swift


$swift = new Swift(new Swift_Connection_SMTP('localhost'));


			$subject = $_POST[subject];

$msg=$_POST[mm];
$msg=str_replace("\\", "", $msg);
$i=0;



			while($a1 = mysql_fetch_array($r1))
			{
				
				
$to = $a1[nemail];





//Create the message
$message =& new Swift_Message($subject);
$message->attach(new Swift_Message_Part($msg, "text/html"));
 
//Now check if Swift actually sends it
if (!$swift->send($message, $to, "admin@.......com")) echo "Failed";



				$i++;
			}

		}

		if(empty($i))
		{
			$i = "0";
		}

		echo "<br><center>$i messages was sent</center>";
	}
}

does it automatically use sockets or do i have to tell swift mailer

there's not really a way i can test this as i cant keep sending out the same newsletter and i cant send 2500 emails to myself so do you think it will work.


Thanks all.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Using the SMTP connection with Swift, like you have done, will automatically use sockets yes.

I still see errors in your code so you probably didn't enable error reporting ;)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

reecec wrote:i cant send 2500 emails to myself
Sure you can. Install an SMTP server on your development machine, configure it so that it doesn't actually send the emails, and set PHP to use it. Then PHP will connect, send lots of email, and you can examine what it did by looking in the queue and log files. I run an email marketing application and I've frequently sent more than 100,000 emails to my local SMTP server just to delete them again a little while later. I wrote my sender in Perl rather than PHP but the principle is exactly the same.

If you're sending lots of email you really need to have some sort of system in place for testing.
DrTom
Forum Commoner
Posts: 60
Joined: Wed Aug 02, 2006 8:40 am
Location: Las Vegas

Post by DrTom »

It's also quite possible your hitting the max_execution time (defaults to 30 seconds).
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Hi,

The email system works fine for a limited amount emails

I have error error reporting on.

but on alot of emails it comes back blank or wants me to download the php file

i sent them to myself and i they are getting sent

i have a feeling this is the server and not the swift script.

but why a blank page?

Im using "firefox" but my client using "ie" says he gets page cannot be displayed


thanks again
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ok, if it wants you to download the PHP file it's one of three things:

1. Infinite Loop
2. Memory exhaustion
3. Segfault

You can't fix #3 without re-installing PHP. You can fix the other two. Where's the entire code?
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks for your reply

Code: Select all

if(isset($_POST[s1]))
{
        if(empty($_POST[subject]) || empty($_POST[mm]))
        {
                echo "<br><center><font color=red size=2 face=verdana><b>All fields are required!</b></font></center>";
        }
        else
        {
                //get the users list
                $q1 = "select * from dd_newsletter";
               $r1 = mysql_query($q1) or die(mysql_error());
       
               

		if(mysql_num_rows($r1) > '0')
		{

//Load in the components

require('../../Swift/Swift.php');

require('../../Swift/Swift/Connection/SMTP.php');

//Instantiate swift


$swift = new Swift(new Swift_Connection_SMTP('localhost'));


                        $subject = $_POST[subject];

$msg=$_POST[mm];
$msg=str_replace("\\", "", $msg);
$i=0;

while($a1 = mysql_fetch_array($r1))
			{   
                               
$to = $a1[nemail];



//Create the message
$message =& new Swift_Message($subject);
$message->attach(new Swift_Message_Part($msg, "text/html"));
 
//Now check if Swift actually sends it
if (!$swift->send($message, $to, "admin@......com")) echo "Failed";


                                $i++;
                        }

                
}
                if(empty($i))
                {
                        $i = "0";
                }

                echo "<br><center>$i messages was sent</center>";
        } 
}

//get the users number
$q2 = "select count(*) from dd_newsletter";
$r2 = mysql_query($q2) or die(mysql_error());
$a2 = mysql_fetch_array($r2);

if($a2['0'] > '0'){
	$list_link = "<br><a href=\"newsletter_subscribers.php\" class=\"Nav\">Click here</a> to list the subscribers.";
}

its not the loop I think its memory
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Cearly you haven't listened to my early prompts to enable error reporting because I can see with my own eyes that there are still problems with your code.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

I have enabled error reporting i did listen to you

Code: Select all

error_reporting(E_ALL);
What errors you can see because php doesnt give any and the form comes up.

Regards,

Reece
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", "On");
The thing that's immediately jumping out at me is the lack of quotes on your array indeces:

Code: Select all

$_POST[subject]

//should be

$_POST["subject"]
The same for all the other array indeces. I haven't looked more closely ;)
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

I know this is how it was coded before (not me)..... php takes it as ' ' I changed it myself to speed it up but still not solved on another script

So is this a server issue.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Code: Select all

error_reporting(E_ALL);
ini_set("display_errors", "On");


if(isset($_POST['s1']))
{
        if(empty($_POST['subject']) || empty($_POST['mm']))
        {
                echo "<br><center><font color=red size=2 face=verdana><b>All fields are required!</b></font></center>";
        }
        else
        {

  //get the users list
                $q1 = "select * from dd_newsletter";
               $r1 = mysql_query($q1) or die(mysql_error());
       
               

		if(mysql_num_rows($r1) > '0')
		{



            
//Load in the components

require('../../Swift/Swift.php');

require('../../Swift/Swift/Connection/SMTP.php');

//Instantiate swift


$swift = new Swift(new Swift_Connection_SMTP('localhost'));


                        $subject = $_POST['subject'];

$msg=$_POST['mm'];
$msg=str_replace("\\", "", $msg);
$i=0;





//Create the message
$message =& new Swift_Message($subject);
$message->attach(new Swift_Message_Part($msg, "text/html"));
 

$to=& new Swift_RecipientList();

                    while($a1 = mysql_fetch_array($r1))
			{   

$to->addTo($a1[nemail]);
                               
                            
  

 




                                $i++;
                        }
 
//The number of successful recipients is returned here (hopefully 3!)

$number_sent = $swift->send($message, $to, "test@........com");


echo $number_sent;




                
                if(empty($i))
                {
                        $i = "0";
                }

                echo "<br><center>$i messages was sent</center>";
        } 
}

}
Swift batch function.............................??

Regards

Reece
Post Reply