How do I mail a message to a group of people?

Author Topic: How do I mail a message to a group of people?  (Read 317 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 139
  • Karma: 0
    • No Addiction
How do I mail a message to a group of people?
« on: 07-05-2016, 04:34:26 »
In many cases, it’s useful to be able to mail a message to more than one person at a time—for example, if you’re sending a newsletter, a message to a group mailing list, a site update notification to the IT staff, a feature freeze notification to a development team, and so on. However, in certain circumstances, sending unsolicited email to a group of people can also be called spamming—something I’m sure you won’t be doing with PHP!

If you’re using PEAR::Mail_Mime, you have the option to send group email by adding BCC or CC headers to the email message. This approach may not suit your requirements, though—listing all the addresses from an especially large distribution list in the header of a single email can quickly overwhelm your email server.

A better approach is to send each email individually, reusing the same instance of the PEAR::Mail_Mime class for each new message.

In this hypothetical example, we’ll retrieve a list of names and email addresses from a discussion forum’s member database, and reuse one instance of the PEAR::Mail_Mime class to send an email to each of those addresses:

require 'Mail.php';
require 'Mail/mime.php';
/* create the email */
$mime = new Mail_Mime("\r\n");
$mime->addAttachment('php.gif', 'image/gif');
$header = array(
=> '',
'Subject' => 'Forum Newsletter'
$mail = Mail::factory('smtp', array('host'=>'
/* go to the database to get the member information */
$dsn = 'mysql:host=localhost;dbname=forum;';
$user = 'user';
$password = 'secret';
$dbh = new PDO($dsn, $user, $password);
$sql = 'SELECT member_email, firstname, lastname FROM member';
/* cycle through the list sending the custom emails */
foreach ($dbh->query($sql) as $row)
"Howdy {$row['firstname']} {$row['lastname']}");
$body = $mime->get();
$hdrs = $mime->headers( $header);
$succ = $mail->send($row['member_email'], $hdrs, $body);
if (PEAR::isError($succ)){
error_log("Email not sent to {$row['member_email']}: " .
catch (PDOException $e)
echo 'PDO Exception Caught. ';
echo 'Error with the database: <br />';
echo 'SQL Query: ', $sql;
echo 'Error: ' . $e->getMessage();


Related Topics

  Subject / Started by Replies Last post
4 Replies
Last post 12-12-2011, 04:39:49
by smith
25 Replies
Last post 01-03-2016, 22:14:01
by hieronymusf01
0 Replies
Last post 05-14-2014, 10:16:06
by Fcmam5
6 Replies
Last post 06-06-2016, 01:16:06
by LyzLauren
1 Replies
Last post 07-18-2016, 04:11:05
by TomClarke