schedule an email in php - php

I want to schedule sending of email from php script. I want user to specify date and time and then on the specified date and time, I want those emails to be sent automatically. How do I implement it? I am working on codeigniter.

One way to do it would be to create a "scheduled_emails" database table. Put all the emails you want to queue in there, including columns such as, recipient, subject, message and optional headers.
You could then set up a script to look at that table and send any emails that have a "send_time" which is greater than the current time. You could then set up a cron job to run this script every.. 5 minutes for example.

PHP usually uses an external scheduler for this sort of thing. That means cron on *nix or Windows Task Scheduler on Windows.
If you want to set it up through a web interface, then you might consider storing your schedule in a database and having cron (etc) kick off a script that looks for overdue emails every 5 minutes.

Related

PHP dynamic schedule Email sending out

My application is running in Wordpress as a plugin. The application has many projects with a value of email frenquency, which will be saved on the database and the value will be: weekly, daily and every Tuesday and Thursday.
Now, I'm going to set up a email scheduler that the scheduled emails will be sent to the projects participants weekly, daily or twice a week depends on the frenquency.
My question is how can I set the email scheduler first. As Wordpress cron is based on the page loading, whereas my purpse is not relied on any page load, so I cannot use the Wordpress cron system. I've seen some articles suggest to use server cron itself. Can anyone give me a brief based on my requirements? Is it possible I can get a dynamic value from database using in the cron script? Plus, I didn't use Laravel for this one. I know Laravel has the task scheduling functionality.
The second quesiton is if I use corn with php script, can I set up the schedule to every Tuesday and Thursday?
Thirdly, I also see some libraries like SendGrid which has a schedule functionality, should I use it or keep on Cron?
Code PHP script(s) to handle sending emails according to data retrieved from DB.
Then create Cron to call certain PHP script(s) which runs on daily basis if customers can choose which day they prefer to get the emails.
Yes.
Cron is more secure, stable and independant.

Send email after every interval of time in php without using ajax

I am making web application in PHP. I need to send mail to my users after every interval of time (say after every two days). How can i keep track of time intervals in php (assume i know the start time).
You could create a cron job that runs one of your php scripts locally, and have that script check against a database to see whether or not the time is appropriate to send the email. For example,
In your crontab:
0 0 */2 * * php /usr/bin/mailcheck.php
And in mailcheck.php, just have logic to check for entries in a database that are overdue for an email.
Or, if you just want to blindly send an email to everyone every interval, just have a sendmail() entry in mailcheck.php, and that'll do it too.

User configurable notification frequency in LAMP Web App

I am developing a Web Application for businesses to track the status of their repairs & part orders that is running on LAMP (Linux Apache MySQL PHP). I just need some input as to how I should go about allowing users to customize the frequency of email notifications.
Currently, I just have a cron job running every Monday at 6:00AM that runs a php script that sends an email to each user of their un-processed jobs. But I would like to give users the flexibility of not only choosing the time they are sent at, but the days of the week as well.
One idea I had was, some way or another, storing their email notification preferences in a MySQL database, and then writing a php script to notify via email but only if the current date/time fits within the criteria they have set & write in code to prevent it from being sent twice within the same cycle. Then I could just run the cron job every minute or 5 or whatever.
Or would it be better to somehow create individual cron jobs for each user programatically via php?
Any input would be greatly appreciated! :)
No you are right.
Individual crons will consume many resources. Imagine 10k of users with a request to send mail at different times ... this imply 10k of tasks.
The best solution is to create a cron task that will run on your users and take the correct actions.
Iterate on your users, check the date/time set up, detect change and send mail with adding a flag somewhere so said "it's done" (an attribute last_cron_scandate or next_calculated_cron_scandate could be a good solution)

Execute a timed command in a time command

I am working with an php application where i need to check a database of users for whom has birthday today.
This i do through a cron job.
Now, when i get the users with birthday, i need to send them a SMS.
The SMS has an individual time associated which determins when the SMS should be send.
Lets say John and Peter has birthday today. Peter works from 01-09 so he should get the SMS at 01 and Peter works from 08-16 so he should get his SMS at 08.
I was thinking about dynamically creating the same amount of cron jbos which equals the people who has birthday on a given date. Those cron jobs would then send the SMS to the appropiate people this one time and thats it.
My question is, is there a smarter way to do this?
Secondly, if the cron job generation idea is good enough, is there a way to remove individual old cron jobs so i dont clug up the cron job list with old jobs which will never be run again?
Thanks in advance.
If you would like to send messages to each person at a specific time using cronjobs then you would have to create a cronjob for each person. Very bad idea in my eyes as you could end up with over 1000 cronjobs!
You would be much better with a system that runs a cronjob every 5 minutes or so (however often you want), and this cronjob executes a PHP script to determine if there are any messages to send out at that time, or between now and the last cronjob.
Whilst this may mean your users could get their message up to 5 minutes late, it is unlikely because nobody I know starts work at 8:02, and therefore, if the cronjob is at 8:00, 8:05, 8:10 etc, you will almost always get the message sent right on time!
If you're worried about creating too many cron jobs (not sure how many would be a problem on your system), then why not create just one cron job at the OS level? Then you can call your program every 10 minutes or so, and it can check if there are new people that need to be sent an SMS.

notify by email on expiry date

I'm working on a database that saves records for laborers details
such as name, nation, visa expiry date.
So, I want to create a little script that searches and matches expiry dates with today's date and send me a notification by email within 1 month before the visa expires.
A very rough outline, assuming you already have a database table.
Every day run a script that calculates todayminusonemonth (solved in Stratton's answer) and performs a select * from databasetable where expirydate = todayminusonemonth. Iterate over the result set, compose a message with the target's specific information and send it out using php's mail interface.
It's by far the easiest solution to make sure the script runs every day, if that's not an option a column should be added to the db to indicate that the mail has been sent. Or an extra table listing all days for which mails have been sent could be created...
See? This first decomposition of the problem was quite easy. Now you can start solving each of these partial problems, or look up information/howtos for each. Feel free to create a new question with more specific needs you may have.
One problem you may run into is that your webhost doesn't allow cron scripts to be executed. A very creative alternative solution using a website monitoring service is explained in I don't have cron jobs on my server. What is an alternative for sending emails without user input?
try something like this:
$monthAgo = date("y-m-d",strtotime('-1 month'));
$sql = ("SELECT * FROM tablename WHERE visa_expiry_date < '".$monthAgo ."'");
and you'll get all the visas that are 1 month away from expiration.
then you just send an email using mail
do this all in a cron job
You can try SQLyog's SQL Scheduler and Reporting Tool which has a Job Agent that allows you to generate, format and send personalized mails with results returned from a query. This allows you to schedule execution of query(s). The resultset(s) can be emailed to multiple recipients. You can specify an external file or enter query(s) that you want to execute using this tool.
Read this.
You need to look into cron jobs? Which will be executed at the specified time with given interval.

Categories