Since swiftmailer
don't know anything about id, you have to translate email to id by yourself. For example add new property to DbReplacements
holding an associative array 'email' => 'id'
(of course firstly limited to known ID, ie. SELECT email, id FROM user WHERE id IN(1,3,6,77)
) and in getReplacementsFor
simply use email as index in this array to get user id.
Code sample to make it more clear:
class DbReplacements implements Swift_Plugins_Decorator_Replacements {
public $email2id = array();
public function getReplacementsFor($address) {
$sql = sprintf(
"SELECT * FROM user WHERE id = %d", $this->email2id[$address]
);
$result = mysql_query($sql);
if ($row = mysql_fetch_assoc($result)) {
return array(
'{username}'=>$row['username'],
'{password}'=>$row['password']
);
}
}
}
$dbReplacer = new DbReplacements();
$decorator = new Swift_Plugins_DecoratorPlugin($dbReplacer);
$mailer->registerPlugin($decorator);
$users = array(
array('email'=>'john.doe@example.com', 'id' => 16),
array('email'=>'john.doe2@example.com', 'id' => 13),
);
foreach ($users as $user) {
$message->addTo($user['email']);
$dbReplacer->email2id[$user['email']] = $user['id'];
}