<p>Using Craft Commerce's events, you can do pretty much anything to the emails before they are sent.</p>
<p>Recently, the good folks at P&amp;T added an event code generator system to their documentation page for events:</p>
<p><a href="https://craftcms.com/docs/commerce/3.x/extend/events.html" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/extend/events.html</a></p>
<p>As a rough sketch, you can set up en event listener in your plugin/modules init method, like this:</p>
<pre class="lang-php prettyprint-override"><code>    public function init(): void
    {

        parent::init();
        self::$instance = $this;

        $this-&gt;_setPluginComponents();

        Event::on(Emails::class, Emails::EVENT_BEFORE_SEND_MAIL, static function(MailEvent $e) {
            modify_email_function($e);
        });
    }
</code></pre>
<p>And then</p>
<pre class="lang-php prettyprint-override"><code>
   public function modify_email_function(MailEvent $event): void
    {
        $order = $event-&gt;order;
        $orderHistory = $event-&gt;orderHistory;
        $commerceEmail = $event-&gt;commerceEmail;
        
        ...your code here, whatever you like really.

   }
</code></pre>
<p>One thing to factor in to your thinking is quite how/when this function will be called - as Commerce's emails are generally sent via a queue job, which has some implications as to what is available to your code at that time (e.g. if you're using a queue runner that causes the queue jobs to be, in effect, run from the console, there will not be a valid user at that time, just as an example).</p>
<p>(There is a corresponding <code>EVENT_AFTER_SEND_EMAIL</code> of course, if you you need to set anything back etc.)</p>
<p>Docs for these events:</p>
<p><a href="https://craftcms.com/docs/commerce/3.x/extend/events.html#beforesendemail" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/extend/events.html#beforesendemail</a></p>
<p><a href="https://craftcms.com/docs/commerce/3.x/extend/events.html#aftersendemail" rel="nofollow noreferrer">https://craftcms.com/docs/commerce/3.x/extend/events.html#aftersendemail</a></p>

CC BY-SA 4.0
2022-02-02T11:23:35.410Z
Thanks a lot for the help ! Do you have any idea how I could alter the mailer configuration from there ? Like changing mailgun's config
Antoine
CC BY-SA 4.0
2022-02-03T05:22:41.390Z
Not immediately, but I'd first look at Craft's config service - https://docs.craftcms.com/api/v3/craft-services-config.html (e.g get config from file) - and then the mail adaptors all extend from: https://docs.craftcms.com/api/v3/craft-mail-transportadapters-basetransportadapter.html
Jeremy Daalder
CC BY-SA 4.0
2022-01-29T23:31:06.300Z
2022-01-29T23:31:06.300Z
Jeremy Daalder
<p>I have a multisite setup where 2 sites can make orders on the same commerce, but I would like to have dynamic mail settings (domain name, origin, api keys, etc) based on the site of the order.</p>
<p>Do you know if it's feasable ? Intercepting a mail event maybe ?</p>

<plugin-craftcommerce><emails>
Craft Commerce multisite mail origin
# Craft Commerce multisite mail origin

(Asked by: username_0 on 2022-01-28)

I have a multisite setup where 2 sites can make orders on the same commerce, but I would like to have dynamic mail settings (domain name, origin, api keys, etc) based on the site of the order.

Do you know if it's feasable ? Intercepting a mail event maybe ?

**Tags:** plugin-craftcommerce, emails


---

## 1 answers

### Answer by username_1 on 2022-01-29. Score: 2 | Accepted answer

Using Craft Commerce's events, you can do pretty much anything to the emails before they are sent.
Recently, the good folks at P&T added an event code generator system to their documentation page for events:
https://craftcms.com/docs/commerce/3.x/extend/events.html
As a rough sketch, you can set up en event listener in your plugin/modules init method, like this:
    public function init(): void
    {

        parent::init();
        self::$instance = $this;

        $this->_setPluginComponents();

        Event::on(Emails::class, Emails::EVENT_BEFORE_SEND_MAIL, static function(MailEvent $e) {
            modify_email_function($e);
        });
    }

And then

   public function modify_email_function(MailEvent $event): void
    {
        $order = $event->order;
        $orderHistory = $event->orderHistory;
        $commerceEmail = $event->commerceEmail;
        
        ...your code here, whatever you like really.

   }

One thing to factor in to your thinking is quite how/when this function will be called - as Commerce's emails are generally sent via a queue job, which has some implications as to what is available to your code at that time (e.g. if you're using a queue runner that causes the queue jobs to be, in effect, run from the console, there will not be a valid user at that time, just as an example).
(There is a corresponding EVENT_AFTER_SEND_EMAIL of course, if you you need to set anything back etc.)
Docs for these events:
https://craftcms.com/docs/commerce/3.x/extend/events.html#beforesendemail
https://craftcms.com/docs/commerce/3.x/extend/events.html#aftersendemail

> **Comments:**
> **username_0**: Thanks a lot for the help ! Do you have any idea how I could alter the mailer configuration from there ? Like changing mailgun's config  
> **username_1**: Not immediately, but I'd first look at Craft's config service - https://docs.craftcms.com/api/v3/craft-services-config.html (e.g get config from file) - and then the mail adaptors all extend from: https://docs.craftcms.com/api/v3/craft-mail-transportadapters-basetransportadapter.html  

---

