Skip to content

Instantly share code, notes, and snippets.

@kimbtech
Last active September 3, 2022 13:13
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimbtech/b6b08f1778420766ee1a2d24117d4871 to your computer and use it in GitHub Desktop.
Save kimbtech/b6b08f1778420766ee1a2d24117d4871 to your computer and use it in GitHub Desktop.
Roundcube multiple SMTP Server plugin for setups having multiple IMAP Server.

This plugin is not needed for versions >= 1.5 of roundcube, as the functionality was added by the developers!

Roundcube allows to access multiple IMAP Server, but all mails have to be send over the same SMTP server. This plugin allows to add multiple SMTP Server (one per IMAP Server) to Roundcube.

// normal declaration of multiple IMAP Server
$config['default_host'] = array(
		'ssl://imap.example.de' => '.de',
		'ssl://mx.example.com' => '.com'
	);

// normal SMTP declaration
$config['smtp_server'] = 'tls://smtp.example.de' // unable to add the SMTP Server for mx.example.com

// new SMTP declaration using the plugin
$config['smtp_server'] = array(
	'imap.example.de' => 'tls://smtp.example.de',
	'mx.example.com' => 'ssl://mx.example.de'
	// IMAP Hostname => SMTP Hostname
	//	(if IMAP Hostname is not in list, we use first SMTP Server)
);

//	and activate the plugin by adding to:
$config['plugins'] = array( ....., 'multi_smtp');

Install

Place the file multi_smtp.php in your installation at /plugins/multi_smtp/multi_smtp.php, edit the config.inc.php.

<?php
class multi_smtp extends rcube_plugin {
public $task = 'mail';
function init(){
if ( version_compare( RCMAIL_VERSION, '1.5', '<') ){ // plugin only needed until version 1.4.x
$this->add_hook('smtp_connect', array($this, 'correct_smtp_server'));
}
}
function correct_smtp_server( $data ){
$imap_server = $_SESSION['storage_host'];
$data['smtp_server'] = isset($data['smtp_server'][$imap_server]) ? $data['smtp_server'][$imap_server] : array_values($data['smtp_server'])[0];
//print_r($data);
return $data;
}
}
?>
@N0ury
Copy link

N0ury commented Sep 12, 2021

Nice plugin. Very simple and it works fine.

@N0ury
Copy link

N0ury commented Nov 24, 2021

Hi,
It doesn't work anymore with php 8.1

[23-Nov-2021 14:02:56 Europe/Berlin] PHP Fatal error:  Uncaught TypeError: array_values(): Argument #1 ($array) must be of type array, string given in /usr/share/roundcube/plugins/multi_smtp/multi_smtp.php:12
Stack trace:
#0 /usr/share/roundcube/plugins/multi_smtp/multi_smtp.php(12): array_values()
#1 /usr/share/roundcube/program/lib/Roundcube/rcube_plugin_api.php(513): multi_smtp->correct_smtp_server()
#2 /usr/share/roundcube/program/lib/Roundcube/rcube_smtp.php(76): rcube_plugin_api->exec_hook()
#3 /usr/share/roundcube/program/lib/Roundcube/rcube.php(284): rcube_smtp->connect()
#4 /usr/share/roundcube/program/lib/Roundcube/rcube.php(1763): rcube->smtp_init()
#5 /usr/share/roundcube/program/include/rcmail_sendmail.php(408): rcube->deliver_message()
#6 /usr/share/roundcube/program/actions/mail/send.php(244): rcmail_sendmail->deliver_message()
#7 /usr/share/roundcube/program/include/rcmail.php(275): rcmail_action_mail_send->run()
#8 /usr/share/roundcube/index.php(283): rcmail->action_handler()
#9 /var/lib/roundcube/public_html/index.php(26): include('...')
#10 {main}
  thrown in /usr/share/roundcube/plugins/multi_smtp/multi_smtp.php on line 12

Please help, I'm not a php dev

@kimbtech
Copy link
Author

Hi,
I just took a look the error and I think this plugin is not needed any more, as the developers of roundcube added the functionality in roundcube itself starting with version 1.5.0 of roundcube (added here https://github.com/roundcube/roundcubemail/blob/1.5.0/program/lib/Roundcube/rcube_smtp.php#L60).

So just delete the folder plugins/multi_smtp and remove multi_smtp from $config['plugins'] if you are on version 1.5.0 or higher.

Hope this helps

@N0ury
Copy link

N0ury commented Nov 25, 2021

@kimbtech thanks for this info.
I'm using 1.5.0
I've just tried and it works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment