We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25132
    • 129 Posts
    I have installed TXNewsletters but am experiencing a problem when I try to send a newsletter. I get the following error message:

    Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /..my server..../assets/modules/txnewsletters/txnewsletters.inc.php on line 1479

    Warning: file_get_contents(http://mydomain.com/newsletter-1.html) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /....my servers...../assets/modules/txnewsletters/txnewsletters.inc.php on line 1479


    Googling, I have found out that this happens because my host has disabled the PHP option ’allow_url_fopen’. This would normally allow a programmer to open, include or otherwise use a remote file using a URL, rather than a local file path. However, the TXNewsletters code seems to want to a URL rather than local file path.

    On the host’s help pages, they provide some example code using cURL to overcome this problem:

    Alternative for file_get_contents()

    Instead of:

    [tt]<?php
    $file_contents = file_get_contents(’http://example.com/’);
    // display file
    echo $file_contents;
    ?>[/tt]

    Use this:
    [tt]
    <?php
    $ch = curl_init();
    $timeout = 5;
    // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, ’http://example.com’);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    // display file
    echo $file_contents;
    ?>[/tt]

    However, if you receive errors you can use this:

    [tt]<?php
    $site_url = ’http://example.com’;
    $ch = curl_init();
    $timeout = 5;
    // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, $site_url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    ob_start();
    curl_exec($ch);
    curl_close($ch);
    $file_contents = ob_get_contents();o
    b_end_clean();
    echo $file_contents;
    ?>[/tt]





    The original code in the TXNewsletters module is:


    [tt]function sendHTML ($newsletterId) {
    global $modx;

    ini_set(’default_socket_timeout’, 120);
    $url = ’http://’.$_SERVER[’HTTP_HOST’].$modx->makeUrl($newsletterId);
    $html = file_get_contents($url); (Line 1479 mentioned in warning above)

    $httpDom = ’http://’.$_SERVER[’HTTP_HOST’];
    $html = preg_replace(’#(<img[^>]*src=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
    $html = preg_replace(’#(<a[^>]*href=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
    $html = preg_replace(’#(onclick=\"window\.open\(\’)([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);

    return $html;
    }[/tt]


    I have absolutely no idea how to rewrite the TXNewsletters code to match the cURL code suggested by the host without wrecking havoc! Is anybody able to help me? Thanks!!
      • 25132
      • 129 Posts
      Ok, I figured it out myself! Seems to be working fine. Now just have to figure out why the unsubscribe isn’t working ... Step by step.

      [tt] function sendHTML ($newsletterId) {
      global $modx;

      ini_set(’default_socket_timeout’, 120);
      $url = ’http://’.$_SERVER[’HTTP_HOST’].$modx->makeUrl($newsletterId);

      $ch = curl_init();
      $timeout = 5;
      // set to zero for no timeout
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $html = curl_exec($ch);
      curl_close($ch);

      $httpDom = ’http://’.$_SERVER[’HTTP_HOST’];
      $html = preg_replace(’#(<img[^>]*src=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
      $html = preg_replace(’#(<a[^>]*href=\")([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);
      $html = preg_replace(’#(onclick=\"window\.open\(\’)([^\"]*/assets)#’, ’${1}’.$httpDom.’/assets’, $html);

      return $html;
      }
      [/tt]

        • 25132
        • 129 Posts
        And just now of course I found the TXNewsletters support thread where this very problem was already discussed and solved in 2008!