Over a million developers have joined DZone.

Subscribe Drupal Users To Mailman List

DZone's Guide to

Subscribe Drupal Users To Mailman List

· ·
Free Resource
redacted code which goes through a Drupal database subscribing all new users to a Mailman list on a separate server.  Relies upon Drupal Drush module and Mailman XMLRPC2 patch.

import os
import xmlrpclib

proxy = xmlrpclib.ServerProxy("https://lists.mydomain.org/mailman/RPC2")

def nicepass(alpha=6,numeric=2):
    returns a human-readble password (say rol86din instead of 
    a difficult to remember K8Yn9muL ) 
    import string
    import random
    vowels = ['a','e','i','o','u']
    consonants = [a for a in string.ascii_lowercase if a not in vowels]
    digits = string.digits
    ####utility functions
    def a_part(slen):
        ret = ''
        for i in range(slen):                   
            if i%2 ==0:
                randid = random.randint(0,20) #number of consonants
                ret += consonants[randid]
                randid = random.randint(0,4) #number of vowels
                ret += vowels[randid]
        return ret
    def n_part(slen):
        ret = ''
        for i in range(slen):
            randid = random.randint(0,9) #number of digits
            ret += digits[randid]
        return ret
    fpl = alpha/2               
    if alpha % 2 :
        fpl = int(alpha/2) + 1                                  
    lpl = alpha - fpl   
    start = a_part(fpl)
    mid = n_part(numeric)
    end = a_part(lpl)
    return "%s%s%s" % (start,mid,end)

if __name__ == "__main__":
        os.system(" cd /home/myuser/public_html/ && /usr/local/php5/bin/php drush sql query \"select mail from users
 where status=1 and unix_timestamp(date_sub(now(), interval 24 hour)) <= created\" | sed 1d > /home/qwep/newusers")
        for line in open("/home/myuser/newusers"):
                print line[:-1]
                        proxy.Mailman.addMember("oursite-announce", "mypassword", line[:-1], "auto-subscribed user", nicepass(6,2), 'false', 'false')
                except xmlrpclib.Fault:
                        print "unexpected error, probably already a member"
        except OSError:
                raise OSError("Couldn't remove the working file")

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}