简体   繁体   中英

how to send automatically email

I programming one application that check records depending on creation date, what i need from application to check the post date automatically, if is it more than or equal 2 days it will send notification email without any action from users or admin.

I know about crontab solution but i know it is working on specified time. how i can do my needs?

MySQL posts table

id  |   post    |   date
1   |   post 1  |   1394094854
2   |   post 2  |   1394094754
3   |   post 3  |   1394094654
4   |   post 4  |   1394094554

my script.php

<?php
define('DBHOST', 'localhost');
define('DBNAME', '-');
define('DBUSER', '-');
define('DBPASS', '-');
/*
/ $db ====> mysql connection
*/

$getPosts = $db->query("SELECT * FROM posts");
foreach($gp as $getPosts){
    if(time() - $gp['date'] >= 172800){ // morethan or equal 2 days
        // send notification email 
        mail('TO_EMAIL', 'SUBJECT', 'MESSAGE', 'HEADERS');
    }
}

please help me in this issue. best regards Sami Mansour

您需要创建一个cron并调用发送电子邮件的脚本页面。

You need to create a cronjob:

0 1 * * * /path/to/php/file/script.php

This wil run the script.php file every day at 1:00 AM.

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        1          0             *                *            /usr/bin/find

You need to create a cronjobs and can simplify the script using mysql functions

$getPosts = $db->query("SELECT * FROM posts WHERE DATEDIFF(NOW(), FROM_UNIXTIME('date')) >= 2");

So you fatch data that are posted more or equel than 2 days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM