I have a job site (in CI) and there can be x number of jobseekers.What i have to do is send revalent jobs according to users job category and location.So there is different message for different jobseeker.
i am using phpMailer to send email for now i have done
$subject = 'Revalent Jobs For Your Profile';
foreach ($job_receiving_users as $job_receiving_user){
$this->send_email->send_email(FROM_NOREPLY_EMAIL,ORG_NAME,$job_receiving_user['email'],$job_receiving_user['username'],$subject,$job_receiving_user['message']);
$time = time() + 10;
while( time() < $time){
// do nothing, just wait for 10 seconds to elapse
}
}
(There is phpMailer email sending method inside library send_email)
There is limit of 200 email per hour from server or can extend it to 500.
What i want to know is this good way to send email?
if i keep 10secod gap between every email will it keep my server busy.All sql actions were done above this code and $job_receiving_users is array of user email,message and username extracted above.
Base your code on the mailing list example provided with PHPMailer
What you're doing in your loop is called "busy waiting"; don't do it. PHP has several sleep functions; use them instead. For example:
$sendrate = 200; //Messages per hour
$delay = 1 / ($sendrate / 3600) * 1000000; //Microseconds per message
foreach ($job_receiving_users as $job_receiving_user) {
//$this->send_email->send_email(FROM_NOREPLY_EMAIL,ORG_NAME,$job_receiving_user['email'],$job_receiving_user['username'],$subject,$job_receiving_user['message']);
usleep($delay);
}
This will cause it to send a message every 18 seconds (200/hour), and the use of the sleep function will mean it consumes almost no CPU while it's waiting.
Related
$i = 1;
foreach ($recipients as $email => $name) {
$mail->ClearAddresses();
$mail->AddBCC($email, $name);
if (!$mail->send()) {
$send = 0;
} else {
$send = 1;
}
$query = "INSERT INTO `newsletter_send`(`email`, `id_newsletter`, `date`, `send`) VALUES ('$email',$id_newsletter, NOW(),$send) ";
$stmt = $link->prepare($query) or die('error');
$stmt->execute();
$mail->clearAllRecipients();
if (($i % 100) == 0) {
sleep(60);
}
$i++;
}
What is the best way to send a large emails without sleep() and without to wait the page to finish loading? In addition to the cron job you have other ideas ?
EDIT: I have 680 users who will receive the email, but after a while I get 500 Internal Server Error.. why? It maybe time_limit?
Message queues.
beanstalkd is a good solution.
You can then use a SDK like pheanstalk to handle the queue and its jobs.
EDIT: If you have restricted access to your server (for example, if you are using a shared hosting) message queues as a service are also an option.
IronMQ
CloudAMQP
AWS (Amazon Web Services) SQS
A good way to send a large amount of emails at a fast pace is to have a lot of worker scripts doing the job instead of 1 php page (GiamPy gave a good example for one of the ways that can be done and I won't mention it since I don't want to be redundant).
One simple (though somewhat hacky) option is: for you to make 20 php scripts in a file. You could name them mailer1.php, mailer1.php, ..., mailer20.php. Then, you could create a folder called mail and put two files inside:
mail/config.txt
and
mail/email.txt
Inside mail/config.txt, you would include the following lines of text:
T
15
where the first line has a T for TRUE meaning you want the mailers to send the mail out as fast as they can in intervals of 15 seconds each. You can obviously change the interval time as well to whatever you like.
And in mail/email.txt you would have the complete email you want to send
After having done all that you make the mailer files. You can make 1 first, write the code, and then copy paste it 19 times to have 20 scripts in total. The code inside could look something like this:
<?php
$pathconfig = "mail/config.txt";
$pathemail = "mail/email.txt";
$email = file_get_contents($pathemail);//now you have the email saved
$filehandleconfig = fopen($pathconfig, "r");
$bool = trim(fgets($pathconfig));
$sleeptime = (integer) trim(fgets($pathconfig));
fclose($filehandleconfig);
while ($bool === 'T')
{
//... code that sends the email
//recheck if 'T' is still 'T':
$filehandleconfig = fopen($pathconfig, "r");
$bool = trim(fgets($pathconfig));
fclose($filehandleconfig);
sleep($sleeptime);
}
?>
So what the previous code would basically do is extract the email that needs to be sent at the beginning, and also extract the time it will sleep after sending an email, and if it should continue to send emails.
What that means is that the mail/config.txt file is your controlpanel, and if you change 'T' to be anything else that 'T' (like 'F' for instance), then all the scripts will terminate.
The downside to this option is that it's a bit hacky, though the upside is that it can be developed in a matter of minutes.
I would like to create a feature for my web application that once a user is entered in my database, every 4 weeks an email is sent to them reminding them to, for example, give some feedback. I've heard cron job is what Im looking for but Im curious what else is out there, is there maybe a php script that exists or an easy way to do it?
I want something like a countdown from once they enter the database to start counting down till 4 weeks has passed then call a php file or something that sends an email of my choosing to them. if this is possible let me know! thank you
I would say to use a cron job (it could run everyday at a certain time that would be good to send an email), and the cron job could call a php script that would look through all your users and check when they signed up, and see if anyone signed up 4 weeks ago (or some multiple of that). For anyone who meets this condition, you could go through a loop and send them emails with the mail() function.
Cron Job
Log into a shell on your server and type "sudo crontab -e" and type in something like this:
30 14 * * * php path/to/some/phpscript.php
In this example, phpscript.php is going to be run at 14:30 every day (2:30 pm). But that doesn't mean it's going to email all the users every day! See the script below.
PHP Script
<?php
# get all users (or your query could choose only users who signed up (4 weeks)*n ago)
$result = mysql_query('SELECT * FROM user');
$users = array();
while($row = mysql_fetch_assoc($result)) $users[] = $row;
# loop through users and (if you didn't already check) see which ones have signed up (4 weeks)*n ago
foreach ($users as $user) {
# take the number of seconds and convert it to number of days
$today = (int)(strtotime(date('c')) / 60 / 60 / 24);
$signup_day = (int)(strtotime($user['signup_date']) / 60 / 60 / 24);
# check if the amount of days since signup is a multiple of 28 (4*7)
if (($today - $signup_day) && ($today - $signup_day) % 28 == 0) {
send_mail_to($user);
}
}
?>
I'm interacting with ActiveMQ via STOMP. I have one process which publishes messages and a multiple processes that subscribes and processes the messages (about 10 parallel instances).
After reading a message I want to be sure that if, for some reason my application fails/crashes, the message will not be lost. So naturally, I turned to transactions. Unfortunately, I discovered that once a consumer reads a message as a part of the transaction, all the following messages are not being sent to the other consumers, until the transaction ends.
Test case: abc queue has a 100 messages. If I activate the following code in two different browser tabs, the first will return in 10 seconds and the second will return in 20 seconds.
<?php
// Reader.php
$con = new Stomp("tcp://localhost:61613");
$con->connect();
$con->subscribe(
"/queue/abc",
array()
);
$tx = "tx3".microtime();
echo "TX:$tx<BR>";
$con->begin($tx);
$messages = array();
for ($i = 0; $i < 10; $i++) {
$t = microtime(true);
$msg = $con->readFrame();
if (!$msg) {
die("FAILED!");
}
$t = microtime(true)-$t; echo "readFrame() took $t MS to complete<BR>";
array_push($messages, $msg);
$con->ack($msg, $tx);
sleep(1);
}
$con->abort($tx);
Is there something I'm missing code-wise? Is there a way to configure ActiveMQ (or send a header) that will make the transaction remove the item from the queue, allow other processes consume the other messages, and if the transaction fails or is timed-out, will put the item back in?
PS: I thought about creating another queue - DetentionQueue for each reading process but I really rather not do it if I have a choice.
You will probably want to adjust the prefetch size of the subscription so that ActiveMQ doesn't send the Messages on the Queue to client 1 before client 2 gets a chance to get any. By default its set to 1000 so best to tune it for your use case.
You can set the prefetch size via the "activemq.prefetchSize=1" header on the subscribe frame. Refer to the ActiveMQ Stomp page for all the frame options.
I want to add a delay of about 30 to 60 seconds between sending of each email to users with phpmail() function. I am taking users email data from table and using while loop and there are about 1000 users in database table. I want there to be a delay in repeating the statement of this while loop, so that every email is sent to next user after that particular user. I am using the the following code:
$sql = "select * from users where `user_email` = '$_REQUEST[q]' or `user_name`='$_REQUEST[q]' ";
$rs_results = mysql_query($sql) or die(mysql_error());
while ($rrows = mysql_fetch_array($rs_results)) {
$fullname = $rrows['full_name'];
$usermailid = $rrows['user_email'];
$username = $rrows['user_name'];
$message =
"Hello $fullname \n
Welcome to My Site \n
The rest of the message";
mail($usermailid, "Important Message - from Site Admin", $message,
"From: \"Mysite Admin\" <auto-reply#mysite.com>\r\n" .
"X-Mailer: PHP/" . phpversion());
}
The above code sends all emails together to 1000 users in table in one go. I want that there should be delay of 30 to 60 seconds between sending of each and every email
Have a look at: http://php.net/manual/en/function.sleep.php. The php sleep function delays the script execution by x seconds.
Are you putting the sleep function inside the loop?
It might be that, even if you are, the mail() requests are just being stacked up until the script finishes running. But that's the other problem: the time your script takes to run will be 500 to 1000 minutes - longer than the maximum probably allowed by your server.
However, with a cron function you can have that wake up every minute and send one email and terminate.
I would like to automate the process of sending this email. I mean when a condition is met then the email should automatically be sent to the given email addresses which is already given as hard-coded values. So for an example in my case a Technician has to complete 40jobs a day and if he finished 35 at the end of the day an email should be sent to the supervisor(provided his email id is already given) with Subject name and the body should be like ” The Technician 1234 has completed only 35 jobs for the day instead of 40. I was wondering how can implement this as Im very new to the field of PHP. Please anyone help me out. If possible please provide me with an example.
Thanks
Dilip
You could run a cron that uses the php mail function to send a report about each programmer at a set time each day. The cron script would look something like:
<?php
$to = 'admin#mydomain.com';//the set up email to mail too
$result = mysql_query('select programmer_id, job_count, job_requirement from table'); //query the database
while($job = mysql_fetch_object($result)){
mail($to,'Job counts for ' . $job->programmer_id,"Completed $job->job_count out of $job->job_requirement jobs","FROM: jobcounter#mydomain.com");
}
Have a script check the conditions and send emails, and run it periodically: http://www.google.com/search?q=cronjob
I'm assuming here that your problem is the triggering of the event, rather than the sending of the email itself.
In the example you described, you would need to have a script run at a certain time of day that would check for any condition that would require an email sending, and then send the email. Under any unix-like system, cron is the ideal solution to this. If you are on some kind of basic shared hosting you may not be able to set this up. In that case you would need to set up a task to run on a machine that you do have control over that would call a URL that would run the PHP script. This could be a cronjob, or a Scheduled Task under Windows.
If your example was switched around so that, say, an email was to be sent as soon as a technician completed 40 jobs then you would be able to send the email as part of the script that handled the form submission from the technician whenever he completed a task.
Setup a Cron Job that runs at the "end of the day":
23 55 * * * /path/to/php /path/to/script.php
Have it run a PHP script that can query your Job Store for whatever condition you want to check. For example with Job Store being a database.
$db = new PDO(/* config */);
$result = $pdo->query(
'SELECT count(id) as "tasks_done"
FROM tasks WHERE engineer = "Tom"
AND finished_at = now()');
$result = $result->fetchAll();
if($result[0]['tasks_done'] < 40) {
mail( ... )
}
If the condition is met, send the mail. The above needs refining of course. Dont expect being able to copy/paste it.
Also see:
What is the best method for scheduled tasks in PHP
http://greenservr.com/projects/crontab/crontab.phps
you should use mail() function:
$technicianId = 1234;
$jobsNeeded = 40;
$jobsDone = getJobsDone($technicianId);
if ($jobsDone <= $jobsNeeded) {
mail('supervisor#yourcompany.com', 'Technician '.$technicianId.' slacking', 'The Technician '.$technicianId.' has completed only '.$jobsDone.' jobs for the day instead of '.$jobsNeeded);
}
Could have a cronjob that runs at the end of each day which checks what each technician has done and emails it to the manager.