Python and SOAP to Batch Import Delivery Templates
Any ideas why this returns with no errors, yet delivery templates are not created anywhere?
I'm trying to batch import my emails (htm files) from a previous ESP into Adobe Campaign as Delivery Templates. I created a new I/O API project and confirmed I can get that to work with single pushes from our Stensul application into Adobe Campaign as Delivery Templates. So I created a second I/O API Project and tried doing the following, and it seems to all run smoothly no errors output, but I do not see any Delivery Templates created.
The folder ID for <folder _operation="none" name="1186"/> I am using is one I got from our instance that looks like: domain-for-adobe.com/acc/folders/folder/1186/nmsDeliveryModel
import os
import requests
import time
import glob
from lxml import etree
# Adobe OAuth Credentials
ACCESS_TOKEN = "AT"
SERVER_URL = 'domain.com/nl/jsp/soaprouter.jsp'
# mcapi Session Token (use this instead of generating it)
SESSION_TOKEN = "ST"
# Folder path for *.htm files
FOLDER_PATH = os.path.expanduser('/Users/my_computer/Desktop/files_folder')
# Function to push HTML as delivery template
def push_html(file_path):
file_name = os.path.basename(file_path)
with open(file_path, 'r', encoding='utf-8') as file:
html_content = file.read()
subject = f"Template for {file_name}"
template_name = file_name.replace('.htm', '')
folder_name = 'Delivery Templates'
delivery_operation = 'insertOrUpdate'
body = f"""
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:xtk:session">
<soapenv:Header/>
<soapenv:Body>
<urn:Write>
<urn:sessiontoken>{SESSION_TOKEN}</urn:sessiontoken>
<urn:domDoc>
<delivery _operation="{delivery_operation}" xtkschema="nms:delivery"
internalName="{template_name}" deliveryMode="4" messageType="0"
isModel="1" label="{template_name}">
<mailParameters>
<subject><![CDATA[{subject}]]></subject>
</mailParameters>
<content>
<html>
<source><![CDATA[{html_content}]]></source>
</html>
<text>
<source><![CDATA[Text version not provided]]></source>
</text>
</content>
<tracking enabled="false" openEnabled="false"/>
<advancedParameters forceCodepage="false"/>
<folderProcess _operation="none" name="nmsRootDelivery"/>
<folder _operation="none" name="1186"/>
</delivery>
</urn:domDoc>
</urn:Write>
</soapenv:Body>
</soapenv:Envelope>
"""
headers = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Content-Type': 'text/xml; charset=UTF-8',
'SOAPAction': 'urn:xtk:session#Write'
}
# ✅ Encode the body to UTF-8
encoded_body = body.encode('utf-8')
response = requests.post(SERVER_URL, data=encoded_body, headers=headers)
if response.status_code == 200:
print(f"✅ Successfully imported {file_name}")
else:
print(f"❌ Failed to import {file_name}: {response.content.decode('utf-8', errors='ignore')}")
# Main Function
def main():
try:
# Get all .htm files recursively
files = glob.glob(os.path.join(FOLDER_PATH, '**', '*.htm'), recursive=True)
if not files:
print("⚠️ No .htm files found in the specified folder.")
return
for file_path in files:
print(f"🚀 Importing {file_path}...")
push_html(file_path)
time.sleep(1) # Adding a small delay to avoid API throttling
print("\n✅ All files imported successfully.")
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
main()
