Q: Why is the AMPScript evaluated more than once when I don't include a reference to the message context?
A: The AMPScript is evaluated both in the HTML and Text versions of the email. You can explicitly remove the AMPScript from the Text version of the email and at that point it will only evaluate one time.
Q: How do I prevent AMPScript from evaluating in Send Previews or when viewing the email as a webpage?
A: You can reference the message context to control exactly what happens and when. The following message contexts are available referencing the following attribute _messagecontext:
SEND - Display the rendered final message for sending to subscriber
PREVIEW - Display the send preview options available within editor
VAWP - Display content
VIEWSENT - Display the non-subscriber link to preview content
FTAF - Display the rendered Forward To a Friend message
LANDINGPAGE - Display a landing page or microsite
VALIDATION - Display information corresponding to the validate option in Marketing Cloud
LINKRESOLUTION - Display resolved dynamic script at click time
SMS - Display SMS message content
SOCIAL - Display Social Forward content
SITE - Display CloudPage content
This is outlined in our help documentation here.
In order to prevent the AMPScript from evaluating during preview or view as a webpage and only evaluate during sending to an inbox, you would wrap your AMPScript in an IF statement to essentially ensure the _messagecontext is SEND rather than PREVIEW or VAWP.
For more information on creating these IF statements, see the documentation linked here.
Example Code:
<html> <body> %%[ IF _messagecontext == 'VAWP' THEN ]%% <p> Print my View as a Web Page Content!! </p> %%[ ELSE ]%% <p> Print my Standard Content for All Other Contexts!! </p> %%[ ENDIF ]%% <table cellpadding="2" cellspacing="0" width="600" ID="Table5" Border=0><tr><td><font face="verdana" size="1" color="#444444">This email was sent by: <b>%%Member_Busname%%</b><br>%%Member_Addr%% %%Member_City%%, %%Member_State%%, %%Member_PostalCode%%, %%Member_Country%%<br><br></font></td></tr></table><custom name="opencounter" type="tracking"> </body> </html>
000381182

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.