SFMC (EXACTTARGET) AMPSCRIPT SNIPPETS

We use these all the time!

Pull in Date

Current Year
%%=Format(Now(), "yyyy")=%%

Date that will update continually
%%=Format(Now(), "MM/dd/YYYY")=%%

Date that is Hard Set from Time of Send
%%=Format(Now(true), "MM/dd/YYYY")=%%

View online link

<a href="%%view_email_url%%">View Online</a>

Casing

%%=ProperCase(@variable)=%%
%%=UpperCase(@variable)=%%
%%=LowerCase(@variable)=%%

Redirect for concatenated or variable links

%%[set @variable = "http://tabletrtd.com/"]%%
%%=RedirectTo(@variable)=%%

System level variables

view online = %%view_email_url%%
email address = %%emailaddr%%
forward to a friend = %%ftaf_url%%
unsubscribe = %%unsub_center_url%%
profile center = %%profile_center_url%%
subscription center = %%subscription_center_url%%
sender reply name = %%replyname%%
sender reply email address = %%replyemailaddress%%
business name = %%Member_Busname%%
business address = %%Member_Addr%%
business city = %%Member_City%%
business state = %%Member_State%%
business postal code = %%Member_PostalCode%%
business country = %%Member_Country%%
<a href="%%profile_center_url%%" alias="Update Profile">Update Profile</a>

<a href="%%unsub_center_url%%" alias="Unsubscribe">Unsubscribe</a>

Date plus Days

%%=Format(DateAdd(Now(true), "14", "D"), "MM/dd/yyyy")=%%

Hiding content on web

%%[if _messagecontext != "vawp" then]%%
content hidden on web
%%[endif]%%

Hiding content on forward to a friend

%%[if _messagecontext != "ftaf" then]%%
content hidden on forward to a friend
%%[endif]%%

Format Date

%%[set @variable = Format(Now(),"MM/dd/YYYY")]%%

Formatting a phone number

%%=Format(@variable,"###-###-####")=%%

Formatting a currency amount

%%=Format(@variable,"$#,#.00;-$#,#.00")=%%

Micro base URL

%%microsite_base_url[default]UniqueID[/default]%%

Variable for Subject

%%=v(@subjectline)=%%

Raise Error

%%[else RaiseError("Write Error Sentence Here", true) endif]%%

Find a word in a Field

%%[if IndexOf(@variable,"this") > 0 then
Write set Statement Here
endif]%%

Targeting a specific domain

if domain(emailaddr) == “domain.com" then

Replace

%%[set @variable = Replace(variable,"old","new")]%%

First Name in variable subject line

%%[
set @firstname = ProperCase(firstname)

/*Is there a First Name in the Subjectline? Y or N*/
set @firstNameVersion = "N"

if empty(firstname) or @firstNameVersion == "N" then
set @subjectline = "TBD"
else
set @subjectline = concat(@firstname,", TBD")
endif
]%%

Still needs work

variable subjectline
if reciptype == "proof" then
Format(@variable, "dddd, MMMM d")
Format(@variable, "yyyyMMdd")
Format(@variable, "h:mm tt")
Format(@variable, "HHmmss")
https://help.exacttarget.com/en-US/documentation/exacttarget/content/ampscript/ampscript_syntax_guide/#section_30
https://help.exacttarget.com/en-US/documentation/exacttarget/content/ampscript/ampscript_syntax_guide/utilities_ampscript_functions/#section_36

EMAIL CLIENT TARGETING

Use these hacks to target specific Email Clients. Thanks to Kevin Mandeville for this info.

outlook.com

[owa] .name {css goes here}

gmail

.not-gmail {css goes here}
<div style="width:0;" class="not-gmail">content goes here</div>

gmail.com

* [summary~="name"] {css goes here}
<div summary="name">content goes here</div>

webkit

@media screen and (-webkit-min-device-pixel-ratio: 0) {.name {css goes here}}

aol mail

