Multiple forms + 1 config file

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
mrdebian
Forum Newbie
Posts: 9
Joined: Thu May 10, 2007 4:38 am

Multiple forms + 1 config file

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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();
Post Reply