You are here:
Generate Barcodes in Emails with AMPscript
Use barcodes to create personalized messages, such as ticket purchase confirmation emails, in Marketing Cloud Engagement. Instead of using a barcode generator, use the AMPscript BarcodeURL function to automatically generate barcodes based on information from a recipient's profile.
Create a Scannable E-Ticket Scenario
This scenario describes how to create confirmation emails that staff can scan at a registration point directly from a participant’s mobile phone.
As a marketer, you assign each attendee a unique ticket ID based on name and email address values captured from an online registration form. To collect this data, you can use smart capture in a CloudPages landing page. If your form isn’t integrated with Marketing Cloud Engagement, manually enter the information in a sendable data extension. Then, with Content Builder and code snippets, build a personalized confirmation email with the unique ticket ID encoded in a barcode image. When the online registration is complete, the event organizer sends the personalized barcode e-tickets to each recipient in the data extension.
Sample Data Extension Structure
Set the email data extension field to EmailAddress and the other fields to 128-character Text.
| SubscriberKey | FirstName | LastName | Barcode | |
|---|---|---|---|---|
| john@example.com | john@example.com | John | Doe | OKVKTZ0K9O |
| kim@example.com | kim@example.com | Kim | Smith | HYW1D8XXDU |
| ben@example.com | ben@example.com | Ben | Clark | 31FPTXMR6I |
| robert@example.com | robert@example.com | Robert | Williams | 0OAHFSCGG4 |
AMPscript Code Snippet for the Email
For e-tickets scanned on mobile phones, this example uses the common Code128Auto, which supports alphanumeric ASCII characters. This barcode type is also narrow and high density, which encodes data with the fewest bars. This example adjusts the width to 300 pixels and height to 100 pixels. The barcode image is the default black and white as we recommend the highest contrast for fast mobile screen scanning. See the AMPScript BarCodeURL function for available barcode types, parameters, and examples.
%%[
var @barcode, @ticket, @firstName, @lastName, @fullName
set @ticket = AttributeValue("Barcode")
set @fullName = Concat(@firstName," ",@lastName)
set @barcode = BarcodeURL(@ticket, "Code128Auto", 300, 100)
]%%Barcode Sample for the Email Body
The BarcodeURL function generates a PNG image at the time of the email send. Use an image tag and set the source attribute to the function’s value.
<p>Name: %%=v(@fullName)=%%</p>
<p>Ticket Number: %%=v(@ticket)=%%</p>
<p><img src="%%=v(@barcode)=%%"></p>Final Result

