PHP dynamic schedule Email sending out - php

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.

Related

Execute a php function automatically according to a given time without using Cron

is there any way to execute a php function according to a specific given time without using CRON?
because i'm creating a script which sends notifications to the clients about their sales at a given time gaps.
**Please note : time gaps will be selected by the clients so there should be an option for them to setup the time gap **
It could get really messy if you start programmatically setting cron tasks for each user, but I think cron would would be part of the solution.
I think it would be better to store each client's scheduling preferences in a database, then run a script periodically (with cron) to check whether it's time to send their notifications, and then send accordingly.
This would enable them to update their preferences without having to find and update (or remove) individual cron jobs.

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)

how to trigger email sending automatically from the database

Hello I'm battling with some problem here. I have a message in my mysql database that I want to send based on the scheduled date is there any way to do this : that when is the exact date the server should send the maill by itself, OR is there anyway to make a page load itself everyday without my access to send the email(if no one visit the site). Thanks. I'm using php and mysql db. I have scheduleddate column in my database
You could use a cron job to call a script which checks the database at a set interval (perhaps once per hour) and then have that script pull out the relevant messages and sends them.
The cron for that would look something like:
0 * * * * /path/to/email/script.php
This script could then query the database and send those messages which need sending. Of course you could set this to run every single minute (* * * * *), but I'd be very wary of having any script run every single minute!
This isn't really ideal thought, and instead you should look at using some form of queue management. If you take a look at queues.io you can find lots of info on various popular queue projects projects.
I also found this post on setting up background jobs using resque very helpful!
Of course how you integrate such a system is very much dependent on your codebase and your environment.
Some frameworks have this kinda functionality integrated very well, I have worked with Laravel 4's queue component quite a lot and it really does make it very easy to work with..
In reality many people just end up using a cron/script combo to do the job, but it's important to know there are other (better) ways to achieve this.
The best way to do this without killing your CPU utilization is by scheduling it via a scheduled cron task on your server. Write your code to send the email, then add that page to a daily cron in your server's Admin Control Panel.

Triggering code at a certain time

Is it possible to tell PHP to execute a piece of code on a given date and time? For example, Blogger.com allows someone to set a blogpost to be published in the future (e.g. 12/12/14 6:00AM).
Can PHP do something similar?
(Sorry, I don't even know what the correct term for events like these would be to be able to even search for them! :( )
You can do this using a cron job (or scheduled task on Windows); although they are typically used for reoccurring jobs.
If you're using a database, most platforms come with a scheduler.
You can schedule your action in your database and use a cronjob on your server or use a cron job service To run your actions.
https://www.setcronjob.com/
For example when you want to publish your blog in the future, you save your publish date in the future and set some sort of auto-publish bit.
Then every hour a PHP script is ran by a cronjob, this script checks the database for all blogs which need to be published.
It's not possible to tell PHP to do this itself, since it would require a process to run forever to periodically call your PHP code. Thankfully though, there's a couple of things which do this:
1) Call a PHP script from a cron job, which then does any necessary work. If you don't have access to a crontab, you can periodically call this when a user pings your site instead, although that will be less reliable, of course.
2) Use at. This works in basically the same way as cron on Linux systems, but will schedule once and at an exact time.
for "triggering code at a certain time", cron works. But for something as simple as publishing an article at a specific time, it isn't needed. You can just store a publish date with your article. When displaying a list of articles you can adjust your query to something like WHERE PUBLISH_DATE <= NOW() and on the article page check if the article's publish date has passed before showing the article.
On Unix-like systems, there's Cron. You can manage Cron from PHP.
On Windows, there are scheduled tasks - you can also use PHP to manage scheduled tasks.
Be careful with this though - it's kinda hard to test, and you may end up with a schedule that cripples your server.

schedule an email in 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.

Categories