How to stop cron job email notification - php

I have hostgator hosting. I want to use Cron Job. I am using this command to perform my task
/opt/php54/bin/php /home/abskillz/public_html/cron.php
Now the question is every time when cron is perform I got email that your action has processed or etc. Can anyone tell me that how can I stop getting this email?

I just remove the output message from my PHP page. So no more emails now :)

You can always set some persistent flag to indicate if sending email is required.
if (file_exist(__DIR__.'/cron_flag_'.self::SEND_EMAILS.'.php')) {
mail('a#b.com', 'subject', 'message');
}
if ($this->db->queryScalar('SELECT value FROM cron_flags WHERE type = "'.self::SEND_EMAILS.'"')) {
mail('a#b.com', 'subject', 'message');
}
So if flag is not set, CronJob just will just jump to the end of file and no processing will be done.

Related

send email daily from wordpress site

I customized a calendar plugin which shows today's birthdays and current months list of wedding anniversaries in the home page of the site. i wrote a code in that plguin's displaying page using wp_mail and mail will send. but this happens only when the site is visited. my code:
if($dat==date('Y-m-d'))/*$dat is the date of event from DB*/
{
if($eid!=''){ /*if recipient email id is not null*/
if($se!=1) /*if email is sending first time then($se=db column 'send'value) $se=0 otherwise it is 1*/
{
$to=$eid;
$sub="Birthday Wishes";
$msg='Happy Birthday '.$ev_title[$j];
$headers= 'From:Mysite <noreply#mysite.com>' . "\r\n".'Content-type: text/html';
$attachments=array(WP_CONTENT_DIR . '/plugins/spider-event-calendar/images/happybday.gif');
$rx=wp_mail($to,$sub,$msg,$headers,$attachments);
$wpdb->update($wpdb->prefix. "spidercalendar_event",array('send'=>1),array('id'=>$ev_id[$j]));/**/
//echo "email send";
}
else{
//echo "email already sent";
}
}
}
i heard about wp_cron but when i searched in this forum about how to write cron in wordpress i saw an answer like
Unfortunately the WordPress cron jobs are only triggered when your site is visited
if it is true then how can i send emails daily even without visiting the webpage.is there any other way for this?
You can set up a regular cron job using your terminal via the following process after logging in;
access your con jobs list using;
crontab -e
There will likely be some lines in there already. Type o to enable editing of the file;
You can use the following website to generate the command for it to issue http://www.corntab.com/pages/crontab-gui.
as an example job I've got on my server;
*/5 * * * * /usr/bin/php -q /var/www/www.mysite.com/cron.php >/dev/null 2>&1
This job runs a php script every 5 minutes and prevents the site admin from receiving an email every time it runs. That's what 2>&1 does at the end of the line.
I'm actually a little unsure what /usr/bin/php -q and >/dev/null do, as I just copied them from another job that was already set up in that way. I'm fairly new to this via command line.
Once you're done editing the job, press esc and then Shift + zz to exit out of it. And you should be done.
I'd recommend using a test script that emails yourself, to make sure it works correctly first, then change it to use the script you're trying to run once you know it works.
I don't know if this process would be different for different server set ups, but it might be. This is just what I have to do for my server which runs CentOS with apache.

PHP Continue sending email but stop page loading?

My page executes a script that takes a relatively long time to complete. I would like to make it so that the user can submit information, immediately echo "Complete", and allow the user to exit the page while the script continues executing. How can I do this?
Use cron. On your page create a email task and save in in db or fs - does not matter. Create a script which runs every n minutes which gets email tasks and executes them.
Unfortunately, your hosting may not have cron support...
Email is naturally slow. I would advise you to use a job queue for your emails. You should look at
Gearman
Beanstalkd
ZeromQ
With these solutions, you can queue slow tasks and continue to show valid information and progress to your user.
Example Client
$client= new GearmanClient();
$client->addServer();
$client->do("email", json_encode(array("A#yahoo.com","Hello World","This is your first Mail")));
echo "Welcome To XYZ" ;
Server
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("email", "sendMail");
while ( $worker->work() );
function sendMail($mail) {
list($to, $subject, $message) = json_decode($mail);
return mail($to, $subject, $message);
}
My suggestion would be to submit all of the information into a table row or similar data structure, then run a cronjob every few minutes to go through each row and run the script based on the information that had been submitted.
This would be slightly complicated, I'm afraid, but it would immediately free the user (once the raw information was stored into the DB)

Stopping a rogue PHP script sending E-Mail

