Run php file without wait for results - php

Let me try to explain to you what i need:
the user creates a post on the site and choose which users can have access to this post. When the user submits the information, each person chosen to have access to post receives an email warning.
To perform this I use ajax through jquery. The problem is that this process may be too long until all the emails have been sent.
So I thought the process would work as follows:
1rst - Save the post;
2snd - Save the information of emails (subject, recipient, etc.) in a database table;
3th - run a php file in "parallel" to fetch the information of emails and send them - with the goal of not having to wait for it to execute.
Is the third step possible?
I hope i explain me well :)
Thank you all.

If you have access to the server crontab, simply setup a new crontab to run every minute and execute the mail sending.
This way it's not related to your workflow, so you can store the emails somehow in files or database, etc.etc., and send out later from a different script.
crontab -e -u (webserver user)

Related

Scheduling emails with cron jobs from a mysql table

I am trying to set up a system where a user enters some information in a form and an email will be constructed where the information is saved into mysql.
I am trying to figure out how to make it so the email will be sent, for example, 20 minutes after the user makes their input. (Without the user staying on the browser).
I need this delay as I need the ability for an admin to log on to a page to look at the email and possibly edit it before it sends.
Is this possible through a cron job. Am I able to set one up that automatically checks sql table for an update and then sends the email after a certain time?
Or is it possible to delay a php script with the sleep function and then send the email. Can I make the PHP script still run when user has closed site and left?
you can use mySQL to store the data sent by the user. this data will be accessed later using another script triggered by a cron Job: if you have the ability to set cron jobs in the control panel or via access to the server, go ahead, use cron tab syntax to define when the job will be triggered, this website may help you:
https://crontab-generator.org/
another approach is to use external service to trigger an event every interval, the event could be accessing the cron job script via HTTP.
if you want your email to be sent exactly after 20 mins, please add a field to your mysql table indicating the desired send date(beware of timezones).
you may also want to add a flag indicating if the email is sent, so you do not send the same email twice.
You can't (easily) have a PHP script stay alive that long.
Your best strategy, IMO, would be to have the PHP script create the email file, and notify the human.
Then you can have PHP run a shell script which uses the "at" program to schedule a task to happen in 20 minutes. At is a cousin of cron, but is better suited for this job.
That scheduled task will be to take the e-mail message, move it some place else (like a "done" directory), and pipe it through your mailer. tip: /usr/sbin/sendmail -t < myEmailFile will work on most Linux boxen.

Method Post in a Cronjob

i need a advice, basically i have a multistep wizard form, and in this wizard, for marketing purposes i need to save in the first step (name, email, address) to the database, case the user dont finish all the wizard steps.
But i need to create a email notification to the administrator case the user doesnt finish the form. So what i think was for example in cpanel create a cronjob that verifies in my database for records that doesnt have completed the multistep and than send a email to the administrator with there emails.
But there is a issue, i need to update a column in my records, in my case a column called "verified" that is boolean and when the mail is sent but to true so that every time the cronjob fires up dont send always emails to the administrator.
But it looks like that cronjob only supports Get method, does anybody have a advice?
Request methods (GET, POST, PUT, DELETE, etc.) are HTTP-specific, not CRON jobs.
But it looks like that cronjob only supports GET method
This is wrong. Cron jobs are to call specific file(s) on a specified time interval. You can read it here: Cron Jobs
In your case, you need to following things:
When the form is partially completed, you are saving the data in the
database. So, you can save using 2 columns in the database, 1 is to check the users who have not completed the form and 2nd is to check if the email is already sent to them.
Suppose 2 columns are is_incomplete (to check users who have not
completed the form) and is_followup_email_sent (to check if the email
is sent for further follow ups).
In your CRON file path, you don't need to specify any GET param until
its needed. You just need to call a file placed on your server and in
that file, you need to connect to the database to get users where
is_incomplete = 1 (form not completed) and then make another
function to send email to this list of users and set
is_followup_email_sent = 1 (that means email is already sent)
Whenever your CRON file is called, you just need to make a check at
the top that you get all those users where is_incomplete = 1 and
is_followup_email_sent = 0 i.e. users who have not completed their
form and admin email is not sent to them yet.
I hope this will help you! Cheers.
Request method (Get, post, put,...) is http-specific. When you run your code in the terminal (cronjob) you do not have a request method.
You should create a script (or command depending if you use a framework) that checks everything and changes the value in the database.

how to implement an email to post system in php

I would like to know how to setup a system in php that I could send emails to and the email content would poblish on my blog as a new post and the email subject would be the new post title.
Am not using word press or any of those blogging platforms am running my own blogging script and a php 5.x version.
I don't even know how to get started on this kind of programming challenge please any one with any ideas is welcomed.
Joe
You would need to create a script that is able to read from your email account. Php has the IMAP library that enables you to read email messages. Check this link to see how you could use the library.
You would need to schedule the script to be run at a periodic interval of time (eg. every 5 minutes). In Linux you would setup a cron job to do it.
So, I mean the script should be invoked periodically. If it finds a new message, it would then update your blog by taking the body of the email.
you need to set up a database that has all the tables and rows necessary to hold all the vital information about the post that people will send via email. then using MYSQL in your PHP file you need to connect to the database (host, username, and password) and then create your own query to enter the desired data to the database.

Sending a form to a php script, that the user never knows or sees

On my site, I have a feature that allows you to follow the updates of other users. When a user makes a change to their info, anyone following him or her will be emailed of this change.
I have it set up so that the php changes the info in the database, then searches through the users contacts to see who is following him/her, and sends emails to those who are following notifying them of the change.
The problem I have run into since adding this notification feature, is that insead of the page loading (form posts to itself) and showing the change almost instantly, (depending on how many people are following a particular user) it can take a few munites for the page to load and show the update (because the php is sending all the emails before the page reloads).
How can I set it up, where the script to send the emails, is run somewhere in the background, and the user does not have to wait for the emails to send before the page reloads, and could possibly even exit out of the website and still have the emails send if still the script is still running?
P.S. All my programming and development skills have been self taught, so I don't know a lot of terminology..... You may have to dumb down your responses so that I will understand what you are talking about. Sorry for the inconvenience, and thank you very much for any help.
I'm surprised it takes several minutes just to send some emails, but anyway you can do this in at least four ways:
Use ajax to send the form data to a separate script that sends the emails while the form posts normally
Have the form script fork (pcntl required)
Make an asynchronous request to your own page via php (either set a low timeout with cURL, or open a socket)
Use exec('script-that-sends-emails args >> some-other-file 2>&1 &');

Multiple PHP processes

I have a page where users enter their email address, click "Send", and the webserver will email them a ~10mb attachment. Right now, I have the page just display "Sending..." and the user waits about 20 seconds on this page.
Instead, I want to have it say "Your attachment will be emailed in a few minutes," sending that process somewhere else and allowing the user to continue browsing without having to open up a new tab.
I really just need someone to point me in the right direction because I don't even know what to Google at this point.
You could call another php file that will process the email sending and make sure to put in this call:
ignore_user_abort(true);
What this does is allows the php process to finish, even though the browser leaves. So you could initiate the process via ajax and then go to another page saying your attachment has been sent.
For more info:
http://www.php.net/manual/en/function.ignore-user-abort.php
I recommend checking out this question I posted a while ago.
How can I run a PHP script in the background after a form is submitted?
The answer explains how you can run a PHP script in the background after a form is submitted. This, of course, is just one way to accomplish this. Another would be to build a list of addresses and set up a cron to run a script on a timed interval. Both can accomplish the same thing, it just depends on how you wish to tackle the issue and what you can do on your server.

Categories