PHP send email script runned by cron jobs

Discussion in 'PHP' started by IonCatalin, Jan 24, 2010.

  1. #1
    I want to send email at a certain time with PHP, I setup a CronJob to run a script every 5 minutes but is something wrong with it. It sends a mail every 5 minutes or don't send at all.

    This is the script:

    <?php

    require("config.inc.php");
    require("class/Database.class.php");
    $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);

    $now = time();
    $db->connect();
    $sql = "SELECT * FROM remindere";

    $rows = $db->fetch_all_array($sql);

    // print out array later on when we need the info on the page
    foreach($rows as $r){
    if($now => $r['time']) {

    $to = $r['email'];
    $subject = 'Reminder';
    $message = $r['titlu'];
    $headers = 'From: RemindeMe@gmail.com' . "\r\n" .
    'Reply-To: fo.translator@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
    deleteReminder($r['id']);
    } else {}
    }

    $db->close();


    In my db i have this fields:
    ID: ..
    MAIL: the email to send the mail
    TIME: a php timestamp with the time when to send the email
    TITLU: the message to send in the email

    the deleteReminder() functions is supossed to delete the reminder if that was send.

    I think the problem is my condition from the if statement if($now => $r['time']).

    Thank you and sory for my English.
     
    IonCatalin, Jan 24, 2010 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    How many records are in the database? perhaps the script times out?
    Make a limit to run through X amount of rows.
     
    Sky AK47, Jan 24, 2010 IP
  3. IonCatalin

    IonCatalin Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have just 4 rows in the database ... to test it.
     
    IonCatalin, Jan 24, 2010 IP
  4. codebreaker

    codebreaker Well-Known Member

    Messages:
    281
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #4
    For the beggining :
    Have you tested the script to see if it works as it should ? (By running it ) .
    You first need to determine what's your problem, the script or the cron .
    Now i've seen this piece of code :
    
    if($now => $r['time'])
    
    Code (markup):
    this should be :
    
    if($now >= $r['time'])
    
    Code (markup):
    You should take a look at php operators.
     
    codebreaker, Jan 24, 2010 IP
  5. IonCatalin

    IonCatalin Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Cron is working how it should. The problem is my script.
    I tested it but it doesn't work ... any ideas?
     
    IonCatalin, Jan 24, 2010 IP
  6. thuankkk

    thuankkk Active Member

    Messages:
    503
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    95
    #6
    $sql = "SELECT * FROM remindere";
    What is the table name? I think it's `reminder`, right?

    and this line:
    $message = $r['titlu'];
    it's 'title'??
     
    thuankkk, Jan 25, 2010 IP
  7. IonCatalin

    IonCatalin Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes .. titlu = title and remindere = reminders. They are in my language .. Romain
     
    IonCatalin, Jan 25, 2010 IP