Answers.Body: <p>The answer seems to be to drop the mailer component modification completely.</p>
  <p>There's a Yii method <code>setTransport()</code> which can be called via Commerce's <code>EVENT_BEFORE_SEND_MAIL</code> event.</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(
      Emails::class,
      Emails::EVENT_BEFORE_SEND_MAIL,
      function(MailEvent $event) {
          $order = $event-&gt;order;
  
          $orderSite = $order-&gt;orderSite-&gt;handle;
  
          if ($orderSite === 'siteB') {
  
              $craftEmail = $event-&gt;craftEmail;
              
              $craftEmail-&gt;setFrom([ 
                  App::env('EMAIL_FROM_EMAIL_B') =&gt; App::env('EMAIL_FROM_NAME_B')
              ]);
  
              $settings = App::mailSettings();
              $transportSettings = $settings['transportSettings'];
  
              $config = [
                  'scheme'   =&gt; 'smtp',
                  'host'     =&gt; $transportSettings['host'],
                  'port'     =&gt; $transportSettings['port'],
                  'username' =&gt; App::env('EMAIL_USERNAME_B'),
                  'password' =&gt; App::env('EMAIL_PASSWORD_B'),
              ];
  
              Craft::$app-&gt;mailer-&gt;setTransport($config);
          }
      }
  );
  </code></pre>
  <p>Possibly necessary to set the transport back to default by using the <code>EVENT_AFTER_SEND_MAIL</code> event too.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-17T19:31:39.900Z
Answers.Text: You should be able to access the Mailer instance that created the email by doing `$event->craftEmail->mailer`, which means you should be able to do `$event->craftEmail->mailer->setTransport(...`, which might mean you could change the mailer config temporarily for just this email and not have to bother with resetting the config?
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-17T13:25:33.387Z
Answers.LastActivityDate: 2024-05-17T13:25:33.387Z
Answers.UserDisplayName: Jamie
Body: <p>I've been trying to send Commerce emails via two different domains but I'm having very little luck getting it working fully.</p>
  <p>In app.php I have something like this:</p>
  <pre class="lang-php prettyprint-override"><code>'mailer' =&gt; function() {
    $settings = App::mailSettings();
    
    $orderId = Craft::$app-&gt;request-&gt;getQueryParam('orderId');
    $order = Order::find()-&gt;id($orderId)-&gt;one();
    $siteHandle = $order-&gt;orderSite-&gt;handle;
  
    if ($siteHandle === 'siteB') {
      $transportSettings = $settings['transportSettings'];
  
      $settings-&gt;fromEmail = App::env('EMAIL_FROM_EMAIL_B');
      $settings-&gt;fromName  = App::env('EMAIL_FROM_NAME_B');
  
      $settings-&gt;transportType = $settings['transportType'];
      $settings-&gt;transportSettings = [
        'username'          =&gt; App::env('EMAIL_USERNAME_B'),
        'password'          =&gt; App::env('EMAIL_PASSWORD_B'),
        'host'              =&gt; $transportSettings['host'],
        'port'              =&gt; $transportSettings['port'],
        'useAuthentication' =&gt; $transportSettings['useAuthentication'],
      ];
    }
    
    $config = App::mailerConfig($settings);
    return Craft::createObject($config);
  }
  </code></pre>
  <p>This does work but only when emails are sent via web requests. Once they're sent via the queue then obviously Craft::$app-&gt;request no longer exists.</p>
  <p>I've not sure how to get the context of the email or the queue job at this point.</p>
  
Tags: <plugin-craftcommerce><emails><craft4><commerce4>
Title: Change email transport settings based on order site
ThreadText: # Change email transport settings based on order site
  
  (Asked by: username_0 on 2024-05-15)
  
  I've been trying to send Commerce emails via two different domains but I'm having very little luck getting it working fully.
  
  In app.php I have something like this:
  
  ```php
  'mailer' => function() {
    $settings = App::mailSettings();
    
    $orderId = Craft::$app->request->getQueryParam('orderId');
    $order = Order::find()->id($orderId)->one();
    $siteHandle = $order->orderSite->handle;
  
    if ($siteHandle === 'siteB') {
      $transportSettings = $settings['transportSettings'];
  
      $settings->fromEmail = App::env('EMAIL_FROM_EMAIL_B');
      $settings->fromName  = App::env('EMAIL_FROM_NAME_B');
  
      $settings->transportType = $settings['transportType'];
      $settings->transportSettings = [
        'username'          => App::env('EMAIL_USERNAME_B'),
        'password'          => App::env('EMAIL_PASSWORD_B'),
        'host'              => $transportSettings['host'],
        'port'              => $transportSettings['port'],
        'useAuthentication' => $transportSettings['useAuthentication'],
      ];
    }
    
    $config = App::mailerConfig($settings);
    return Craft::createObject($config);
  }
  
  ```
  
  This does work but only when emails are sent via web requests. Once they're sent via the queue then obviously Craft::$app->request no longer exists.
  
  I've not sure how to get the context of the email or the queue job at this point.
  
  **Tags:** plugin-craftcommerce, emails, craft4, commerce4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-05-17. Score: 1
  
  The answer seems to be to drop the mailer component modification completely.
  
  There's a Yii method `setTransport()` which can be called via Commerce's `EVENT_BEFORE_SEND_MAIL` event.
  
  ```php
  Event::on(
      Emails::class,
      Emails::EVENT_BEFORE_SEND_MAIL,
      function(MailEvent $event) {
          $order = $event->order;
  
          $orderSite = $order->orderSite->handle;
  
          if ($orderSite === 'siteB') {
  
              $craftEmail = $event->craftEmail;
              
              $craftEmail->setFrom([ 
                  App::env('EMAIL_FROM_EMAIL_B') => App::env('EMAIL_FROM_NAME_B')
              ]);
  
              $settings = App::mailSettings();
              $transportSettings = $settings['transportSettings'];
  
              $config = [
                  'scheme'   => 'smtp',
                  'host'     => $transportSettings['host'],
                  'port'     => $transportSettings['port'],
                  'username' => App::env('EMAIL_USERNAME_B'),
                  'password' => App::env('EMAIL_PASSWORD_B'),
              ];
  
              Craft::$app->mailer->setTransport($config);
          }
      }
  );
  
  ```
  
  Possibly necessary to set the transport back to default by using the `EVENT_AFTER_SEND_MAIL` event too.
  
  > **Comments:**
  > **username_1**: You should be able to access the Mailer instance that created the email by doing `$event->craftEmail->mailer`, which means you should be able to do `$event->craftEmail->mailer->setTransport(...`, which might mean you could change the mailer config temporarily for just this email and not have to bother with resetting the config?  
  
  ---
  
  