We have received a few requests about the mailing list API. There has especially been interest in how to integrate the mailing list to an existing website without going through any Servage subscribe/unsubscribe pages. Therefore I have created a very simple sample AJAX integration example, and thought it would be interesting for some of your as well.
You need to include the jQuery library, and please be aware that the email validation is very basic, and please also note that most browsers will not allow the API call because it goes outside the scope of your domain/URL (i.e. it tries to connect to Servage’s domain, instead of yours). In such cases you need to setup a relay-script or similar, but that is not part of this simple tutorial.
<script type="text/javascript"> $(document).ready(function() { $('#newsletter form').submit(function(event) { // Preventing the default form submit and doing our API call instead event.preventDefault(); // Setting the URL for the API request var url = 'http://www.servage.net/apps/ml/?maillist=20262&api=true'; // Getting the email address var email = $('input[name=email]', $(this)).attr('value'); // Adding the email address to the request url = url + '&email=' + email; // Is this a "subscribe" or "unsubscribe"? (and adding to the request) $('input[name=option]', $(this)).each(function() { if ($(this).is('[type=radio]')) { if ($(this).is(':checked')) { url = url + '&' + $(this).attr('value') + '=true'; } } }); // Adding email to mailing list via API if (isValidEmail(email)) { $.ajax({ type: "GET", url: url, success: function(data, textStatus) { // Yes, it was added alert('Email address added to mailing list.'); }, complete: function (XMLHttpRequest, textStatus) { // Whoops, it couldn't be added if (textStatus != 'success') alert('The email address could not be added (connection error).'); } }); } else alert('Please enter a valid email address.'); }); }); function isValidEmail(email) { return (email.indexOf(".") > 2) && (email.indexOf("@") > 0); } </script> <div id="newsletter"> <form method="post" action="#"> <label for="email">Email address:</label> <input type="text" name="email" /> <label for="email">Subscribe</label> <input type="radio" name="option" value="subscribe" checked /> <label for="email">Unsubscribe</label> <input type="radio" name="option" value="unsubscribe" /> <input type="submit" name="submit" value="Submit" /> </form> </div>
7 comments (leave a comment)
For jquery include i prefer to use google’ s
or put it on my own webspace as you never know when the files are moved.
by Parko on July 13, 2009 at 18:10
What are the chances of installing a circulating mailing list like Mailman on Servage?
http://www.gnu.org/software/mailman/index.html
I’d do it but one must compile the code and install it using an RPM. Would you be willing to investigate this? It’s part of the Cpanel installation and Fantastico. Even though Servage is very proud of their control panel, there would be no harm in offering Cpanel as well as this one.
Reply by Jakob Jensen (Servage) on July 22, 2009 at 08:00
Running two or more different control panels in parallel is probably not going to happen any time soon. However, installing a large-scale, professional mailing list applications is not an impossible task. I have relayed your request to our tech department.
by Ray on July 21, 2009 at 09:12
Hi..
I have tried https://www.sympa.org/ instead… it have a better support for virtual domains the mailman…
/Dan
by Dan Hemsø on July 31, 2009 at 07:45
drop by. nice blog.
by Kate on August 3, 2009 at 01:47
Most of the time I find it adequate to set up some sub folders to sort daily emails as i get them and then archive once a month.
by treasure trooper on September 9, 2009 at 10:41
I LOVE THIS BLOG!!!
by cazinou online on August 21, 2010 at 02:34