title: design pattern for switching email providers in the code
body: <p>We need to send emails in our php app (who doesn't). Initially when our app was in infancy, we used simply linux sendmail. 
  A bit moving forward we switched to our own SMTP server. That means code change in every file that has email related function.
  A year later we shifted to AWS and had to change the code again to start using AWS email service.
  Now we moved to Google cloud and again email code is changed to use some third party provider.</p>
  
  <p>Configuration of email, is spread all over the places and changing one provider means hundreds of files need to be updated and if you miss one, clients may not be able to receive email for one part but can get emails for other.</p>
  
  <p>I just took a step back and realized that we are changing code that makes no sense to change only because our email provider is changed.</p>
  
  <p>But for the life of me I am unable to figure out what would be a way out of this mess.</p>
  
  <p>All I need to do is to remove emailing from my code and refactor it in such a way that my app code can function independent of the mail service provider.</p>
  
  <p>Her is my take on this </p>
  
  <pre><code>class EmailGateway()
  {
    private $emailer;
    public function __construct($someEmailProvider)
    {
       $this-&gt;emailer = $someEmailProvider;
    }
  
    public function send($from, $to, $subject, $bodyText, $bodyHtml="")
    {
  
      this-&gt;emailer-&gt;send($from, $to, $subject, $bodyText, $bodyHtml="");
    }
  }
  </code></pre>
  
  <p>And then in my code all I need is to call it like </p>
  
  <pre><code># Gmail?
  $mailGateway = new EmailGateway(new GmailEmailer("username", "password"));
  
  # local?
  $mailGateway = new EmailGateway(new SendMailer());
  
  # SMTP?
  $mailGateway = new EmailGateway(new MyMailServerMailer("192.168.0.3", "no_user", "secret_password"));
  </code></pre>
  
  <p>In my app code, I can even go one step further and calla a factory to get the current default email provider</p>
  
  <pre><code>class EmailService()
  {
     public static function currentProvider(): EmailProviderInterface
     {
  
        return new MyMailServerMailer("192.168.0.3", "no_user", "secret_password");
  
     }
  }
  </code></pre>
  
  <p>That will make my above caller code even simpler one liner</p>
  
  <pre><code># SMTP?/GMAIL?/Local?/Whatever
  $mailGateway = new EmailGateway(EmailService::currentProvider());
  </code></pre>
  
  <p>So whenever I need to change the provider, all I will do is change the guts of currentProvider() and I am good to go.</p>
  
  <p>Am I doing it the right way?
  Is it a proper strategy pattern at all?
  Should I even care about what pattern it is as long as it can solve my problem?</p>
  
  <p>Is there a better way to pull myself out of this ever increasing mess?</p>