Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:44:22+00:00 2026-06-13T22:44:22+00:00

The Swiftmailer docs says you can create your own class to handle replacements using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:44:23+00:00Added an answer on June 13, 2026 at 10:44 pm

    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'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short: Is it possible using PHP / SwiftMailer / Throttler Plugin - if yes,
I am using gmail as an smtp server for my swiftmailer class. however I
I am creating a mail class in symfony using swiftmailer. I have written mailer
I am using swiftmailer , a library for sending mail in php. The problem
I'd like to use Sendgrid WebAPI preferably without SMTP or Swiftmailer using the code
I'm having a problem with the latest major release of SwiftMailer where the decorator
I am able to attach a file using SwiftMailer with its name hardcoded. But
I'm using Swiftmailer to send the contents of a form to a gmail account.
I'm currently using SwiftMailer to send out emails to several users (up to 50).
How to send spool from swiftmailer without using command? php app/console swiftmailer:spool:send --env=prod I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.