First off, remember to always use the Dada::MailingList::Subscribers module in your script:
#!/usr/bin/perl use DADA::MailingList::Subscribers;
If your script isn't in the, cgi-bin/dada directory, make sure to add the library path:
#!/usr/bin/perl # Adding the lib path to the dada directory: use lib qw(/home/myaccount/www/cgi-bin/dada); use DADA::MailingList::Subscribers;
Easy enough. To create a DADA::MailingList::Subscribers object, you'll need only one thing, the listshortname of the list. For all these examples, we'll use, mylist.
Create a new DADA::MailingLIst::Subscribers object:
#!/usr/bin/perl # Adding the lib path to the dada directory: use lib qw(/home/myaccount/www/cgi-bin/dada); use DADA::MailingList::Subscribers; my $lh = DADA::MailingList::Subscribers->new(-List => 'mylist');
Once you have the DADA::MailingList::Subscribers object, you can simply use the, add_to_email_list method, like this:
# add *one* email address to your list: $lh->add_to_email_list(-Email_Ref => ['user1@example.com']);
The, -Email_Ref paramater takes an array reference of email addresses you want added.
So, if you want to add more than one email address, you'd say this:
# add two email addresses to your list: $lh->add_to_email_list(-Email_Ref => ['user1@example.com', 'user2@example.com']);
Now, before you add an email address to your list, you may want to make sure that the email address should be added to a mailing list.
For example, if the address is already subscribed, you won't want to subscribe it twice, so do a quick check that everything is alright:
my ($status, $errors) = $lh->subscription_check(-Email => 'user1@example.com');
If $status is equal to 1, it should be safe to subscribe the email address:
my $email = 'user1@example.com'; my ($status, $errors) = $lh->subscription_check(-Email => $email); if($status == 1){ $lh->add_to_email_list(-Email_Ref => [$email]); } else { print "there were problems subscribing $email"; }
$errors holds a hashref to any problems found, that you can then look at:
my ($status, $errors) = $lh->subscription_check(-Email => $email); foreach(keys %$errors){ print "Problem: $_\n"; }
For example, the above code may print back:
Problem: subscribed
This would let you know that the email address is already subscribed.
In you code you could say something like:
my $email = 'user1@example.com'; my ($status, $errors) = $lh->subscription_check(-Email => $email); if($status == 1){ $lh->add_to_email_list(-Email_Ref => [$email]); } else { if($errors->{subscribed}){ print "The email address, $email is already subscribed."; } }
For more information on the subscription_check method, see:
http://mojo.skazat.com/support/documentation/MailingList_Subscribers.pm.html#subscription_check
If you have a large number of email addresses you want to look at one time, consider using the, filter_subscribers method. It takes a reference to an array of email addresses, like add_to_email_list, but returns 5 seperate list refs, with your emails sorted in the following categories:
Use it like this:
my = @addresses = qw( user1@example.com user2@example.com dsafjfadskjadfs alreadysubscribed@example.com blacklisted@example.com );
my ($subscribed, $not_subscribed, $black_listed, $not_white_listed, $invalid) = $lh->filter_subscribers(-Email_Ref => [@addresses]);
In our example, let's say, alreadysubscribed@example.com is actually subscribed to our mailing list, and blacklisted@example.com is actually blacklisted from our mailing list. Printing out our results:
print "Subscribed:\n"; foreach(@$subscribed){ print "* $_\n"; } print "Not Subscribed:\n"; foreach(@$not_subscribed){ print "* $_\n"; } print "Black Listed:\n"; foreach(@$black_listed){ print "* $_\n"; } print "Invalid Addresses:\n"; foreach(@$invalid){ print "* $_\n"; }
Would print something like this:
Subscribed: * alreadysubscribed@example.com Not Subscribed: * user1@example.com * user2@example.com Black Listed: * blacklisted@example.com Invalid Addresses: * dsafjfadskjadfs
You could then safely subscribed the addresses that are valid, and aren't already subscribed:
my = @addresses = qw( user1@example.com user2@example.com dsafjfadskjadfs alreadysubscribed@example.com blacklisted@example.com ); my ($subscribed, $not_subscribed, $black_listed, $not_white_listed, $invalid) = $lh->filter_subscribers(-Email_Ref => [@addresses]); $lh->add_to_email_list(-Email_Ref => [$not_subscribed]); print "Just Subscribed the following addresses:\n"; foreach(@$not_subscribed){ print "$_\n"; }
The following program ties everything we've learned together - it's a simple script that will take two paramaters:
This script does a similar job as the list administration, add screen, but on the commandline. It may come useful, if timeouts occure in the web-based version.
Here's the program - it's meant to be run via the commandline. As it's setup now, you can put it in the, cgi-bin/dada/ directory and it'll run out of the box. If you want to have it run anywhere else, just change the, use lib line.
#!/usr/bin/perl -w use strict; use 5.006; use lib qw(./ ./DADA ./DADA/perllib); use DADA::MailingList::Subscribers; use DADA::App::Guts; use Getopt::Long; my $verbose = 1; my $list; my $file; GetOptions("verbose=i" => \$verbose, "list=s" => \$list, "file=s" => \$file, ); main(); sub main { if(!$list){ print "You must pass a listshortname in the, '--list' paramater\n"; exit; } if(DADA::App::Guts::check_if_list_exists(-List => $list) == 0){ print "Your list does seem to be valid! Double check!\n"; print "Available Lists:\n\n"; foreach(DADA::App::Guts::available_lists()){ print $_ . "\n"; } exit; } if(! $file){ print "You must pass a path to a file that holds a list of subscribers you'd like to subscirbe!\n"; exit; } if (! -e $file){ print "Can't find: $file file!\n"; } my $addresses = get_addresses(); my $lh = DADA::MailingList::Subscribers->new(-List => $list); my ($subscribed, $not_subscribed, $black_listed, $not_white_listed, $invalid) = $lh->filter_subscribers(-Email_Ref => $addresses); if($verbose){ if($subscribed->[0]){ print "\nAlready Subscribed:\n" . '-' x 72 . "\n"; foreach(@$subscribed){ print "\t$_\n"; } } if($black_listed->[0]){ print "\nBlack Listed:\n" . '-' x 72 . "\n"; foreach(@$black_listed){ print "\t$_\n"; } } if($not_white_listed->[0]){ print "\nNot White Listed:\n" . '-' x 72 . "\n"; foreach(@$not_white_listed){ print "\t$_\n"; } } if($invalid->[0]){ print "\nInvalid Emails:\n" . '-' x 72 . "\n"; foreach(@$invalid){ print "\t$_\n"; } } if($not_subscribed->[0]){ print "\nSubscribing the following emails:\n" . '-' x 72 . "\n"; foreach(@$not_subscribed){ print "\t$_\n"; } } } my $new_email_count = $lh->add_to_email_list(-Email_Ref => $not_subscribed); print "\n" if $verbose; print "Subscribed $new_email_count address(es);\n\n" if $verbose; } sub get_addresses { open my $ADDRESS_FILE, '<', $file or die "Cannot read file at: '" . $file . "' because: " . $!; my $addresses = do { local $/; <$ADDRESS_FILE> }; close $ADDRESS_FILE or die "Didn't close: '" . $file . "'properly because: " . $!; my @new_addresses = split(/\s+|,|;|\n+|\r+/, $addresses); return \@new_addresses; }
When you're on the command line run the program like this:
/home/myaccount/cgi-bin/dada justin> perl dada_add_subscribers.pl --list mylist --file addresses_to_add.txt