Selecting delivery server for transactional email

I've tried some settings, but cannot find out how to do so:

For a specific client, with a specific domain let's say 'clientdomain.com', we've got specific transactional emails.
It is needed to sent these specific transactional emails over a specific delivery server. Other email should sent over the 'standard' delivery service for that customer.
We sent transactional email using the 2.0 API

As far as I can see there is no option to select a specific delivery server through the API call.

How can we achieve the above so that, for a selection of (transactional) emails, we can select a specific delivery server?
 
Doesnt it work to create a delivery server only for transactional emails, and assign that server to that customer, so all transactional emails for that customer go out using that server?
 
Maybe it does, but it is needed to only sent this specific (transactional) mails over a specific delivery server.
The case is: invoices need to be sent over a more secure (and expensive) environment, others can use the regular delivery server

For these invoices (also for other transactional emails) we use the transactional email API
Only uniqueness is the domain which is sent from, this domain differs from our other sendings.

Can we set the selection of a delivery server purely based on the mailFrom domain used (so that MailWizz sents all email with a specific domain used in the mailFrom from that server while we exclude it in the other delivery servers) OR as a setting while sending the email through the (transactional)API?
 
Is there something I can do in the code, or something else what realizes what we are looking for?

It is important for us to stay within the customer environment and that this specific transactional email is being sent over a specific SMTP server:
Only uniqueness is the domain which is sent from, this domain differs from our other sendings.

- Can we select on the mailFrom variable to switch sending over a specific delivery server?
- Can we push the desired delivery server to send with the API call?
- Can we put something in the email content (hidden) which can be used to select the correct delivery server?
- maybe even some manually altered code in one of the php files?
 
I am glad you asked, if you can do it programatically, then you can add a code like below in apps/init-custom.php:
PHP:
hooks()->addFilter('delivery_servers_before_pick_server', function(?DeliveryServer $server = null, CAttributeCollection $hookParams) {
    
    $targetCustomerId = 12345; // change to the right customer id
    $targetServerId = 2222; // change to the right server id
    
    if (
        $hookParams->deliveryObject instanceof TransactionalEmail &&
        $hookParams->deliveryObject->customer_id === $targetCustomerId
    ) {
        $server = DeliveryServer::model()->findByPk($targetServerId);
    }
    
    return $server;
});
In the above code, $hookParams->deliveryObject is an instance of TransactionalEmail model(see /apps/common/models/TransactionalEmail.php), where you have access to all the transactional email params to make a decision if you have to load that delivery server or not.
 
Hi Twisted,

thank you for this information!
I've created the 'init-custom.php' in the /apps folder holding the code above.

Can you inform me how I can use this within my API call? This is currently something like:
Code:
            $endpoint = MailWizz::endpoint(\EmsApi\Endpoint\TransactionalEmails::class);

            $payload = [
                'to_name'    => $toName,
                'to_email'   => $toEmail,
                'from_name'  => self::FROM_NAME,
                'from_email' => self::FROM_EMAIL,
                'subject'    => $subject,
                'body'       => $htmlBody,
                'send_at'    => gmdate('Y-m-d H:i:s'),
            ];

            if ($plainText !== '') {
                $payload['plain_text'] = $plainText;
            }

            if (!empty($attachments)) {
                $payload['attachments'] = $attachments;
            }

            $resp = $endpoint->create($payload);

$htmlBody is being received using a template from MailWizz (received with an API call).
Or do I need to alter something in MailWizz directly?

Good to know:
This customer, for which we use the API call, uses transactional email sending for multiple emails. Only a specific email should be sent over a specific delivery server (being: 6).
Is it needed to create a second customer - which is basically the same - with an own API key?
It would be best, for statistics and sending limits, to stay with one and the same customer (just switching the deliveryServer based on a setting in the API call OR - for example - with a useful setting/filter on mailFrom in the deliveryServer settings).
 
Last edited:
Can you inform me how I can use this within my API call?
You don't have to change your API at all, the code you placed in the init-custom file is what matters, but that code has to be the right one that selects the delivery server, maybe match on the subject if you want to only send for a particular email.
PHP:
hooks()->addFilter('delivery_servers_before_pick_server', function(?DeliveryServer $server = null, CAttributeCollection $hookParams) {
    
    $targetCustomerId = 12345; // change to the right customer id
    $targetServerId = 6; // change to the right server id
    
    if (
        $hookParams->deliveryObject instanceof TransactionalEmail &&
        $hookParams->deliveryObject->customer_id === $targetCustomerId &&
        $hookParams->deliveryObject->subject == 'Your email subject here'
    ) {
        $server = DeliveryServer::model()->findByPk($targetServerId);
    }
    
    return $server;
});
 
Hi Twisted,

The subjectline has got variables in it, also the mailFrom holds variables.
Can we match on the domain in the from_email, something like:


Code:
hooks()->addFilter('delivery_servers_before_pick_server', function(?DeliveryServer $server = null, CAttributeCollection $hookParams) {
   
    $targetCustomerId = 12345; // change to the right customer id
    $targetServerId = 6; // change to the right server id
   
    if (
        $hookParams->deliveryObject instanceof TransactionalEmail &&
        $hookParams->deliveryObject->customer_id === $targetCustomerId &&
        $hookParams->deliveryObject->mail_from == '%mydomain.nl'
    ) {
        $server = DeliveryServer::model()->findByPk($targetServerId);
    }
   
    return $server;
});
 
Last edited:
Back
Top