The Laravel 5.3 Mailer class is configured as a singleton according to this article https://laravel-news.com/allowing-users-to-send-email-with-their-own-smtp-settings-in-laravel
Let's assume a user in a requests sets Mail::setSwiftMailer to a different server in order to send mails via his mailserver/account.
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
Does this affect other users? As far as I understood, the usual singleton characteristics do not apply in PHP http://blog.gordon-oheim.biz/2011-01-17-Why-Singletons-have-no-use-in-PHP/
A Singleton created in one Request lives for exactly that request. A Singleton created in another Request done at the same time will still be a completely different instance. And it will occupy it’s own memory. Those instances are not linked to each other. They are completely isolated, because PHP is a Shared-Nothing architecture. You do not have one single unique instance, but many similar instances in parallel processes.
So does changing the mailserver for the singleton function change the mailserver for other users?
Does changing the Laravel config affect the other users? I assume at least here the answer is yes.
Config::set('mail', ['driver' => $mail->driver, 'host' => $mail->host, 'port' => $mail->port, ...]);
Have a look at the most simple Singleton implementation:
class Singleton {
protected static $_instance = null;
public static function newInstance()
{
if (null === self::$_instance)
{
self::$_instance = new self;
}
return self::$_instance;
}
// disallowed
protected function __clone() {}
// disallowed
protected function __construct() {}
}
It makes sure that only one instance of this exists within this one and single request.
So your Swift_SmtpTransport is a Singleton:
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
This means that every instance of Swift_SmtpTransport is the one you have just created (for this request) (this does not mean that you can not change the properties - maybe the instance has getters and setters to be called after it has been created).
And now lets see what you have already found out:
A Singleton created in one Request lives for exactly that request.
So it is per request and the request does also not affect other requests. So no, I do not think that other users are affected. Not even if you change the config as per single request.
For more information also read: Simultaneous Requests to PHP Script - for each request a new independent process is forked which does not have anything to do with the others. The only thing is that they may block each other.
The documentation of Laravel says:
Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.