let's get the love on and and share some of our favorite code, tips and tricks in php. I'll go first:
><?php
/**
* This function works with your cpanel (mailman) mailinglists. It allows to
* create a custom signup form rather than going directly through mailman. It
* works great when your are signing up a using and are collecting other
* information, you can say "Check this box to signup for the newsletter". You
* can see what i mean by going here:
*
* http://stuartdavis.com/punkmonksignup.php
*
*
* Parameters
* $email - the email address of the person to be subscribed
* $listname - official name of your mailing list
* $domain - your domain name, without the "www." (i.e., stuartdavis.com)
*
* This function will return true on success or false on failure.
*
* Created by: Matt Westgate <matt at stuartdavis.com>, 10/20/2002
*
**/
function mailingListSub($email, $listname, $domain) {
$pwd = substr(md5(time()),0,6);
$args = "&email=".urlencode(stripslashes($email)).
"&pw=".urlencode(stripslashes($pwd)).
"&pw-conf=".urlencode(stripslashes($pwd)).
"&digest=0".
"&email-button=Subscribe";
$header = "POST /mailman/subscribe/".trim($listname)."_".$domain." HTTP/1.0\r\n";
$header .= "Host: ".$domain."\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= 'Content-Length: ' . strlen($args) . "\r\n";
$header .= "Connection: Close\r\n\r\n";
$fp = fsockopen($domain, 80, $errno, $errstr, 30);
if (!$fp) { // ERROR
fclose($fp);
return false;
} else {
fputs($fp, $header . $args);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
//echo "$res\n";
if (strstr($res, "Confirmation")) {
fclose($fp);
return true;
} elseif (strstr($res, "deferred")) {
fclose($fp);
return true;
}
}
}
fclose($fp);
return false;
}
/**
* Takes as input and email address and returns true if it is well-formed.
* It doesn't doesn't check to see if the email address actually exists. just
* that it looks right.
*
* Created by: Matt Westgate <matt at stuartdavis.com>, 06/05/2000
**/
function validEmail($email) {
$email = trim($email);
if ($email == "")
return false;
$email_regex="^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~ ])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~ ]+\.)+[a-zA-Z]{2,6}\$";
return eregi($email_regex, $email);
}
?>