.★:not(#★) {css goes here}
<div class="★" id="★">content goes here</div>

thunderbird

.moz-text-html .name {css goes here}

samsung

#secdiv .name {css goes here}
#messageWebViewDiv .name {css goes here}

lotus notes 8

.notes.name {css goes here}

yahoo

@media yahoo {.name {css goes here}}

BACKGROUND COLOR ON ENTIRE EMAIL

To have full coverage using a background color, the background color needs to be in three places: a bgcolor on the body tag, a style background-color on the body tag, and a bgcolor on the containing table. The reasons are listed below.

bgcolor=”#eeeeee” on <body>
Works everywhere except:

  • Gmail App on Android
  • AOL Web Clients
  • Gmail Web Clients
  • Google Apps Web Clients
  • Yahoo! Mail Web Clients

style=”background-color:#eeeeee;” on <body>
Works everywhere except:

  • Android 4.4
  • Yahoo! Mail Web Clients

bgcolor=”#eeeeee” on <table>
Leaves white padding around entire email in:

  • Outlook 2007
  • Outlook 2010
  • Outlook 2013
  • Outlook 2016
<body bgcolor="#eeeeee" style="margin:0; padding:0; width:100% !important; background-color:#eeeeee;">
<table bgcolor="#eeeeee" cellpadding="0" cellspacing="0" border="0" width="100%">
</table>
</body>

JUMP LINK // ANCHOR TAG

To use a Jump Link in Email, you’ll have to use an “id” on the <td> and a “name” on the <a> (href) tag.

Note: The value must contain a pound sign (#) before the value name in the href field.

JUMP LINK

<a href="#JumpLinkSection">Jump To Jump Link Section</a>

JUMP LINK SECTION

<td align="left" valign="top" id="JumpLinkSection"><a href="http://tabletrtd.com" name="JumpLinkSection">This is the Jump Link Section!</a></td>

USING A SPACER GIF

How to use a Spacer Gif correctly to provide pixel perfect spacing in an Email.

FOR HEIGHT

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td height="20" align="left" valign="top" style="font-size:0; line-height:1px;"><img src="images/spacer.gif" alt="" width="1" height="20" border="0" style="display:block;" /></td>
  </tr>
</table>

FOR WIDTH

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td width="20" align="left" valign="top"><img src="images/spacer.gif" alt="" width="20" height="1" border="0" style="display:block;" /></td>
  </tr>
</table>

FOR FULL WIDTH (<hr />)

<table bgcolor="#000001" cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td height="1" align="left" valign="top" style="font-size:0; line-height:1px;"><img src="images/spacer.gif" alt="" width="1" height="1" border="0" style="display:block;" /></td>
  </tr>
</table>

CSS

<style type="text/css">
@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
.widthMobile {
  width: 320px !important;
}
}
</style>

PHOTO CLASS

To resize images from a desktop for a responsive mobile version, place a class on the image. For a fluid design use a width: 100% and a height: auto. For a non fluid design, use fixed measurements based on a ratio of your image dimensions.

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td align="left" valign="top"><img src="http://placehold.it/500x90" alt="" width="500" height="90" border="0" style="display:block;" class="photo" /></td>
  </tr>
</table>

CSS FLUID

<style type="text/css">
@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
.photo {
  width: 100% !important;
  height: auto !important;
}
}
</style>

CSS FIXED BASED ON A RATIO

<style type="text/css">
@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
.photo {
  width: 320px !important;
  height: 57px !important;
}
}
</style>

REVERSE STACK

Here is the most scalable solution for the Responsive Drop issue on Android 4.4. The strategy here is to use two stacked tables with align left and a closing and opening TD in an MSO object.

<table dir="rtl" class="widthMobile" width="500" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td align="left" valign="top">
      <table dir="ltr" align="right" class="widthMobile" width="250" cellpadding="0" cellspacing="0" border="0" bgcolor="#F0161A">
        <tr>
          <td align="left" valign="top" style="font-family:Helvetica, Arial, sans-serif; font-size:14px; mso-line-height-rule:exactly; line-height:18px; color:#ffffff; padding:10px 10px 10px 10px;">Column 2 Column 2 Column 2 Column 2 Column 2 Column 2 Column 2 Column 2 Column 2 Column 2</td>
        </tr>
      </table>
      <!--[if mso]>
      </td>
      <td align="left" valign="top">
      <![endif]-->
      <table dir="ltr" align="left" class="widthMobile" width="250" cellpadding="0" cellspacing="0" border="0" bgcolor="#2849A7">
        <tr>
          <td align="left" valign="top" style="font-family:Helvetica, Arial, sans-serif; font-size:14px; mso-line-height-rule:exactly; line-height:18px; color:#ffffff; padding:10px 10px 10px 10px;">Column 1 Column 1 Column 1 Column 1 Column 1 Column 1 Column 1 Column 1 Column 1 Column 1</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

CSS

<style type="text/css">
@media only screen and (max-width: 480px), only screen and (max-device-width: 480px) {
.widthMobile {
    width: 320px !important;
}
}
</style>

PREHEADER TEXT (OR PREVIEW TEXT)

Preheader Text is copy hidden at the top of your HTML. Place this Code at the very top of your HTML, directly BELOW your opening body tag, ABOVE your tracking pixel. This text will display as the first text of your email in the Preview Pane of some Email Clients and most Mobile Clients. The hidden version will not show in the Email itself.

VISIBLE VERSION

<table cellpadding="0" cellspacing="0" border="0" width="100%">
  <tr>
    <td align="left" valign="top" style="font-family:Helvetica, Arial, sans-serif; font-size:14px; mso-line-height-rule:exactly; line-height:18px; color:#3a3a3a; padding:20px 20px 20px 20px;">PREHEADER TEXT HERE</td>
  </tr>
</table>

HIDDEN VERSION

<table cellpadding="0" cellspacing="0" border="0" width="100%">
 <tr>
   <td style="font-size:0; line-height:0; display:none !important;">
     <div style="display:none !important;">PREHEADER TEXT HERE</div>
   </td>
 </tr>
</table>