gpt4 book ai didi

php - 如何在 WordPress 备份后每周发送邮件?

转载 作者:行者123 更新时间:2023-12-01 12:18:50 27 4
gpt4 key购买 nike

WordPress 中有多个站点。 backup buddy plugin 使用配置好的 amazon S3 bucket 我们需要每周发送邮件 在所有站点备份信息中是否收到最近的备份。

最佳答案

终于找到答案了

首先安装 AWS S3 包引用这个'https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html '然后下面提到的代码创建 index.php 文件然后在浏览器中调用工作正常

当你想自动运行 index.php 文件时使用 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 = 'reciever@example.com';
$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' => 'info@example.com',
);

$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);
}

关于php - 如何在 WordPress 备份后每周发送邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com