I want to send an automatic email every day at 10:00

I tried this code

$timeN = date('H:i');
$timeS = date('10:00');
if ($timeS == $timeN) {
    $to = get_option('admin_email');
    $nfrom = "my site";
    $subject = "test";
    $body = " Hello";
    $headers = 'From: "'. $nfrom .'" <' . $to . '>';
    wp_mail=$mail($to, $subject, $body, $headers);

It works if I refresh the page. If the page has been refreshed or opened 10 times it is sends 10 messages. How can i fix it?

I am sorry, I'm not a programmer and do not speak English well.


Creating a Scheduled Event

First we’ll create a function that will schedule our event. Mine is called “mycronjob” and it will run once every day. All this code can go into your plugin’s main file, outside the main function:

// create a scheduled event (if it does not exist already)
function cronstarter_activation() {
    if( !wp_next_scheduled( 'mycronjob' ) ) {  
       wp_schedule_event( time(), 'daily', 'mycronjob' );  
// and make sure it's called whenever WordPress loads
add_action('wp', 'cronstarter_activation');

Adding your Repeat Function

this is just a placeholder for your own code you’d like to run on a recurring basis:

// here's the function we'd like to call with our cron job
function my_repeat_function() {

    // do here what needs to be done automatically as per your schedule
    // in this example we're sending an email

    // components for our email
    $recepients = 'you@example.com';
    $subject = 'Hello from your Cron Job';
    $message = 'This is a test mail sent by WordPress automatically as per your schedule.';

    // let's send it 
    mail($recepients, $subject, $message);

// hook that function onto our scheduled event:
add_action ('mycronjob', 'my_repeat_function'); 

After all that if you face some problem with specific time then you can check this answer Here

| improve this answer | |
  • Where is the specific time at which it will run everyday? – user2238 Jul 4 '18 at 11:45

You should insert the validator, like this, so it will send only 1 mail in a day:

$timeN = date('H:i');
$timeS = date('10:00');
if ($timeS == $timeN) 
    if (get_option('last_mail_sent_time')!= date('d'))
     //your codes here

     update_option('last_mail_sent_time', date('d'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.