We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33983 ☆ A M B ☆
    • 181 Posts
    I am writing a newsletter extra and I have a lot of things done. Now I am trying to get the step of sending out the list or lists that can be for many different eletters. The problem that I am having is the ->process() is not working as expected for my snippet.

    In the code below I am getting the Resource associated with the newsletter and then process it to get the output. But what happens is that process() method returns the current Resource that this snippet is in. Am I doing something wrong or is this a bug?

    // process the eletter but leave the placeholders as
    $doc = $modx->getObject('modResource', array('id'=>$this->get('resource')));
            
    $modx->log(modX::LOG_LEVEL_ERROR,'EletterNewsletter->_createEletter() - Resource: '.$this->get('resource') );
            
    $docUrl = preg_replace('/&/', '&', $modx->makeUrl($this->get('resource'), '', '&sending=1', 'full') );
           
    $modx->log(modX::LOG_LEVEL_ERROR,'EletterNewsletter->_createEletter() - Resource URL: '.$docUrl );
            
    $context = $modx->getObject('modContext', array('key' => $doc->get('context_key')));
    $contextUrl = $context->getOption('site_url', $modx->getOption('site_url'));
            unset($context);
    
    // the eletter body/message:
    $message = $doc->process();
    


    See the full code here:
    https://github.com/jgulledge19/GroupEletters/blob/master/core/components/groupeletters/model/groupeletters/eletternewsletters.class.php

    Thanks for reviewing!
      • 3749
      • 24,544 Posts
      It depends on what $this is. Also, you want this:

      $modx->makeUrl($url, "", "", "full");


      $resource->process() is usually not reliable for getting a fully rendered resource. You might want to take a look at the EmailResource package. It may do just what you want, and if not, you may find the code useful.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 33983 ☆ A M B ☆
        • 181 Posts
        Thanks Bob,

        The $this is the newsletter object.

        I did review your package and I got some ideas from it, thanks. What I want to do is allow on demand processing. Lets say I had 10 different newsletters to be sent out to be processed by the queue. I would like the first run to grab the latest or the current resource and then process it. What GroupEletters does now is you have to send a Test and then it saves that version and uses it to process. But if a person makes a change and did not send a test then the changes do not get transferred over. I guess I could process and every time on the save event.
          • 3749
          • 24,544 Posts
          It isn't how EmailResource does it, but the most reliable method would be to just get the pages with cURL. It's a little slow, but that way you get the fully processed page.

          This code writes the page to a file, but should give you the idea. You could put the pagtitles or IDs in a comma-separated list in a snippet property and loop through them.

          $ch = curl_init(); // Initialize Curl
          if ($ch === false) {
              echo "Failed to initialize cURL";
          }
          
          @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
          @curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1); // don't use a cached version of the url
          
          $url = 'http://localhost/addons/';
          
          curl_setopt($ch, CURLOPT_URL, $url); // Set CURL options
          
          $resource = curl_exec($ch); // get the page
          
          if (curl_errno($ch)) {
              echo  'cURL error: ' . curl_errno($ch) . " - " . curl_error($ch);
          } else {
              $file = 'c:/xampp/htdocs/addons/assets/mycomponents/curltest/doc.html';
              $fp = fopen($file,'w');
              if (! $fp) {
                  echo 'Could not open file';
              } else {
                  fwrite($fp,$resource);
                  fclose($fp);
              }
          
          }
          
          curl_close($ch);




          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting