9 Replies - 1962 Views - Last Post: 17 November 2012 - 10:51 AM Rate Topic: -----

#1 c7m4d4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-November 12

Sending email with SMTP through PHP

Posted 16 November 2012 - 02:11 AM

Ok, so i am running a groupon clone script that sends the daily automated email updates with smtp but uses a php script. I currently have over 800 email subscribers. When talking to hostgator they said i will only be able to send 500 emails an hour and the rest will not go through. So i am trying to find the code to throttle the emails, but the only code I can find that comes remotely looks like what might control the sending is this..

$between_delay = 75; //max limit of mails send at a slot
$send_count = 1; 
$send_delay = 1; //Delays the program execution for the given number of seconds.



Do you think this is the right code? if so what numbers should be inserted so i know all my emails will get sent out?

Thanks guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Sending email with SMTP through PHP

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 06:03 AM

Here's how I would do it:

1. Create a database table of "SendQueue" and save the emails of the people you need to send and email to, and also save the template the email must use.

ID    Email         Template_ID
1     foo@bar.com   3            //New TV Ad
2     asd@bar.com   3            //New TV Ad
3     qwe@bar.com   3            //New TV Ad
4     ehr@bar.com   3            //New TV Ad
5     foo@bar.com   5            //New Refrigerator Ad
6     ehr@bar.com   5            //New Refrigerator Ad


2. Every hour on the hour, take the first 499 emails added, send the emails, remove those 499 from the queue table.

3. Create a cron task on your webhost and invoke that PHP script every hour.

---

Simple enough, no?
Was This Post Helpful? 0
  • +
  • -

#3 c7m4d4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-November 12

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 01:21 PM

To be honest, I do not know too much about creating databases.. I was hoping i could change those 3 number and that would determine how many are sent at time, how many times to send, and the delay between each sending, but i am not positive... here is the full code from the script...
<?php
//error_reporting(E_ALL);
//error_reporting(E_STRICT);
date_default_timezone_set('America/Los_Angeles');
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

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

$body             = $message;
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP

// $mail->SMTPDebug  = 2;                   
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

$mail->SMTPAuth   = true;                           // enable SMTP authentication
$mail->SMTPSecure = SMTP_TRANSPORT_LAYER_SECURITY;  // sets the prefix to the server
$mail->Host       = SMTP_HOST;      	            // sets SMTP server HOST name
$mail->Port       = SMTP_PORT;                      // set the SMTP port for the server
$mail->Username   = SMTP_USERNAME;  	           // SMTP username
$mail->Password   = SMTP_PASSWORD;                 // SMTP password

$mail->SetFrom(FROM_EMAIL, FROM_NAME);

$mail->AddReplyTo(REPLY_TO_EMAIL,REPLY_TO_NAME);

$mail->Subject    = $subject;

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

//$mail->AddAddress(SITE_EMAIL, APP_NAME);

$to_list = explode(',',$to);

$between_delay = 75; //max limit of mails send at a slot
$send_count = 1; 
$send_delay = 1; //Delays the program execution for the given number of seconds.

ignore_user_abort(true); // Ignore user aborts and allow the script to run forever
set_time_limit(300); //to prevent the script from dying

foreach($to_list as $row)
{

	if ( ($send_count % $between_delay) == 0 ){
		sleep( $send_delay ); //Delays the program execution for the given number of seconds.
	}
	$address = $row;
	if(!empty($address)) {
		$mail->AddAddress($address, "User");
		$mail->Send();	
		$mail->ClearAddresses(); //clear address
	}
$send_count++;

}

if(!empty($mail->ErrorInfo)) { 

	  set_response_mes(-1,"Mailer Error: " . $mail->ErrorInfo);
	  if($_SESSION['userrole'] == 1)
	  {
		$url = substr($_SERVER['REQUEST_URI'],1); 
		if($url=='admin/daily_mails/' || $url=='admin/daily_mails.php/'){ 
			$url='admin/profile/'; 
		}
		url_redirect(DOCROOT.$url);
	  }
	  url_redirect(DOCROOT);

}

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

