Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: sanitize UTF8 chars for mailman

...

2. Copy the script below and save it under the name sync2mailman.php

Code Block

 <?php
require_once '/path/to/civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'api/v2/Contact.php';

// the following array is used to sanitize special characters for mailman without using libraries
// such as iconv, Normalizer, or Transliterator
$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );

$config =& CRM_Core_Config::singleton();

global $tmpfilename;
global $mmparams;

$tmpfilename = '/home/myusername/sync2mailman.txt'; // Temporary file location that is not accessible to web

// Add the Mailman sync_members parameters here
// see http://www.tin.org/bin/man.cgi?section=8&topic=sync_members for syntax
// after testing, remove the -n argument for production
$mmparams = "-n -w=no -g=no";

function SyncList($mmlistname, $GroupId) {
  global $tmpfilename;
  global $mmparams;
  global $chars;

   // Open the temporary file
  $f = fopen($tmpfilename, 'w+');

  // Set search criteria
  $params['rowCount'] = 50000;
  $params['sort'] = 'sort_name ASC';
  $params['return.display_name'] = 1;
  $params['return.email'] = 1;
  $groups = array($GroupId => 1);
  $params['group'] = $groups;
  $result = civicrm_contact_search( $params );

  // Process the search results
  $nrows = 0;  
  foreach ($result as $id => $values) {
    if ($values['email'] != '') {
      $fix_displayname = str_replace('  ', ' ', $values['display_name']); // remove extra spaces in some names
      $line = trim($fix_displayname) . ' <' . trim($values['email']) . '>' . "\n";
      fwrite(      $line = strtr($line, $chars);  // sanitize UTF8 characters so mailman can handle them
      fwrite($f, $line);
      $nrows++;
    }
  }

  if ($nrows > 0) { // Zero rows is probably an error
    // Run the mailman sync_members command
    $command = "/usr/sbin/sync_members $mmparams -f $tmpfilename $mmlistname 2>&1";
    echo "Running $command\n";
    $output =  shell_exec($command);
    echo $output;
  }
  else
    echo "WARNING no rows... skipping sync command.\n";

  // Close the temporary file
  fclose($f);
}

SyncList('list_name','1'); // specify your mailing list name and CiviCRM group id here
// repeat for each list
?>

...

6. Setup a cron. For example "/usr/sbin/php -f /home/myusername/bin/sync2mailman.php"

...