Skip to main content

Company auf Basis des Empfängers setzen

Wenn auf Grund der Empfängeradresse der Mandant gesetzt werden soll, kann der folgende UserExit verwendet werden:

<?php

use Squeeze\xDoc;
use Squeeze\Email\EmailFile;
use Squeeze\xReturnObject;

function AfterEmailImport(xDoc $xDoc, EmailFile $emailFile = null)
{
    // Define the valid Email addresses and the assigned company
    $addressMapping = array();
    $addressMapping['mailbox01@company.net'] = '1000';
    $addressMapping['mailbox02@company.net'] = '2000';

    // Loop over all Email addresses
    foreach ($emailFile->getTo() as $address) {

        // Get the current Email address
        $currentAddress = mb_strtolower($address->getEmailAddress());

        // Check if the Email address is defined
        if(isset($addressMapping[$currentAddress])) {

            // Get the assigned company
            $assignedCompany = $addressMapping[$currentAddress];

            // Set the field value for Company
            if($xDoc->getFieldByName("Company") != null) {
                $xDoc->getFieldByName("Company")->getValue()->setValueAndText($assignedCompany);
            }

            // Set the field value for used Email address
            if($xDoc->getFieldByName("EmailToAddress") != null) {
                $xDoc->getFieldByName("EmailToAddress")->getValue()->setValueAndText($currentAddress);
            }

            // Optional store the additionalInfo
            $additionalInfo = $xDoc->loadAdditionalInfo();
            $additionalInfo['additionalInfo']['Company'] = $assignedCompany;
            $additionalInfo['additionalInfo']['EmailToAddress'] = $currentAddress;
            $xDoc->storeAdditionalInfo($additionalInfo);

            // break the loop so only the first valid address is used
            break;
        }
    }
    return new xReturnObject(true, 200, 'UserExit processed', null);
}