Home > OS >  PHP : How to send e-mail to the user using cron job
PHP : How to send e-mail to the user using cron job

Time:11-09

I am new in programming so I really need a guide for my project. I need to send an e-mail as a reminder to the user either few days before or on the same day as the user inputted in their reminder. I know I will need a cron job in order to sent the e-mail automatically but I want to know, if my coding is correct and what do i need to add in order to let the cron job know how to send the e-mail. I am lacking so much so I appreciate any help.

<?php
session_start();
error_reporting(0);
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");

if(mysqli_connect_errno()){
    echo "Unable to connect".mysqli_connect_error();
}

$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");

if(mysqli_connect_errno()){
    echo "Unable to connect".mysqli_connect_error();
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$current_date = date("Y-m-d");

$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");

$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");

if(isset($res))
{
    $mail = new PHPMailer(true); //create instance of phpmailer

    $mail -> isSMTP();
    $mail -> Host = 'smtp.gmail.com';
    $mail -> SMTPAuth = true;
    $mail -> Username = '[email protected]';
    $mail -> Password = '*mypassword*';
    $mail -> SMTPSecure = 'tls';
    $mail -> Port = 587;

    $mail -> setFrom('[email protected]');
    
     $mail -> addAddress($email);   
    

    $mail -> isHTML(true);

    $mail -> Subject = "Reminder!";
    $mail -> Body = "test";

    $mail -> Send();

}

Update: I corrected the code and tried to run and i got this error message. p/s : i already configure the Gmail and App password.

Fatal error: Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, mysqli_result given in C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php:1080 Stack trace: #0 C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php(1080): trim(Object(mysqli_result)) #1 C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php(1014): PHPMailer\PHPMailer\PHPMailer->addOrEnqueueAnAddress('to', Object(mysqli_result), '') #2 C:\xampp\htdocs\belajar\send_email.php(37): PHPMailer\PHPMailer\PHPMailer->addAddress(Object(mysqli_result)) #3 {main} thrown in C:\xampp\htdocs\belajar\phpmailer\src\PHPMailer.php on line 1080

<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");

if(mysqli_connect_errno()){
    echo "Unable to connect".mysqli_connect_error();
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$current_date = date("Y-m-d");

$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");

$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");

if(isset($res))
{
    $mail = new PHPMailer(true); //create instance of phpmailer

    $mail -> isSMTP();
    $mail -> Host = 'smtp.gmail.com';
    $mail -> SMTPAuth = true;
    $mail -> Username = '[email protected]';
    $mail -> Password = '*mypassword*';
    $mail -> SMTPSecure = 'tls';
    $mail -> Port = 587;

    $mail -> setFrom('[email protected]');
    
    $mail -> addAddress($email);   
    

    $mail -> isHTML(true);

    $mail -> Subject = "Reminder!";
    $mail -> Body = "test";

    $mail -> Send();

}

CodePudding user response:

This line returns a mysqli_result and not the email.

$email = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");

You need to fetch the result and get the value like this:

$result = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
$array = mysqli_fetch_assoc($result);
$email = $array['email'];

Furthermore I don't know why you use two querys that do almost the same. Remove this line:

$res = mysqli_query($con, "SELECT * FROM reminder WHERE reminder_date = '$current_date'");

And replace:

if(isset($res))

with:

if(isset($email) && $email)

UPDATE

If you want to send the notification to multiple recipients you need to get ALL email addresses and add them in a foreach like this:

<?php
session_start();
$con = mysqli_connect("localhost", "root", "", "smart_expenses_management");

if(mysqli_connect_errno()){
    echo "Unable to connect".mysqli_connect_error();
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$current_date = date("Y-m-d");

$emails = array();
$result = mysqli_query($con, "SELECT email FROM reminder WHERE reminder_date = '$current_date'");
while ($row = mysqli_fetch_assoc($result)) {
    $emails[] = $row['email'];
}

if(count($emails) > 0)
{
    $mail = new PHPMailer(true); //create instance of phpmailer

    $mail -> isSMTP();
    $mail -> Host = 'smtp.gmail.com';
    $mail -> SMTPAuth = true;
    $mail -> Username = '[email protected]';
    $mail -> Password = '*mypassword*';
    $mail -> SMTPSecure = 'tls';
    $mail -> Port = 587;

    $mail -> setFrom('[email protected]');
    
    foreach($emails as $email) {
        $mail -> addAddress($email);
    }

    $mail -> isHTML(true);

    $mail -> Subject = "Reminder!";
    $mail -> Body = "test";

    $mail -> Send();

}
  • Related