Page 1 of 1

Multiple forms + 1 config file

Posted: Tue Dec 25, 2007 2:19 am
by mrdebian
Hello,

I have around 15 forms all using swiftmailer and would like to seperate the part of the code that smtp, user details are given in order for client to easily update only one page than editing 15.
Can I do that just by cutting the code, pasting it to a file where then I just include in each page or there is another way more 'gently' to achieve this?

Thanks

Posted: Wed Dec 26, 2007 4:38 pm
by Chris Corbyn
The best way to do this is to do something like I do for the smoke tests. Create a config file with something like:

Code: Select all

<?php

define('MAILER_TYPE', 'smtp');
define('SMTP_HOST', 'smtp.server.tld');
define('SMTP_PORT', 25);
Then create a factory to make new Swift instances.

Code: Select all

class SwiftFactory {
  public function createSwift($type=null) {
    if (is_null($type)) {
      $type = MAILER_TYPE;
    }
    switch ($type) {
      case 'smtp':
        $swift = new Swift(new Swift_Connection_SMTP(SMTP_HOST, SMTP_PORT));
        break;
      case 'sendmail':
        $swift = new Swift(new Swift_Connection_Sendmail(SENDMAIL_PATH));
        break;
      case 'mail':
        $swift = new Swift(new Swift_Connection_NativeMail());
        break;
    }
    return $swift;
  }
}
Then replace $swift = new Swift(...); everywhere in your code with:

Code: Select all

$factory = new SwiftFactory();
$swift = $factory->createSwift();