Exacttarget Emails: how to dynamically attach multiple files?

2.9k Views Asked by At

I am doing research on how to use exact target to send out email attachments: (could be multiple)

http://help.exacttarget.com/en-US/documentation/exacttarget/content/email_attachments/

I couldn't hard code it as in the following instruction:

%%=AttachFile("Portfolio","Example1")=%%
%%=AttachFile("Portfolio","Example2")=%%
%%=AttachFile("Portfolio","Example3")=%%
%%=AttachFile("Portfolio","Example4")=%%
%%=AttachFile("Portfolio","Example5")=%%

because the number of attachments I need to send out varies.

Does anyone have any idea of how I can achieve this programmatically?

Thanks a lot!

3

There are 3 best solutions below

1
On

%%[IF Condition1 THEN]%%
%%=AttachFile("Portfolio","Example1")=%%

%%[ELSEIF Condition2 THEN]%%
%%=AttachFile("Portfolio","Example1")=%%
%%=AttachFile("Portfolio","Example2")=%%

%%[ELSEIF Condition3 THEN]%%
%%=AttachFile("Portfolio","Example3")=%%

%%[ELSEIF Condition4 THEN]%%
%%=AttachFile("Portfolio","Example3")=%%
%%=AttachFile("Portfolio","Example4")=%%

%%[ELSEIF Condition5 THEN]%%
%%=AttachFile("Portfolio","Example1")=%%
%%=AttachFile("Portfolio","Example5")=%%

%%[ELSE ENDIF]%%

0
On

Quick link to more in-depth info on Attachment function in AMPScript

Really the best way I can think of without more information would be to include fields like "Attachment 1", "Attachment 2", etc in your data extension and fill these with the location and then just include the location as a variable in the attachfile.

e.g. %%=AttachFile("Portfolio",[Attachment 1])=%%

and then just use conditional such as IF Not EMPTY() etc, to decide which functions should be executed.

This way you can edit the locations dynamically across each recipient as well as the number of times AttachFile is called. It is a bit messy and cumbersome, but again without more info this is the best I can do.

0
On

How do you know the number of attachments? Is this known pre-send or not until send-time? Is this a Triggered Send or User-Initiated?

Depending on these answers, you could achieve this in several ways.

The main solution path I would explore would be to do some sort of LookupRows() or LookupOrderedRows() to retrieve the files to be attached, then loop through those rows and attach the files. Also, it is important to note that AttachFile() functions need to executed in inline-AMP Script, %%=AttachFile()=%%, not in AMP code blocks, %%[ ...... ]%%.

So your code would probably look something like the following (I included an example of files hosted on an external site, the ET FTP, and the Portfolio):

%%[ VAR @sub, @fileDe, @files, @file, @fileUrl, @fileName, @fileExternalKey

    SET @sub = [_subscriberkey]
    SET @fileDE = "Name Data Extension holding attachment filename/url"

    /*LookupRows() from DE. Assumes data extension uses SubscriberKey as the foreign key*/
    SET @files = LookupRows(@fileDe,"SubscriberKey",@sub)

    /*Validate files were returned*/
    IF Rowcount(@files) > 0 THEN
        FOR @i = 1 TO Rowcount(@files) DO
            SET @file = Row(@files,@i)

            /*If you are using files hosted at an external HTTP/HTTPS location*/
            SET @fileUrl = Field(@file,"FileURL")
            ]%%
            %%=AttachFile("HTTP",@fileUrl)=%%
            %%[
            /*If you are using files hosted in the Import folder of the ExactTarget FTP*/
            SET @fileName = Field(@file,"FileName")
            ]%%
            %%=AttachFile("FTP",@fileName)=%%
            %%[
            /*If you are using files hosted in the ExactTarget Portfolio*/
            SET @fileName = Field(@file,"PortfolioFileExternalKey")
            ]%%
            %%=AttachFile("Portfolio",@fileExternalKey)=%%
            %%[
        NEXT @i
    ELSE
        /*No records returned for @files*/
    ENDIF
]%%