?>



To be all honest, it was an Indian company i got the script from and the language they use is kinda throwing me off... what do you think?
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 01:45 PM

It could work or it could not work. I can't read and parse all that code to be honest.
Was This Post Helpful? 0
  • +
  • -

#5 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 02:44 PM

Hey,

All that code does is delay one second every 75 emails. If you are trying to limit the number you send to 500 an hour you are going to have to record who has received emails for the day and who still needs them sent.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 03:33 PM

You could just create a PHP script that runs forever in the background, sending one email every 3600/500 seconds, keeping track of the last email it sent to and using that to find the next email to use. That way you aren't flooding the email server with 500 emails once every hour.

Should be no more complex than this:
<?php
// Remove the time limit from the script execution.
set_time_limit(0);

// Calculate the delay needed between messages so that
// you never reach more than 500 an hour. (In microseconds!)
$maxPerHour = 500;
$delayBetweenMessages = 1000000 * 3600 / $maxPerHour;

// This variable will hold the ID of the last email address to
// receive a message. In the loop it will be used to fetch the
// next email.
$lastEmailID = 0;

// Start an infinite loop.
while (true) {
    // Create a SQL query to find the next email address in line.
    $sql = "SELECT id, email FROM subscribers
            WHERE id > {$lastEmailID}
            LIMIT 1";

    // Execute the query and get the result set.
    // I'll leave that up to you, as I don't know how you interface
    // with your database. I'll just assume you return a $row array
    // with the required data.
    
    // Check if an email was found. If not, just reset the last ID
    // value and skip to the next iteration of the loop.
    if ($row && isset($row["id"], $row["email"])) {
        // Send an email to the given address.
        // Again, I'll leave this up to you. You've got plenty of
        // example code for this in your previous posts.
        
        // Now just store the current ID as the last ID, and then
        // sleep for the required duration before moving on.
        $lastEmailID = $row["id"];
        
        usleep($delayBetweenMessages);
    }
}



Then you just that somewhere on your server, and execute it in the background.
$ php /path/to/the/script.php &



Similarly, you could write the PHP script to send a single email per execution, perhaps keeping a record of which email address was last used in a file or in a database. Then set that up to be executed as a cron job every 8 seconds. (Or 7.2 second, if Cron Jobs can be that accurate. Can't remember if they can be.)
Was This Post Helpful? 1
  • +
  • -

#7 c7m4d4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-November 12

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 05:15 PM

Couldnt i just change the between_delay to 50 then send_delay to 360 second.... that way it will send 50 emails every 6 minutes, which would equal 500 emails/hour, until all the emails are are sent.... or is am i just being way too simple?

As far as programming and writing something I suck at it, and i am not about to ask some one here to do it, so thats why i am wondering if changing these numbers will work...

Thanks for all the input so far..
Was This Post Helpful? 0
  • +
  • -

#8 c7m4d4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-November 12

Re: Sending email with SMTP through PHP

Posted 16 November 2012 - 07:14 PM

or if i change it to..
 $between_delay = 1; //max limit of mails send at a slot  

2 $send_count = 1;   

3 $send_delay = 8; //Delays the program execution for the given number of seconds. 




Would that send 1 email at a time with an 8 second delay inbetween, sending 450 emails an hour?
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Sending email with SMTP through PHP

Posted 17 November 2012 - 02:33 AM

Seems it would be simplest to just try it out. You'd have to change the set_time_limit value to 0 though, as what you are trying would require the script to run infinitely, and the code you posted is set to die after 300 seconds.
Was This Post Helpful? 0
  • +
  • -

#10 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Sending email with SMTP through PHP

Posted 17 November 2012 - 10:51 AM

One problem I can see with running a script infinitely is that if something interrupted that process, for instance the web server restarting, the script would no longer be running and you wouldn't know about it.

It seems to me a cleaner solution would be to use a cron job. If you didn't want to send all 500 at once you could still use Atli 's approach of sending an email every seven seconds or so.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1