The Salesforce Developers website will undergo maintenance on May 29, 2024 from 3:00 a.m. UTC to 10:00 a.m. UTC. The maintenance process may affect the availability of our documentation. Please plan accordingly.

AttachFile()

Attaches a file to an outgoing email message. You can optionally configure this function to include a link to the file when the email is viewed as a web page.

Before you use this function, you must enable AMPscript email attachments. Contact your Salesforce account executive for more information about enabling this capability.

You can also contact your account executive to adjust the error threshold that causes the send job to stop prematurely. This feature helps prevent a single error from stopping the entire job.

The AttachFile() function has eight parameters:

  • fileLocationType (string): Required. The type of location to pull the file from. Accepted values: http, ftp, portfolio, contentbuilder.
  • fileLocation (string): Required. The location to pull the file from. This parameter can contain a maximum of 2088 characters. If you specify http for the first parameter, this parameter must contain a URL. If you specify ftp for the first parameter, this parameter must contain the name of a file in the Import folder of your Enhanced FTP site. If you specify portfolio or contentbuilder for the first parameter, this parameter must contain the external key of the file to attach.
  • attachmentFileName (string): The name assigned to a file when it’s attached to an email message. If you don't specify a new name, the function uses the original file name. If you specify http for the first parameter and don't provide a value for this parameter, the function uses the Content-Disposition information from the HTTP header. If the server providing the file doesn’t provide Content-Disposition information, the function uses an auto-generated value.
  • boolViewOnWeb (boolean): If true, a link to the file is included when a recipient selects the "View as a Web Page" link in the email. If false, the link is omitted. You can only use this parameter if the value of the first parameter is http.
  • viewOnWebLocation (string): The URL to use when including a link to the file in the "View as a Web Page" context. If the value of boolViewOnWeb is true, you must provide a value for this parameter. You can only use this parameter if the value of the first parameter is http.
  • viewOnWebFileName (string): The file name to use when including a link to the file in the "View as a Web Page" context. You can only use this parameter if the value of the first parameter is http.
  • viewOnWebDuration (number): The number of days the link appears in the "View as a Web Page" context. You can only use this parameter if the value of the first parameter is http.
  • boolContentDispositionAttachment (boolean): If true, the function changes the value of the content-disposition header for the attachment to attachment. If false, the value of this header is set to inline.

You can use the AttachFile() function to attach the types of files listed in this table to your emails.

File TypeMIME Type
docapplication/msword
pdfapplication/pdf
rtfapplication/rtf
pkpassapplication/vnd.apple.pkpass
xlsapplication/vnd.ms-excel
pptapplication/vnd.ms-powerpoint
docmapplication/vnd.ms-word.document.macroEnabled.12
pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation
xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
rarapplication/x-rar-compressed
icsapplication/ics
xmlapplication/xml
zipapplication/x-zip-compressed
zipapplication/zip
wavaudio/wav
wavaudio/x-wav
gifimage/gif
jpegimage/jpeg
jpgimage/jpeg
jpgimage/jpg
pngimage/png
pngimage/x-png
tifimage/tiff
tiffimage/tiff
icstext/calendar
csvtext/csv
htmtext/html
htmltext/html
txttext/plain
rtftext/rtf
vcftext/vcard
vcftext/x-vcard
xmltext/xml
mp4video/mp4

To use this function, specify the type of location where the file is stored, and the location or external key of the file. You can optionally specify additional parameters.

This function supports secure transmission over HTTPS. If the remote server doesn’t respond to the AttachFile() call within 30 seconds, the send is rescheduled for 15 minutes later.

You can only attach files contained in the account used to send the email message. This function doesn’t support shared content from Content Builder or Portfolio.

This example attaches a PDF document from a website to your outgoing email. In this example, a link to the file doesn’t appear when the recipient clicks the "View as a Web Page" link in the email.

This example attaches a Word document from the Import folder of the Enhanced FTP site.

This example attaches a PDF file from a website and gives the file a new name. The specified name is shown to the recipient of the email.

This example attaches a PDF file from a website, gives it a new name, and sets the value of the boolViewOnWeb parameter to true. When this parameter is true, you must also specify the URL that you want to link to in the "View as a Web Page" context. The URL that you specify can be different from the URL of the file that is attached to the email. You must also specify the link text to include.

The final parameter indicates that the link only remains active in the "View as a Web Page" context for four days.