how to trigger email sending automatically from the database - php

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.

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.

Scheduled task PHP alternatives

I'm working on an existing custom eCommerce PHP application which is currently running a very resource intensive CRON every 15 minutes.
The gist of it is: Customers can set up complex filters for products they are interested in, and receive emails based on these filters. The CRON which runs every 15 minutes, checks for all new products that have been listed since it last ran, and compares them with each customers filters. If the products match a customers filters they are send an email via amazon SES.
Up until now, this method has been working ok, but as the number of active customers is rising very quickly the CRON is starting to make a noticable performance drop on the application every 15 minutes which lasts for a minute or two while it runs.
I have been toying with other ideas to help spread out the load on the server, such as performing the task each time a product is listed, so the server doesn't need to catch up on multiple products at a time.
What is usually the best practise when approaching something like this?
My recommended approach is to use a rabbitmq queue where your cron will send messages. Then set up a couple of consumers(scripts that will wait at the other end of the queue) that will take the message one by one, compose email and send it to the customer.
This way, you can scale the number of consumers to match the volume of emails needed to be sent.
If queues are not something you're familiar with, take a look at the RabbitMQ tutorials : https://www.rabbitmq.com/tutorials/tutorial-one-php.html
The message queues fit perfectly well and you can easily make use of them with enqueue library. Just a few words on why should you choose it:
It supports a lot of transports from the simplest one (filesystem) to enterprise ones (RabbitMQ or Amazon SQS).
It comes with a very powerful bundle.
It has a top level abstraction which could be used with the greatest of ease.
There are a lot more which might come in handy.
Instead of defining cron tasks you need a supervisord (or any other process manager). It must be configured to run a consume command. More on this in the doc.
Whenever a message is published it is delivered to a consumer (by a broker) and is being processed.
I suggest using RabbitMQ broker.

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)

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.

Can php launch scripts without user interaction in order to interact with the database?

I've search on the web and apparently there is no way to launch a php script without user interaction.
Few advisors recommend me Cron but I am not sure this is the right way to go.
I am building a website where auctions are possible just like ebay. And after an amount of time the objects are not available anymore and the auction is considered as finished.
I would like to know a way to interact with the database automatically.
When do you need to know if an object is available? -> Only if someone asks.
And then you have the user interaction you are searching for.
It's something different if you want to, let's say, send an email to the winner of an auction. In this case you'd need some timer set to the ending time of the auction. The easiest way to do this would be a cron job...
There are several ways to do this. Cron is a valid one of them and the one I would recommend if its available.
Another is to check before handling each request related to an object whether it is still valid. If it is not, you can delete it from the database on-the-fly (or do whatever you need to) and display a different page.
Also you could store the time at which your time-based script was run last in the database and compare that time with the current time. If the delay is large enough, you can run your time based code. However, this is prone to race conditions if multiple users hit the page at the same time, so the script may run multiple times (maybe this can be avoided using locks or anything though).
To edit cronjobs from the shell: crontab -e
A job to run every 10 minutes: */10 * * * * curl "http://example.com/finished.php"
TheGeekStuff.com cron Examples
Use heartbeat/bot implement
ation
Cron job that runs pretty frequently or a program that starts on boot and runs continuously (maybe sleeping periodically) is the way to go. With a cron job you'll need to make sure that you don't have two running at any given time or write it such that it doesn't matter if you have more than one working at any given time. With "resident" program you'll need to figure out how to handle the case when it crashes unexpectedly.
I wouldn't rely on this mechanism to actually close the auction, though. That should be handled in your database/web site. That is, the auction has a close time and either the database constraints or your code makes it impossible to bid on a closed auction. Notifying the winner and seller, setting up the payment process, etc. are things your service/scheduled task could do.

Categories