php send mail using cron jobs - php

I have two file 'mail.php' and 'cron.php' . 'mail.php' receives email address from a source continuously . 'cron.php' will send email to those address at the next hour of receiving hour .If email address is received at 5.45 email will be sent at 6.00 .
My idea is to run cron.php every hour which will get email addresses and their receiving time form session variable.
But how can I synchronise this two file ?? Is it possible to access session variable simultaneously by this two file ?? that means mail.php will write data and after sending mail cron.php will delete those data from session.
Thanks in advance.

You'd have to post some code up. I dont really understand what you mean. The first part of your weustion leads me too believe that you have something setup already, whereas the latter half doesn't.
Basically what you need to do, is have mail.php (the one that gets the email addresses from wherever there coming) and store those somewhere (database or plain text file maybe). Then every hour, run the cron, grab the emails, do whatever you need to do then either elete thos eemails, archive them in another place if you need to keep them, or if your using a database you could use an "email_sent" column and set it to 1 once the emails have been sent.

Answer about session and CRON here.
can php cron jobs access session variables/cookies?
If I were you I'll save the email addresses in database or a text file.
And then delete them after executed.

Can't you just make a function for sending emails (turn mail.php into a function?) and call it with cron.php?

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.

Run php file without wait for results

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)

PHP: How to output validation status of register script without waiting for an email to be sent?

I have a registration form on my site. The form is sent to the server via AJAX and is then validated. Then it will output an address for the browser to be redirected to, if it was validated as valid information. PHP will then send a validation email to the email specified. The problem is that the sending of the email takes 10 seconds, and I don't want my users to wait for 10 seconds from they press Register to they get redirected...
Is there a way for PHP to tell the client the information was correctly validated and output the redirection URL, and continue sending the email without the client waiting?
It's called a background job.
To do this in the simplest way (note: not the best, but the simplest):
I'm assuming you're storing your registration details in some form of database. Add an extra column to flag that you need to send the validation email to this user.
In another script, check the database for any rows with this flag set, and send the email from there.
This second script will be triggered by a cronjob or similar, on a schedule of your choosing.
This way, users don't need to wait for that 10 seconds.
There are more efficient solutions that will cope better for larger scale sites, but I'm guessing that you don't yet need to know this, and that if I brought them into the equation it'd confuse matters. Look into Job Queues, if you want to know more.

How can I send a daily email notification to users in PHP?

I have a simple user signup form with a checkbox that lets users get a daily email notification if there was activity on any of their projects...much like Stack Overflow has a "Notify xxx#example.com daily of any new answers".
My current thinking to implement this in a LAMP environment is as follows:
In the user database, set a boolean value if the user wishes to receive a daily email.
If there is any project activity, the project gets updated with the current timestamp.
Each night (midnight), a PHP file is executed (likely through a cron job) that scans through the project database to identify which projects had activity that day. For projects with activity, the project owner name is selected and the user table is scanned to check if the user wishes to receive a daily email notification. If yes, add to a recipient list; else, ignore.
Questions / concerns I have that would appreciate some guidance on before I start to implement:
I'm in a shared hosting environment. What precautions do I need to take from being misidentified as spam either by the hosting company or the receiving mail servers?
Do I need to "chunk" out the recipient list (50 emails at a time) and email each group? Is this as simple as putting a sleep(30); between each call to mail()?
I'm using the CodeIgniter framework and will have the cron job call the appropriate function in a controller to run this at midnight. How do I limit calls from only the cron job to prevent some unauthorized user from calling this function from the browser?
Thanks.
If you do change the "From" header in php, make sure you change it to the domain that's hosted on that server. It looks suspicious when mail #a.com is being sent by b.com's servers.
I would send the emails individually foreach ($Users as $User)..., since this allows you to personalize the email contents. Even if you don't need to personalize emails now, you might want to later, and the support for it will already be there when you need it.
First, I would store the script outside of the web root. I'm not sure if CodeIgniter will let you do this, but there is no need for the script to ever be served by Apache. Cron doesn't care where the script is stored. Additionally, I've checked the time when the script is executed. If it's not midnight, then don't blast out the emails. Also, you could keep a log around and also check to make sure the emails haven't already been sent that day before sending.
1) Start with an SPF record and a DKIM if you can that lets mail servers know to expect email from your servers
2) First, you need to put the recipients in the BCC field so that it not each user has the email addresses of 49 other users on your system. One step further is to do each email separately, putting only the recipient in the TO field. This approach also allows your to tailor each email to the user (perhaps putting in "Hi [First name]".
3) Have the cron job something like this
wget http://localhost/send-emails
Then in your script, check $_SERVER and make sure you only allow requests from 127.0.0.1
About the third question: You can either use an .htaccess file to prevent access to that specific page or you can call your script in cron with a command line parameter and check for that variable in $argv.
1) The SPF record is the most important thing. Use the email from the domain so whatever#whatever.com, where whatever.com has the SPF records set correctly.
2) It's always good to throttle email, especially when first starting out. You should check your shared servers policies, which are normally 200-500/hour. Calculate how many seconds that comes to. For example 300/hour is 1 every 12 seconds. After a few weeks of sending good emails, you should be ok to send larger amounts.
3) You can have the cron file outside the webroot or limit access via .htaccess or another method.

Email pipe to php script working only sometimes

I have a php pipe script that receives an email, takes an attached *.csv from it and parses it.
However, when the email is sent from where it is supposed to be coming from, it silently errors. But when I take that same email and resend it from my address it goes through just fine. is there any simple reason it could be doing this?
try and save some information about the email in a logfile before anything else, then some more info as you parse it. This way you get a more better understanding of what is happening
Is the email youa re sending from a real email? There may be a setting that allows emails to be sent from a valid user.
What happens if you chown the script to be the user you want to send the email from. Does it work then?

Categories