Viewed   162 times

i have a program which use to send group emails, i set a cornjob per mins for this program, and set execute per 2nd/min, and check if the now time is match to my defined schedule time in db. if true the program will run and send email, else nth...

the problem is i want to dynamic set the amount of email sending per mins by user, let say i have 10000 emails to send, and i want to send 3 emails per min, something like that, but i cant find the way when using cronJob, (set cookie? record the current email to db?), since i found that if i use sleep(), it will cause php execute limit error...

Can anyone tell me how to do that? or using other method to do schedule task? pear? if using other methods, can show how to use it briefly ? Please And Thx

 Answers

2

I think you should set this configuration at the application level rather than the cron job level.

Let's say, cron job should run every minute but the application itself should know howmany e-mails to send when a cron job runs by checking the datetime for the last e-mail is sent, and let's say, send 3 more e-mails and so.

Monday, October 31, 2022
 
5

Drop the mail() function in favor of PHPMailer. It is way more flexible, is object oriented, much easier to configure with SMTP and has much better attachments support (if you need it).

To send your email in phpmailer you'll just need something like this to set your SMTP:

$mailer = new PHPMailer();
$mailer->Mailer = 'smtp';
$mailer->Host = '123.456.789.012';
$mailer->From = '[email protected]';
$mailer->FromName = 'Me Myself';
$mailer->AddAddress = '[email protected]';
$mailer->Subject = 'My subject line';
$mailer->Body = 'Your Body text here, in HTML if you set $mailer->IsHtml(true)';
$mailer->Send();
Tuesday, October 18, 2022
3

Well I came up with this solution similar to the PDO one. Are there any unforeseen problems with running this as a cron job?

<?php
$con = mysql_connect("localhost","root","123456");
$throttle = 0;
$batch = 50;
$pause = 10; // seconds

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("maildb", $con);

// Message Table
$MSGresult = mysql_query("SELECT * FROM msgs");

// User Table
$USERresult = mysql_query("SELECT * FROM members");

while($MSGrow = mysql_fetch_array($MSGresult))
  {
    while($USERrow = mysql_fetch_array($USERresult))
        {
          mail($USERrow['email'],$MSGrow['subject'],$MSGrow['body']);
          $throttle += 1;
          if ($throttle > $batch ) { sleep($pause); $throttle = 0;} 
        }
    mysql_data_seek($USERresult,0);
  }

mysql_close($con);
?> 
Thursday, December 22, 2022
 
chouser
 
2

Cron reads the stdout/stderr of the command that gets executed, if something is written then cron sends an E-Mail.

I guess the php-executable is compiled as "cgi" or "fcgi" so it emits those headers by default.

To solve this you have apparently three possible solutions:

  • Use the "cli" version of PHP
  • Redirect stderr and stdout to /dev/null (that means append > /dev/null 2>&1 to your cron command).
  • Define MAILTO="" (see this page).
Tuesday, November 15, 2022
 
shosti
 
2

The structure of your message is wrong. You need nested multiparts to get the right structure, something like this:

  multipart/mixed
    multipart/alternative (holding the two forms of the body part)
      text/plain
      text/html
    text/plain or image/gif (the attachment)
Saturday, December 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :