diff --git a/README.md b/README.md index c4ee129..2b60d98 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,6 @@ This plugin allows users to switch between different accounts (including remote) * Branch 3.X - for Roundcube v1.3 Please specify verion like "~2.0" in your composer.json file for ident_switch requirement. In this case you will stay inside compatible branch until you manually update ypur Roundcube installation. + +### Switching SMTP ### +Plugin also switched SMTP credentials but only for server specified in general config. You should use %u and %p substitutions for user and password to make it work. This substitutions are replaced by username and password for selected IMAP account. diff --git a/ident_switch.php b/ident_switch.php index 02d07d2..4b5cf71 100644 --- a/ident_switch.php +++ b/ident_switch.php @@ -150,16 +150,29 @@ class ident_switch extends rcube_plugin function on_smtp_connect($args) { + $iid = $_SESSION['iid' . self::MY_POSTFIX]; + if (!is_integer($iid) || $iid == -1) + return $args; + $rc = rcmail::get_instance(); - // TODO: Rewrite with full settings! - - if (strcasecmp($rc->user->data['username'], $_SESSION['username']) !== 0) + $sql = 'SELECT host, flags, port, username, password, iid FROM ' . $rc->db->table_name(self::TABLE) . ' WHERE iid = ? AND user_id = ?'; + $q = $rc->db->query($sql, $iid ,$rc->user->ID); + $r = $rc->db->fetch_assoc($q); + if (is_array($r)) { - if ($args['smtp_user'] == '%u') - $args['smtp_user'] = $rc->user->data['username']; - if ($args['smtp_pass'] == '%p') - $args['smtp_pass'] = $rc->decrypt($_SESSION['password' . self::MY_POSTFIX]); + if (!$r['username']) + { // Load email from identity + $sql = 'SELECT email FROM ' . $rc->db->table_name('identities') . ' WHERE identity_id = ?'; + $q = $rc->db->query($sql, $r['iid']); + $rIid = $rc->db->fetch_assoc($q); + + $r['username'] = $rIid['email']; + } + + // Exactly the same in core SMTP handler + $args['smtp_user'] = str_replace('%u', $r['username'], $args['smtp_user']); + $args['smtp_pass'] = str_replace('%p', $rc->decrypt($r['password']), $args['smtp_pass']); } return $args;