Page 1 of 2
Batch emailing [Solved]
Posted: Sun Feb 18, 2007 1:22 pm
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.";
}
Posted: Sun Feb 18, 2007 1:26 pm
by neophyte
You have to open a socket. PHPMailer can handle sockets but it'd be better to use SwiftMailer.
Posted: Sun Feb 18, 2007 1:28 pm
by Chris Corbyn
Blank pages usually indicate you have error reporting turned off. Turn it on.
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.
Posted: Sun Feb 18, 2007 3:56 pm
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.
Posted: Sun Feb 18, 2007 5:17 pm
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

Posted: Sun Feb 18, 2007 6:30 pm
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.
Posted: Sun Feb 18, 2007 9:31 pm
by DrTom
It's also quite possible your hitting the max_execution time (defaults to 30 seconds).
Posted: Wed Feb 21, 2007 11:52 am
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
Posted: Wed Feb 21, 2007 12:25 pm
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?
Posted: Wed Feb 21, 2007 12:32 pm
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
Posted: Wed Feb 21, 2007 1:00 pm
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.
Posted: Wed Feb 21, 2007 1:22 pm
by reecec
I have enabled error reporting i did listen to you
What errors you can see because php doesnt give any and the form comes up.
Regards,
Reece
Posted: Wed Feb 21, 2007 2:16 pm
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

Posted: Wed Feb 21, 2007 3:35 pm
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.
Posted: Thu Feb 22, 2007 12:01 pm
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