Distributing an HTML email to a mailing list

Shaffer 0 Tallied Votes 141 Views Share

This function distributes mail to a mailing list, out of an array. Wether the array is plundered from a database, a file, or from the code, the function only takes a prepared one. Also, this function is only good for HTML messages.

The array's syntax is to be:

array("recipent's name"=>"email","recipent's name"=>"email","recipent's name"=>"email");

In-order to insert the name into the email, just use ((name)), as it will be replaced with the array-given value.
$from is for the email you want to be replied to.
If all recipents were mailed successfully, the function returns true, else it returns the errors.
You can use it like so:

if($handle=@mail_list($array,$subject,$message,$from)) {
  #...#
}
else {
  echo $handle;
}

If you want, I can make another function with a lot more variables to send. Just request it via comment, please.

function mail_list($ml_array,$subject,$message,$from) {
  $i=0;
  foreach($ml_array as $name => $email) {
    $message = str_replace("((name))",$name,$message);
    if(@mail($email,$subject,$message,"Content-type: Text/HTML;From:".$from.";Reply-To:".$from.";")) {
      $i++;
    }
    else {
      $errors[] = "Failed to send email to ".$email.".";
    }
  }
  if($i==sizeof($ml_array)) return true;
  else return implode("<br/>",$errors);
}
sonny.scroggin 0 Newbie Poster

This is great! I've been thinking about how to create a script that would do just this!

I'm still learning php so I have a question regarding how to initiate the array to pull the a list of email addresses from a mysql database.

Could you help me with that?

Thank you,

Sonny

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.