phplinuxemailsql-server-job

Linux server: Send mail to my users


I am fairly new to creating server scripts and jobs that the server runs every day.

My problem is as follow:

I want to send an email to my users reminding them of a specific job they have to do.

My idea:

Database -> collect all users who needs to be notified and insert them into a table notify_user

Script -> find all users and send them a mail

Script -> Delete all from the table

This script will then run at a specific time every day for instance every 24th hour.

As I stated earlier I am not really keen on how to setup such a script.

My server is an Ubuntu server and my application is a PHP program.

Does anyone know how I might achieve this or know where I might find some documentation on this subject since I have been unable to find anything that solves this issue.


Solution

  • If given that you know how to populate the 'notify_user' table then these are my steps to reproduce a solution sample for you. I did this on my VPS server with sendmail daemon running.

    # mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 167
    Server version: 5.5.40-0ubuntu0.14.04.1 (Ubuntu)
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql>  create database stack_mail_db;
    Query OK, 1 row affec`enter code here`ted (0.05 sec)
    mysql> grant all privileges on stack_mail_db.* to 'stack_mail_usr'@'localhost' identified by 'stack_mail_pass';
    Query OK, 0 rows affected (0.11 sec)
    mysql> use stack_mail_db;
    Database changed
    mysql> create table notify_user( id int not null auto_increment primary key, user_name tinytext, user_email tinytext );
    Query OK, 0 rows affected (0.28 sec)
    

    After creating this sample database we should populate it with at least 2 users (for testing) with working emails. I changed here the actual emails I used.

    mysql> insert notify_user (user_name, user_email) values ('test1', 'test1@test.com');
    Query OK, 1 row affected (0.18 sec)
    
    mysql> insert notify_user (user_name, user_email) values ('test2', 'test2@test.net');
    Query OK, 1 row affected (0.03 sec)
    

    Now we should write a script that gets these details and sends emails:

    # vim cron_email.php
    <?php
    $host = 'localhost';
    $user = 'stack_mail_usr';
    $pass = 'stack_mail_pass';
    $dbname = 'stack_mail_db';
    
    $conn = new mysqli($host, $user, $pass, $dbname);
    
    if ($conn->connect_error) {
            trigger_error('DB connection failed: ' . $conn->connect_error, E_USER_ERROR);
    }
    
    $query = 'select * from notify_user';
    
    $res = $conn->query($query);
    
    if ($res === false) {
            trigger_error('Failed query: ' . $query . ' Error: ' . $conn->error, E_USER_ERROR);
    }
    
    $headers = 'From: admin@example.com' . "\r\n" .
            'Reply-To: admin@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
    $res->data_seek(0);
    while ($row = $res->fetch_assoc()) {
            $to = $row['user_email'];
            $subject = 'Notification for ' . $row['user_name'];
            $message = 'Hello ' . $row['user_name'];
            $mail = mail($to, $subject, $message, $headers);
            if ($mail) {
                    $conn->query('delete from notify_user where id=' . $row['id']);
            } else {
                    echo "Email failed\n";
            }
    }
    

    Now it's time to put this script on cron:

    # crontab -e
    0 0 * * * php -f /path/to/cron_email.php
    

    This will run your script exactly every midnight. If you want to set a more specific hour, look at this tutorial: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

    Hope this helps ^)