Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Recipes Batch recipients type change

joebeeson edited this page Jun 21, 2011 · 1 revision

This recipe will show you how to easily adjust the type value for the recipients of an Email object.

Synopsis

Let's say you are sending out a mass email, a newsletter of sorts, and want to easily change all recipients of the email to a BCC instead of TO so that you can effectively hide their email addresses from one another.

Example

<?php

// Create our email object and add recipients
$email = $this->Postman->create('no-reply@company.com', 'Monthly Newsletter');
$email->addRecipient('jack@jack.com');
$email->addRecipient('jill@jill.com');
/* More recipients! */

array_map(
	function($recipient) { $recipient->setType('bcc'); },
	$email->getRecipients()
);

Explanation

The secret sauce here involves the array_map method and PHP's anonymous function support.

We ask the Email object to give us all of the Recipient objects via getRecipients and hand them off to our anonymous function which performs a setType on the object, changing it to a BCC.