Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send mail weekly once WordPress backup taken or not?

Multiple sites having in WordPress. backup buddy plugin using configured amazon S3 bucket we need every week send mail recent backup received or not in all sites backup information.

like image 511
vadivel a Avatar asked Nov 20 '25 06:11

vadivel a


1 Answers

Finally I got the answer

First install the AWS S3 package refer this 'https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html' Then below mentioned code create index.php file then call in browser working good

When you want to automatically run index.php file use cron

require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$bucketData = '{
   "bucket":{
      "1":{
         "name":"bucketname-1",
         "backup":"backups/"
      },
      "2":{
         "name":"bucketname-2",
         "backup":"backups/"
      }   
   },
   "uncheck":[
      "menardsimages" 
   ]
}';
//multile-bucket-having-mentioned-except'uncheck'

$bucketData = json_decode($bucketData);
/**
 * List your Amazon S3 buckets.
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

$key = 'xxxxxxxxx';
$secret = 'yyyyyyyy';
//Create a S3Client
$s3Client = new S3Client([
    'region' => 'us-west-2',
    'version' => 'latest',
    'credentials' => [
        'key' => $key,
        'secret' => $secret,
    ]
]);

function recent_backup_verify($objects){
    $tendays_bf_today = date('Y-m-d',(strtotime ( '-7 day' , strtotime ( date("Y-m-d")) ) )); 
    foreach ($objects as $object) {
       $created_date = $object['LastModified']->format('Y-m-d');
       if($created_date > $tendays_bf_today){
           return $created_date;
       }
    }
    return false;
}

function searchForName($name, $array) {
   foreach ($array as $key => $val) {
       if ($val->name === $name) {
           return $val->backup;
       }
   }
   return null;
}

$buckets = $s3Client->listBuckets();
//print_r($created_bucketlist);
$backup_array = array();

foreach ($buckets['Buckets'] as $bucket) {
    //files-listing-insideofthe-bucket
    if(!in_array($bucket['Name'], $bucketData->uncheck)){
        $objects = $s3Client->getIterator('ListObjects', array(
            "Bucket" => $bucket['Name'],
            "Prefix" => searchForName($bucket['Name'], $bucketData->bucket) //must have the trailing forward slash "/"
        ));
        $getbackup_result = recent_backup_verify($objects);
        if($getbackup_result != false){
            $backup_array[$bucket['Name']]['status'] = 'Taken';
            $backup_array[$bucket['Name']]['date'] = $getbackup_result;
        }else{
            $backup_array[$bucket['Name']]['status'] = 'Not Taken';
            $backup_array[$bucket['Name']]['date'] = '-';
        }

    }
}


sendgrid_form($backup_array);
function sendgrid_form($backup_array){

    $msg = '<table border="1" cellpadding="5" cellspacing="0" width="500px" style="border-color:#ddd;">';
    $msg .= '<tr bgcolor="#555" style="color:#fff;">';
    $msg .= '<th>S.No</th><th>Site Name</th><th width="100px">Backup Date</th><th width="100px">Backup Status</th>';
    $msg .= '</tr>';
    $s_no = 1;
    foreach($backup_array as $site=>$val){
        if($val['status'] == 'Taken'){$color = 'green';}else{$color='red';}
        $msg .= '<tr>';
        $msg .= '<td align="center">'.$s_no.'</td><td>'.$site.'</td><td align="center" >'.$val['date'].'</td><td align="center" style="color:'.$color.'">'.$val['status'].'</td>';
        $msg .= '</tr>';
        $s_no++;

    }
    $msg .= '</table>';


    $message_html = "<hr width='20%' align='left'>
                    <img src='logo.png' width='100px'>
                    <hr width='20%' align='left'>
                    <h3>Hi Team,</h3>
                    <p>This is the list that has the every week backup status of our websites/applications that received in our AWS S3. If the 'Backup Status' is 'Not Taken' please check with the script/plugin immediately.</p>
                    ".$msg."
                    <p>Thank You</p>";

    $to = '[email protected]';
    $json_string = array(
        'to' => array($to),
        'category' => 's3_backup'
    );

    $params = array(
    'api_user' => 'xxxxx',
    'api_key' => 'yyyy',
    'x-smtpapi' => json_encode($json_string),
    'to' => $to,
    'subject' => 'Backup S3 Report!',
    'html' => $message_html,
    'from' => '[email protected]',
    );

    $request = 'https://api.sendgrid.com/api/mail.send.json';
    $session = curl_init($request);
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($session);
    curl_close($session);
    $temp = json_decode($response);

    if($temp->message == 'success'){
        $data['success'] = "Email Send Successfully!";
    }else{
        $data['success'] = "Email Send Failed!";
    }
    echo json_encode($data);
}
like image 103
vadivel a Avatar answered Nov 22 '25 21:11

vadivel a



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!