The Swiftmailer docs says you can create your own class to handle replacements using the Decorator plugin:
class DbReplacements implements Swift_Plugins_Decorator_Replacements {
public function getReplacementsFor($address) {
$sql = sprintf(
"SELECT * FROM user WHERE email = '%s'",
mysql_real_escape_string($address)
);
$result = mysql_query($sql);
if ($row = mysql_fetch_assoc($result)) {
return array(
'{username}'=>$row['username'],
'{password}'=>$row['password']
);
}
}
}
But in my case my database contains duplicated email address, i.e. the same address can appear on 3-4 accounts, so I need to get the replacements based on the user id instead.
How do I modify the above class to match my criteria?
Since
swiftmailerdon’t know anything about id, you have to translate email to id by yourself. For example add new property toDbReplacementsholding 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 ingetReplacementsForsimply use email as index in this array to get user id.Code sample to make it more clear: