PHP: Generate random email addresses

This script generates 1000 random email addresses. These can be used as dummy data for testing purposes, or to tease email harvesting bots.

<?php

// array of possible top-level domains
$tlds = array("com", "net", "gov", "org", "edu", "biz", "info");

// string of possible characters
$char = "0123456789abcdefghijklmnopqrstuvwxyz";

// start output
echo "<p>\n";

// main loop - this gives 1000 addresses
for ($j = 0; $j < 1000; $j++) {

  // choose random lengths for the username ($ulen) and the domain ($dlen)
  $ulen = mt_rand(5, 10);
  $dlen = mt_rand(7, 17);

  // reset the address
  $a = "";

  // get $ulen random entries from the list of possible characters
  // these make up the username (to the left of the @)
  for ($i = 1; $i <= $ulen; $i++) {
    $a .= substr($char, mt_rand(0, strlen($char)), 1);
  }

  // wouldn't work so well without this
  $a .= "@";

  // now get $dlen entries from the list of possible characters
  // this is the domain name (to the right of the @, excluding the tld)
  for ($i = 1; $i <= $dlen; $i++) {
    $a .= substr($char, mt_rand(0, strlen($char)), 1);
  }

  // need a dot to separate the domain from the tld
  $a .= ".";

  // finally, pick a random top-level domain and stick it on the end
  $a .= $tlds[mt_rand(0, (sizeof($tlds)-1))];

  // done - echo the address inside a link
  echo "<a href=\"mailto:". $a. "\">". $a. "</a><br>\n";

} 

// tidy up - finish the paragraph
echo "</p>\n";

?>
		

Please do get in touch if you have any questions or suggestions for improvement.