Swiftmailer:用户ID的dbReplacements?

时间:2012-11-04 08:42:55

标签: php mysql email smtp swiftmailer

Swiftmailer文档说你可以使用Decorator插件创建自己的类来处理替换:

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']
      );
    }
  }
}

但在我的情况下,我的数据库包含重复的电子邮件地址,即同一地址可以显示在3-4个帐户上,因此我需要根据用户ID获取替换。

如何修改上述课程以符合我的标准?

2 个答案:

答案 0 :(得分:1)

由于swiftmailer对id不了解,因此您必须自行将电子邮件转换为id。例如,将新属性添加到DbReplacements持有关联数组'email' => 'id'(当然首先限于已知ID,即。SELECT email, id FROM user WHERE id IN(1,3,6,77)),并在getReplacementsFor中仅使用电子邮件作为索引此数组用于获取用户ID。

代码示例使其更清晰:

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'];
}

答案 1 :(得分:1)

  

如何修改上述课程以符合我的标准?

你做不到。 ID丢失了。除非您没有至少与相关消息相关联的ID(即幕后发生的事件消息),否则您将无法创建Class Swift_Plugins_DecoratorPlugin提供的具体子类型它是自己的_Replacements接口,在要求替换时提供消息。

让我们创建你自己的插件,它仍然是替换的装饰插件:

<?php

interface My_Swift_Plugins_Decorator_Replacements extends Swift_Plugins_Decorator_Replacements
{
    public function setMessage(Swift_Mime_Message $message);
}

class My_Swift_Plugins_DecoratorPlugin extends Swift_Plugins_DecoratorPlugin implements My_Swift_Plugins_Decorator_Replacements
{

    private $_replacements;

    public function __construct(My_Swift_Plugins_Decorator_Replacements $replacements) {
        $this->_replacements = $replacements;
    }

    /**
     * Invoked immediately before the Message is sent.
     *
     * @param Swift_Events_SendEvent $evt
     */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt) {
        $this->setMessage($evt->getMessage());
        parent::beforeSendPerformed($evt);
    }

    public function setMessage(Swift_Mime_Message $message) {
        $this->_replacements->setMessage($message);
    }
}

如果您为插件中的消息分配ID,则会在调用getReplacementsFor之前获取消息集。然后,您可以将其分配给属性并在该函数中读取它。

class DbReplacements implements My_Swift_Plugins_Decorator_Replacements {
  private $message;
  public function setMessage(Swift_Mime_Message $message) {
      $this->message = $message;
  }

  public function getReplacementsFor($address) {
    $sql = sprintf(
      "SELECT * FROM user WHERE email = '%s' and id = '%d'",
      mysql_real_escape_string($address),
      $this->message->emailID;
    );

    $result = mysql_query($sql);

    if ($row = mysql_fetch_assoc($result)) {
      return array(
        '{username}'=>$row['username'],
        '{password}'=>$row['password']
      );
    }
  }
}