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
Multiple forms + 1 config file
Moderators: Chris Corbyn, General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
The best way to do this is to do something like I do for the smoke tests. Create a config file with something like:
Then create a factory to make new Swift instances.
Then replace $swift = new Swift(...); everywhere in your code with:
Code: Select all
<?php
define('MAILER_TYPE', 'smtp');
define('SMTP_HOST', 'smtp.server.tld');
define('SMTP_PORT', 25);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;
}
}Code: Select all
$factory = new SwiftFactory();
$swift = $factory->createSwift();