0

There is an old old thread about this and Salesforce hasn't being nice enough to extend their Python SDK (though the PHP and C# ones are way ahead).

I can create the data extension but I'm not able to retrieve a EmailSendDefinition, set the DataExtension and perform it.

Can anyone point me into the right direction to accomplish this?

Thank you

  • 1
    Unfortunately it's going to be a while before the Python SDK gets updated. Are you open to other SDKs or using SOAP/REST directly? – Timothy Jun 11 '19 at 16:42
  • 1
    Right now, I'm going to mix Python with PHP, it seems like the shortest path to get this done. – Luis B. Jun 11 '19 at 20:02
1

Solved by mixing Python and PHP... Thank God for Docker

subprocess.call([
                    'php', 
                    '../php/send_email_to_data_extension.php', 
                    '--sf_secret',            sf_client_secret,
                    '--sf_id',                sf_client_id,
                    '--email_definition_key', send_definition_template_key,
                    '--data_extension_key',   data_extension_key
                    ])
# send_email_to_data_extension.php
# Run the composer require stuff
<?php
require_once __DIR__ . "/vendor/autoload.php";
use FuelSdk\ET_Client;
use FuelSdk\ET_Email_SendDefinition;


class EmailToDataExtensionSender{
    public $salesforceClient;

    public function __construct($id, $secret){
        $this->salesforceClient = new ET_Client(true, false, ['clientid' => $id,'clientsecret' => $secret]);    
    }

    public function sendEmailToDataExtension($emailCustomerKey, $dataExtensionCustomerKey){
        # Look for the sendemail definition template
        $emailSendDefinition = new ET_Email_SendDefinition();
        $emailSendDefinition->authStub = $this->salesforceClient;
        $emailSendDefinition->props = ['Client.ID', 'CreatedDate','ModifiedDate','ObjectID','CustomerKey','Name','CategoryID','Description','SendClassification.CustomerKey','SenderProfile.CustomerKey','SenderProfile.FromName','SenderProfile.FromAddress','DeliveryProfile.CustomerKey','DeliveryProfile.SourceAddressType','DeliveryProfile.PrivateIP','DeliveryProfile.DomainType','DeliveryProfile.PrivateDomain','DeliveryProfile.HeaderSalutationSource','DeliveryProfile.FooterSalutationSource','SuppressTracking','IsSendLogging','Email.ID','BccEmail','AutoBccEmail','TestEmailAddr','EmailSubject','DynamicEmailSubject','IsMultipart','IsWrapped','SendLimit','SendWindowOpen','SendWindowClose','DeduplicateByEmail','ExclusionFilter','Additional'];
        $emailSendDefinition->filter = ['Property' => 'CustomerKey','SimpleOperator' => 'equals','Value' => $emailCustomerKey];
        $getResult = $emailSendDefinition->get();
        if($getResult->status && count($getResult->results) == 1){
            # Create a temporary copy of it and change the data extension for the desired one
            $emailSendDefinition->props["Name"] = $getResult->results[0]->Name . "_TMP";
            $emailSendDefinition->props["CustomerKey"] = $getResult->results[0]->CustomerKey . "_TMP";
            $emailSendDefinition->props["PartnerKey"] = $getResult->results[0]->PartnerKey;
            $emailSendDefinition->props["CategoryID"] = $getResult->results[0]->CategoryID;
            $emailSendDefinition->props["SendDefinitionList"] = ["CustomerKey"=> $dataExtensionCustomerKey, "DataSourceTypeID"=>"CustomObject"];
            $emailSendDefinition->props["SendClassification"] = ["CustomerKey" => $getResult->results[0]->SendClassification->CustomerKey];
            $emailSendDefinition->props["Email"] = (array)$getResult->results[0]->Email;
            $postResult = $emailSendDefinition->post();
            if ($postResult->status) { 
                # Perform the send
                $sendResult = $emailSendDefinition->send();
                if ($sendResult->status) { 
                    # Delete the temporary one afterwards
                    $emailSendDefinition->delete();
                } else {
                    throw new Exception("Unable to send using send definition due to:".print_r($sendResult,true));
                }
            } else {
                throw new Exception("Unable to create send definition due to: ".print_r($postResult,true));
            } 
        } else {
            throw new Exception("Unable to create send definition due to: ".print_r($getResult,true));
        }
    }
}


# Run forest run!
$longArgumentList = [
    "sf_id:", 
    "sf_secret:",
    "email_definition_key:",
    "data_extension_key:"
];
$opts = getopt("",$longArgumentList);
// var_dump($opts);

$sender = new EmailToDataExtensionSender($opts['sf_id'], $opts['sf_secret']);

$sender->sendEmailToDataExtension($opts['email_definition_key'], $opts['data_extension_key']);

Yeah

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.