Please don't laugh at me but I believe that I just did something extremely stupid. I was working on setting up a newsletter for a site that I am working on, but I tried it out at first when there was a typo. While scanning throughout the database and sending emails, I screwed up on the part that makes it stop. I fixed the code, but the emails are still being send (to my mom :O) and they don't seem to be stoping.
This is the script when I executed it:
$message = $_POST['emailmessage'];
$subject = $_POST['subject'];
$query = mysql_query("SELECT `email` FROM `members` WHERE `active`='1'");
//This line underneath should not be there
$rows = mysql_fetch_assoc($query);
$headers = array(
"From: contact#thestopitcampaign.com",
"Content-Type: text/html"
);
//should be '$rows = mysql_fetch_assoc($query)' instead of '$rows'
while($rows)
{
mail($rows['email'],$subject,$message,implode("\r\n",$headers));
echo "<p>Sent to: " . $rows['email'] . "</p>";
}
I contacted FatCow to see if they could stop the script, but they said that they could not do that and they would have to delete my entire account and put me on a different server. I cannot do that. Is there anyway to generate an error or something that would make the rogue script stop? FYI I do not have SSH access.
--I looked in my php config file and the timeout for a script is 300 seconds. That seems like a lot of emails to send. Is there anyway to stop those emails?
What has been sent can't be stopped any more. But it won't run forever and probably has already stopped.
If the server is not grossly misconfigured by the provider, your script didn't run any longer than a certain time limit, e.g. 60 seconds. Even though messages continue to come through, it's probably no longer running, but the mail server is taking its time to handle all the messages that it created.
I would wait and see - the flood is likely to end soon.
What the provider says about moving the account to a different server doesn't seem to make any sense at all - if there is a rogue process that is sending E-Mail, they should be able to kill it easily. But anyway... I would wait.
My guess is that the emails are just queued up and should eventually stop as php script wont keep executing itself after the page has stopped loading.
I'd suggest you to use a newsletter provider like mailchimp as they are more relyable, are safe and the service is easy to integrate in your website.
Hope that helps!

How can I have an array of emails and make sure they all send?

I would like to be able to get an array of emails and make sure each email is sent. (e.g. Array > send each email > result) I kind of changed the question here because this is more important, plus I have added a 50 rep. point. Codewise how can I do this?
Apart from still using the mail() function, you probably want to setup a cron job for sending out the mails. For spooling mail send jobs use a separate database table. Or if it's about some sort of mailing list functionality, then a simple recipient list will do.
If you just want to send out a bunch of the same email at once, you
could call implode() on your array of emails to turn it into a string:
$to_string = implode(', ', $to_array);
Or, if you want to try something more complicated, you could use a
foreach loop to cycle through each email and to keep track of
successes and failures:
$success = array();
$failure = array();
foreach ($to_array as $to_email)
{
if (mail($to_email, $subject, $message, ...))
$success[] = $to_email;
else
$failure[] = $to_email;
}
I'm guessing your original question had to do with sending out all these
emails every day or something without necessitating you hitting a
button. If you have ssh access, see what happens if you type:
crontab -e
If you get some sort of error, you will have to speak with your system
administrator about cron. If you get a file, then you can use cron. This
is not a part of your current question, though, so I'll leave it.
The same way. You must have code that sends an email to an email address. Whether they are on the site or not, it is the same code. You just need to know their email address.
EDIT: If you are wondering how you would trigger the email to be sent, maybe you want to schedule it using a cron job, for example send an email every day at midnight.
This says it all, really: http://php.net/manual/en/function.mail.php
You just need an outgoing mail server installed (postfix, exim, sendmail)
Easy way to send an email:
$to = "usermail#test.com";
$from = "my_email#mydomain.com";
$subject = "Hello!";
$contents = "This is an test mail!";
mail($to, $subject, $contents, "From: $from");
If you don't have access to cron jobs then you will probably struggle to run without user interaction.
A common method for dealing with this is to run on every nth page load, or every so-often. This only works if you have a site that's visited about as often as you want to send email. You'll also want to use an ACID-compliant database. Pseudo-code follows.
if (1 == rand(1,100)) { // run once every 100 page loads
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Alternately, you can run it on a timer:
if (time() - get_last_time_run() > $run_at_least_once_every_this_many_seconds) {
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Or you can combine both with a &&. This depends on how often your page gets hit.
If you want to send email more often than you get page hits... too bad ;). Or have an external service call the page every so often.

breaking loop process into parts by delaying them, need advice

i wanted to break down a process (loop) into some parts, for example if have 128 emails to send :
function subs_emails(){
$subscribers = //find subscribers
if(!empty($subscribers )){
foreach($subscribers as $i => $subscriber){
sendEmail($subscriber->id);
if($i % 15 == 0){ //<-- send email per 15
sleep(60); //to pause the process for 60 seconds
}
}
return true;
}else{
return false;
}
}
will this works ?? or is there any other "better approach" solution ?? need advice please
thanks
The usual approach would be to send only a few emails at once and mark the sent ones on the background(via database flag sent=1 for example)
Then call the script every few minutes via a cronjob
This way you dont run into problems with php timeouts when sending emails to a lot of subscribers
sleep() will cause the script to stall for the defined number of seconds (60 in your example). It won't really be breaking the loop but instead merely delaying it.
A possible solution is to make a note of which subscriber has already been sent an email. Then you can have your script execute at regular intervals via cron and only load a small amount of those who have not yet been sent an email. For example:
Script executes every 10mins
Load 15 subscribers who have not been flagged as already notified
Loop through all 15 loaded subscribers and send each an email
Set flag on all 15 to say they have been sent the email
Script will then run 10mins later to process the next 15 subscribers

Categories