mail notification on create page

,
<?php
/*
* @author     Fred Radeff <fradeff@akademia.ch>
* @copyright  Copyright (c) 2019 radeff.red (https://radeff.red)
* @license    http://www.gnu.org/licenses/ GNU General Public License
* base documentation: https://documentation.concrete5.org/developers/sending-mail/working-mail-service
* a single page to notify by email on new pages on a concrete5's website
* usage:
* - create a single page with a random name, eg NkjK4sfdHsf3VJN4nqgTnotifyonaddpage.php
* - modifiy (adapt begin -> adapt end)
* - create a daily cron calling the url of the single page AND INTERCEPT ERRORS!
 */
 
$th = Loader::helper('text');
$dh = Core::make('helper/date'); /* @var $dh \Concrete\Core\Localization\Service\Date */
$nh = Loader::helper('navigation');
$mailService = Core::make('mail');
$mailService->setTesting(false); // or true to throw an exception on error.
 
//adapt begin
$delay=1; //how much days to consider this is a new page? default=1day;
$site="radeff.red/c58";
$subject="Nouveautés sur le site : " .$site;
$mailService->from('site@akademia.ch', 'webmaster');
$destinataire='zpartakov@akademia.ch';
//adapt end
 
$compteur=0;
$maintenant=date("U");
 
$list = new \Concrete\Core\Page\PageList();
//$list->sortByPublicDate();
$list->sortByPublicDateDescending();
$pages = $list->getResults();
 
$mailContent="<p>Modifications sur le site: " .$site ." depuis " .$delay ." jour(s)</p><hr />";
$mailContent.= '<div class="grid-container">';
 
foreach ($pages as $page):
  $title = $th->entities($page->getCollectionName());
  $dateU=$page->getCollectionDatePublic();
  $dateU=strtotime($dateU);
  $date = $dh->formatDateTime($page->getCollectionDatePublic(), true);
  $url = $nh->getLinkToCollection($page);
  if($maintenant-$dateU<($delay*24*3600)){ //only if overlaid delay
    $compteur++;
    $mailContent.= "<div class=\"row\">";
    $mailContent.= $date;
    $mailContent.= " : ";
    $mailContent.="<a href=\"";
    $mailContent.= $url;
    $mailContent.="\">";
    $mailContent.= $title;
    $mailContent.= "</a>";
    $mailContent.= "</div>";
  }
endforeach;
$mailContent.= '</div>';
$mailContent.= "<hr /><p><em>Ceci est un mail automatique. Merci de ne pas y répondre.</em></p>";
 
//echo $mailContent;
 
$mailService->load('mailtemplate');
$bodyHTML .= '<!DOCTYPE html>';
$bodyHTML .= '<html lang="fr">';
$bodyHTML .= '  <head>';
$bodyHTML .= '      <meta charset="utf-8">';
$bodyHTML .= '  </head>';
$bodyHTML .= '  <body>';
$bodyHTML .= '      <div>';
$bodyHTML .= $mailContent;
$bodyHTML .= '      </div>';
$bodyHTML .= '  </body>';
$bodyHTML .= '</html>';
 
$mailService->setBodyHTML($bodyHTML);
$mailService->setSubject($subject);
$mailService->to($destinataire);
 
if($compteur==0){
  echo "<p>pas de nouveautés</p>";
}else{
 
echo "<p>il y a des nouveautés, envoi du mail</p>";
$mailService->sendMail();
}
  • info/concrete5/58/notifyoncreate.txt
  • Dernière modification: 2019/09/03 13:22
  • par radeff