Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Project: com_music
  4.  * File: send_emails.php
  5.  * Created by Jesús Estévez Rodríguez <jesus@jensen.technology>
  6.  * Date: 22/04/19
  7.  * Time: 10:18
  8.  */
  9. /**
  10.  * A command line cron job to attempt to remove files that should have been deleted at update.
  11.  */
  12.  
  13. // We are a valid entry point.
  14. const _JEXEC = 1;
  15.  
  16. // Load system defines
  17. if (file_exists(dirname(__DIR__) . '/defines.php'))
  18. {
  19.     require_once dirname(__DIR__) . '/defines.php';
  20. }
  21.  
  22. if (!defined('_JDEFINES'))
  23. {
  24.     define('JPATH_BASE', dirname(__DIR__));
  25.     require_once JPATH_BASE . '/includes/defines.php';
  26. }
  27.  
  28. // Get the framework.
  29. require_once JPATH_LIBRARIES . '/import.legacy.php';
  30.  
  31. // Bootstrap the CMS libraries.
  32. require_once JPATH_LIBRARIES . '/cms.php';
  33.  
  34. // Configure error reporting to maximum for CLI output.
  35. error_reporting(E_ALL);
  36. ini_set('display_errors', 1);
  37.  
  38.  
  39. /**
  40.  * A command line cron job to send the emails to the musicians.
  41.  *
  42.  * @since  3.0
  43.  */
  44. class Sendemails extends JApplicationCli
  45. {
  46.     /**
  47.      * Entry point for CLI script
  48.      *
  49.      * @return  void
  50.      *
  51.      * @since   3.0
  52.      */
  53.     public function doExecute()
  54.     {
  55.         // Database connector
  56.  
  57.         $db = JFactory::getDBO();
  58.         $this->out('Checking for logs not sent');
  59.         $this->out('...');
  60.  
  61.         //Checking for unsent mails
  62.  
  63.         $query = $db->getQuery(true);
  64.         $query
  65.             ->select(array('b.email', 'a.*'))
  66.             ->from($db->quoteName('#__music_email_logs', 'a'))
  67.             ->join('INNER', $db->quoteName('#__users', 'b') . ' ON (' . $db->quoteName('a.email_muscian_id') . ' = ' . $db->quoteName('b.id') . ')')
  68.             ->where($db->quoteName('a.email_sent_time') . ' = 0000-00-00 00:00:00');
  69.         $db->setQuery($query);
  70.         $mails = $db->loadObjectList();
  71.         $this->out('There are ' . count($mails) . ' unsent mails.');
  72.  
  73.         // If there are some mail remaining to send, send it
  74.         if (count($mails) !== 0)
  75.         {
  76.             foreach ($mails as $mail)
  77.             {
  78.                 // Send emails
  79.                 $this->out('Preparing email to send it');
  80.                 $to   = $mail->email;
  81. //              $from = $mail->email_from;
  82.                 $from = array("jesus@jensen.technology", "Jesus Estevez");
  83.  
  84.                 # Invoke JMail Class
  85.                 $mailer = JFactory::getMailer();
  86.  
  87.                 # Set sender array so that my name will show up neatly in your inbox
  88.                 $mailer->setSender($from);
  89.  
  90.                 # Add a recipient -- this can be a single address (string) or an array of addresses
  91.                 $mailer->addRecipient($to);
  92.  
  93.                 $mailer->setSubject($mail->email_subject);
  94.                 $mailer->setBody($mail->email_body);
  95.  
  96.                 # If you would like to send as HTML, include this line; otherwise, leave it out
  97.                 $mailer->isHTML();
  98.  
  99.                 # Send once you have set all of your options
  100.                 $sent = $mailer->send();
  101.  
  102.                 if ($sent)
  103.                 {
  104.                     $this->out('Sent!');
  105.                     $this->out('Updating log with id ' . $mail->id);
  106.  
  107.                     // Update sent_time column
  108.  
  109.                     $query = $db->getQuery(true);
  110.  
  111.                     // Update the sent_time field with the actual time.
  112.                     $fields = array(
  113.                         $db->quoteName('email_sent_time') . ' = ' . date('m-d-Y h:i:s', time())
  114.                     );
  115.  
  116.                     // Conditions for which records should be updated.
  117.                     $conditions = array(
  118.                         $db->quoteName('id') . ' = ' . $mail->id,
  119.                     );
  120.  
  121.                     $query->update($db->quoteName('#__music_email_logs'))->set($fields)->where($conditions);
  122.  
  123.                     $db->setQuery($query);
  124.  
  125.                     $result = $db->execute();
  126.                 } else
  127.                 {
  128.                     $this->out('Error sending email with the log id: ' . $mail->id );
  129.  
  130.                 }
  131.             }
  132.         }
  133.         else
  134.         {
  135.             $this->out('All the mails are already sent');
  136.  
  137.         }
  138.  
  139.     }
  140.  
  141.  
  142. }
  143.  
  144. // Instantiate the application object, passing the class name to JCli::getInstance
  145. // and use chaining to execute the application.
  146. JApplicationCli::getInstance('Sendemails')->execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement