GreenArrow Email Software Documentation

PHPMailer SimpleMH Multiple Recipient Example

Below is an example of using the SimpleMH Injection Method with PHPMailer to send multiple messages in a single SMTP session. See the SimpleMH Headers page for information on what the headers shown in this document represent. See the PHPMailer SimpleMH Example page for a more basic example with one recipient.

<?php

// phpmailer-simplemh-multi-recipient-example.php
// Inject multiple test messages into GreenArrow Engine using PHPMailer and SimpleMH

// Use PHPMailer
require_once('/usr/share/php/PHPMailer/class.phpmailer.php');
require_once('/usr/share/php/PHPMailer/class.smtp.php');

// GreenArrow Engine installation settings
$mail->Host = "mta.example.com"; // Connect to this GreenArrow server
$mail->SMTPAuth = true; // enables SMTP authentication. Set to false for IP-based authentication
$mail->Port = 587; // SMTP submission port to inject mail into. Usually port 587 or 25
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "password"; // SMTP password
//$mail->SMTPDebug = 2; // uncomment to print debugging info

// Timezone
date_default_timezone_set('America/Chicago');

// Campaign Settings
$mail_class = "transactional"; // Mail Class to use
$from_address = "[email protected]";
$from_name = "Newsletter";
$subject = "SimpleMH PHPMailer Example";
$html_body = "HTML body goes here.";
$text_body = "Text body goes here.";

// List of recipients
$recipients = array(
	"[email protected]" => "Name 1",
	"[email protected]" => "Name 2",
);

// Create the SMTP session
$mail = new PHPMailer();
$mail->IsSMTP(); // Use SMTP
$mail->SMTPKeepAlive = true; // prevent the SMTP session from being closed after each message
$mail->SmtpConnect();

// Set headers that are constant for every message outside of the foreach loop
$mail->SetFrom($from_address, $from_name);
$mail->Subject = $subject;
$mail->addCustomHeader("X-GreenArrow-MailClass: $mail_class");

// Send a message to each recipient.
// Headers that are unique for each message should be set within the foreach loop
foreach ($recipients as $email => $name) {

	// Generate headers that are unique for each message
	$mail->ClearAllRecipients();
	$mail->AddAddress($email, $name);

	// Generate the message
	$mail->MsgHTML($html_body);
	$mail->AltBody = $text_body;

	// Send the message 
	if($mail->Send()) {
		echo "Message sent!\n";
	} else {
		echo "Mailer Error: " . $mail->ErrorInfo . "\n";
	}

}

// Close the SMTP session
$mail->SmtpClose();