I am trying to send email using PHP mailer at regular interval. I am using windows 10 and have set up a cron . Here is .bat file code .
#echo off
schtasks /Create /TN XAMPP /TR "C:/xampp/php/php-win.exe C:/xampp/htdocs/aurr/emailcron.php" /SC MINUTE /MO 1
pause
Problem is even though I am able to send emails ,there is no attachment in received emails .But when i manually execute emailcron.php ,I get attachment in received email.My guess is this might be because of permission issue .How do I fix this error?
When the script is run by the cron task it's run by whichever user the cron script is run as (a default if you don't override it), and when you run it yourself it's obviously run as you. If you have permission to read the file but the cron user does not, it won't be able to send the file.
Several things to look at here:
Run the cron task as a user that does have permission to read the
files, e.g. you.
Give the cron user permission to read the files.
Make your script check for a successful return value when it adds
attachments.
For the last of these, do it like this:
if (!$mail->addAttachment('path\to\file')) {
echo "Could not access path\to\file";
exit;
}
That way it will generate an error and not send the message if it can't access the file, rather than failing silently as it's doing at present.
Related
Right now my cron job is running but it creates a new log file every time it runs, which is both ridiculous and unnecessary. What I want is a NO log file at all (I will never check them, and I don't want to have to go in and clean them up). If I set it to send an email it just sends an email saying it ran ok and the output of the script is contained in the log file.
BUT - I need to see the output of the PHP script it's executing as it contains success / fail information and in the future probably a list. If I have to I can just make the PHP script send an email instead of the CRON job, but can CRON send an email with the output of the script, yet create no log file?
With this command I've disabled emails but it still creates a new log file with the script output every time it runs:
wget *webaddress*/generate_late_fees.php >/dev/null 2>&1
In shared hosting you usually don't have any control over the syslog and cron daemon anyways, so there's no chance to turn off logging for that cronjob.
Just do the email stuff from within the script and don't rely on the crond's automatic output to email forwarding.
Another option is to do the email stuff from the console using the mail command. Just pipe wget's output to the mail command.
wget *webaddress*/generate_late_fees.php | mail -s "this is the subject" "to#address" >/dev/null 2>&1
I dont know alot about cron job but i have a php file which sending data with attachment when i run it using the browser it's working fine and attachment is sent ,but if i use cron job to run it ,it never goes with the attachment.
here is the cron job command-line :
/opt/php54/bin/php /home/username/public_html/path/to/myscript.php
and here is the place of error in the php file ,as the cron job sending me email with "Some how file :hhshd.jpg is not exist!" , however the data with attachment is sent successfully when i use the browser.
if (!file_exists("uploads/" . $fileName))
{
die("Some how file :$fileName is not exist!");
}
Note: i am using the cronjob of Hostgator.
Problem solved i have used this cron job command:
curl http://yoursite/path/to/publish.php
One possible reason cold be when you run the script through browser, the Apache user is able to create the attachment file and send it.
But when cronjob is not able to create attachment file due to permission issue.
You can try using 'sudo' in your cronjob or try changing the permission of the directory where you are creating the attachment.
It is most likely to be an ownership & permissions problem. What you have to put in your cron config depends on where you've put it and what user your web server runs as. If you want it to run at 5am each day, it's in /etc/crontab and www-data is your web server user, you would need an entry like this:
0 5 * * * www-data /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
If it's in the web server user's own crontab file, it would skip the user name field. You would edit it using crontab -u www-data -e, and add a line like this:
0 5 * * * /opt/php54/bin/php /home/username/public_html/path/to/myscript.php
One other issue may be that the current working directory may be different, so you may need to set the command to include a cd to the directory your script is in first, like this:
cd /home/username/public_html/path/to && /opt/php54/bin/php myscript.php
If you want to receive any error output from a cron job, set a MAILTO environment variable by adding a line like this to the cron file before you run the command:
MAILTO=user#example.com
Example:
* * * * * curl http://localhost/Android/SMS/p1.php
type above line to execute every second p1.php email sending file with attachment.
p1.php file is in my htdocs/Android/SMS folder.
I need to run my php code each hour, and my knowledge about crontab/cronjob is pretty bad.
I want the script to run each hour and send email for 2 case: success and failure.
I wrote this code but I don't know if it's good for crontab (I think it's not working):
0 * * * * php <full-path-script>
if [ “$?” = “0” ]; then
echo “Backup Process was Successful. A new log file <filename>.txt has been
created” | mail -s “Backup Status Successful” <email> -A <path>
<filename>.txt
else
echo “Backup Process Failed. Please contact System Administrator. A new log
file <filename>.txt has been created” | mail -s “Backup Status Failed”
<email> -A <path><filename>.txt
if you think I should do it different please explain and show me how.
BTW - I'm working with postfix.
OK, You can solve your issue with writing / running two PHP scripts together.
Existing script, just add writing to file, write it in CSV format the start time and the end time of the script. [ Each run in new line ]
Run another "deamon" PHP script every x minutes via crontab that parse the CSV file, the script goals are: Parsing the CSV file and searching for lines with start time and without end time, and sending an email if something wrong in CSV file.
Pay attention,last line in CSV file can be active process, so take interval or time time that you think its should done.
//Create another PHP file / class that sends an email and control it from the php code.
// the message
$msg = "First line of text\nSecond line of text";
// send email
mail("someone#example.com","My subject",$msg);
What can interrupt in PHP in your code ? use timeouts / try catch / reties mechanism to reduce the failures.
OK, here is what I've done to solve my problem:
First - Be sure to write all your shell scripts on Linux (otherwise you script will get messed up and you won't even be able to see it), you can setup your own Linux using vmware or other program.
This is my shell script:
`#!/bin/bash
/usr/bin/php <full-path-script>
if [ “$?” = “0” ]
then
echo “Backup Process was Successful. A new log file `date "+\%Y-\%m-\%d-
\%H"`.txt has been created” | mail -s “Backup Status Successful” <email-
address> -a <path>`date "+\%Y-\%m-\%d-\%H"`.txt
else
echo “Backup Process Failed. Please contact System Administrator” | mail -s
“Backup Status Failed” <email-address> -a <path>`date "+\%Y-\%m-\%d-\%H"`.txt
fi`
In order to make sure your script will send error into $? you should avoid using die(); on errors, In fact you should use exit(1); (it'll put 1 in $? so we'll know the script failed to run).
Don't forget to make cronjob that will task this script for you and send you notifications to your email address :)
I hope this help anyone!
have a php page called cronEmail in the web folder. It incudes the code to end an email to specific users on the website. I want it to open the page once a day and send the email. The page has only php and MySql code to read the recipients of the email.
I am trying to use Task scheduler in the control panel to run the page. I create a user defined script and in schedule I set the time to a certain time and to only run daily once a day.
In the rum command i have tried numerous ways to run it on the time but every time it just passes and does nothing. an example of what I put in for the script is
/web/cronEmail.php OR
chmod 755 /volume1/web/cronEmail.php
There are only two of a many can anyone point me in the right direction
Thanks a million
Seems like you want a cronjob, in the terminal open cron with:
crontab -e
then at the bottom of the file place this
0 4 * * * php /url/to/folder/cronEmail.php
Not sure if you found another solution, but this is what worked for me. If you put the following in the "Run Command" section, it should work:
php /volume1/web/cronEmail.php
You can also create an error log by doing the following:
php /volume1/web/cronEmail.php>> /volume1/web/errors.log 2>&1
If you don't use the php at the beginning, and open up the error.log file that is created, what you'll see is that the Task Scheduler doesn't seem to know that it's looking for PHP, and doesn't recognize the script.
I have some executable PHP code that I'm trying to implement using cron jobs. The page sends a couple emails. It works just fine when I type in the address into my URL bar and load the page (the url is akin to www.mysite.com/mypage.php) but for some reason it doesn't work when I do it as part of a cron job. I have double checked the permissions and the file is executable, so that's not the issue. I am getting an email confirming that the cron job was completed, but the emails that are supposed to be sent by the program do not come through. Here is my code in crontabs:
SHELL= /bin/bash/
HOME = /
MAILTO = "mymail#gmail.com"
* * * * * /usr/bin/php /usr/share/nginx/html/mypage.php
Any idea why this might not be working?
EDIT ABOUT PERMISSIONS: Possibly relevant: I am editing cron jobs by typing 'sudo crontab -e'. I was able to successfully set up another cron job that just emails me text. However, I tried setting up another text-only-email cron job NOT in sudo (ie typing 'crontab -e') and that did not work. I didn't receive any emails. I also got an error when I typed 'crontab -e' about it not being able to read .nano_history and permission being denied but I was able to bypass that by pressing Enter
Check to see that wget is installed on your server. it should be. Then use wget to call your program from the cron tab. This way you do not have to concern yourself with calling php from the command line.
wget http://999.999.99.9/hr/stats/send_stats.php?task=first