<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Djay on Medium]]></title>
        <description><![CDATA[Stories by Djay on Medium]]></description>
        <link>https://medium.com/@dogra.dd?source=rss-e172eb5959c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KIFSNgpuDiCxNEaD</url>
            <title>Stories by Djay on Medium</title>
            <link>https://medium.com/@dogra.dd?source=rss-e172eb5959c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 23:01:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dogra.dd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PDF & ICS Attachments in Salesforce B2C Commerce Emails]]></title>
            <link>https://medium.com/@dogra.dd/pdf-ics-attachments-in-salesforce-b2c-commerce-emails-2807afa6794c?source=rss-e172eb5959c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/2807afa6794c</guid>
            <category><![CDATA[sfcc]]></category>
            <category><![CDATA[salesforce-commerce-cloud]]></category>
            <category><![CDATA[salesforce]]></category>
            <category><![CDATA[salesforce-commerce]]></category>
            <dc:creator><![CDATA[Djay]]></dc:creator>
            <pubDate>Fri, 28 Feb 2025 15:25:57 GMT</pubDate>
            <atom:updated>2025-02-28T15:29:24.006Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*inoJoC-6RNoOZT0oXMToqw.png" /></figure><p><strong>Attachments enhance an email</strong> by providing additional context, supporting documents, images, or files, improving clarity, efficiency &amp; making it a rich multimedia experience.</p><h3><strong>Use Case: Why’d I explore attachments in SFCC Emails?</strong></h3><p>A client requirement to attach return PDFs for orders and ICS attachments to put reservations on the customer’s calendars.</p><p>SFCC provides the ability to send regular emails out of the box. Follow along to know how to add multiple types of attachments to meet different client use cases.</p><h3><strong>Code: PDF Attachments</strong></h3><blockquote><strong>Case1: When the PDF is already available in an encoded format.</strong></blockquote><pre>//email sending hook<br>sendEmailPDF: function (emailObj, template, context) {<br>  var hooksHelper = require(&#39;*/cartridge/scripts/helpers/hooks&#39;);<br>  return hooksHelper(&#39;app.customer.email&#39;, &#39;sendEmail&#39;, [emailObj, template, context], sendPDF);<br>}<br><br>//email sending utility function<br>function sendPDF(emailObj, template, context) {<br>  var Mail = require(&#39;dw/net/Mail&#39;);<br>  var renderTemplateHelper = require(&#39;*/cartridge/scripts/renderTemplateHelper&#39;);​<br><br>  context.pdfAttachment = emailObj.pdfAttachment; //encoded pdf<br><br>  var email = new Mail();<br>  email.addTo(emailObj.to);<br>  email.setSubject(emailObj.subject);<br>  email.setFrom(emailObj.from);<br>  email.setContent(renderTemplateHelper.getRenderedHtml(context, template), &#39;multipart/mixed; boundary=emailSectionDelimiter&#39;, &#39;UTF-8&#39;); //MIME format, email boundary &amp; encoding specification<br>  email.send();<br>}</pre><pre>&lt;iscomment&gt; isml template example &lt;/iscomment&gt;<br><br>&lt;iscontent type=&quot;multipart/mixed; boundary=emailSectionDelimiter&quot; compact=&quot;false&quot;&gt;<br>--emailSectionDelimiter &lt;iscomment&gt; boundary here denotes start of first section &lt;/iscomment&gt;<br>Content-Type: text/html; charset=UTF-8<br><br>    &lt;style&gt;<br>        /* css styling for email */<br>    &lt;/style&gt;<br><br>    &lt;!-- Email Body : BEGIN --&gt;<br>        &lt;!-- Email Header : BEGIN --&gt;<br>            &lt;h1&gt;Sample Header Content&lt;/h1&gt;<br>        &lt;!-- Email Header : END --&gt;<br><br>        &lt;!-- Footer Section : BEGIN --&gt;<br>            &lt;h1&gt;Sample Footer Content&lt;/h1&gt;<br>        &lt;!-- Footer Section : END --&gt;<br>    &lt;!-- Email Body : BEGIN --&gt;<br><br>--emailSectionDelimiter &lt;iscomment&gt; boundary here denotes end of first section &lt;/iscomment&gt;<br>Content-Type: application/pdf; name=&quot;ShippingLabel&quot;;<br>Content-Description: ShippingLabel<br>Content-Disposition: attachment; filename=&quot;ShippingLabel&quot;; creation-date=&quot;${(new Date()).toISOString()}&quot;; modification-date=&quot;${(new Date()).toISOString()}&quot;<br>Content-Transfer-Encoding: base64<br>${pdict.pdfAttachment}<br>--emailSectionDelimiter-- &lt;iscomment&gt; boundary with preceeding -- here denotes end of the entire template &lt;/iscomment&gt;</pre><p>The <strong>emailSectionDelimiter </strong>boundary is used to separate sections of the isml template and enables us to mark each section with its respective MIME type &amp; encoding/decoding</p><p>e.g. the body of the email is of type text/html, but the pdf attachment in the 2nd section is of type application/pdf.</p><blockquote><strong>Case2: When the PDF is available as a .pdf file</strong></blockquote><blockquote><strong>( This approach deals with <em>Reading</em> and <em>Converting</em> the .pdf file and then plugging the same in Case1’s code)</strong></blockquote><p>A real-time use case for this scenario may be a flyer or a promotional pdf that contains static content and has to be used for multiple emails to the end users of an application.</p><p>The same email sending util function can be modified to <strong><em>read the pdf from the impex folder</em></strong> in SFCC.</p><pre>//email sending utility function<br>function sendPDF(emailObj, template, context) {<br>  var Mail = require(&#39;dw/net/Mail&#39;);<br>  var renderTemplateHelper = require(&#39;*/cartridge/scripts/renderTemplateHelper&#39;);​<br><br>  var pdfContent = readPDFFile(&#39;IMPEX/samplePDF.pdf&#39;);<br>  var encodedPdf = encodeBase64ForEmail(pdfContent, &#39;ISO-8859-1&#39;);<br>  context.pdfAttachment = encodedPdf;<br><br>  var email = new Mail();<br>  email.addTo(emailObj.to);<br>  email.setSubject(emailObj.subject);<br>  email.setFrom(emailObj.from);<br>  email.setContent(renderTemplateHelper.getRenderedHtml(context, template), &#39;multipart/mixed; boundary=mailwithattachment&#39;, &#39;UTF-8&#39;);<br>  email.send();<br>}<br><br>//util function to read a .pdf file<br>function readPDFFile(filePath) {<br>    var File = require(&#39;dw/io/File&#39;);<br>    var FileReader = require(&#39;dw/io/FileReader&#39;);<br><br>    var testPDF = new File(filePath);<br>    var pdfFile = new FileReader(testPDF, &#39;ISO-8859-1&#39;);<br><br>    var pdfContent = &#39;&#39;;<br>    var line = &#39;&#39;;<br><br>    do {<br>        line = pdfFile.readN(1000);<br>        pdfContent += line;<br>    } while (line != null);<br><br>    return pdfContent;<br>}<br><br>//util function to convert the pdf into a base64 encoded value<br>function encodeBase64ForEmail(str, characterEncoding) {<br>    var StringUtils = require(&#39;dw/util/StringUtils&#39;);<br>    var StringWriter = require(&#39;dw/io/StringWriter&#39;);<br>    var base64 = StringUtils.encodeBase64(str, characterEncoding);<br>    var base64Str = &#39;&#39;;<br>    var stringWriter = new StringWriter();<br><br>    var offset = 0;<br>    var length = 76;<br><br>    while (offset &lt; base64.length) {<br>        var maxOffset = offset + length;<br><br>        if (base64.length &gt;= maxOffset) {<br>            stringWriter.write(base64, offset, length);<br>            stringWriter.write(&#39;\n&#39;);<br>        } else {<br>            stringWriter.write(base64, offset, length - (maxOffset - base64.length));<br>        }<br>        offset += length;<br>    }<br><br>    stringWriter.flush();<br>    base64Str = stringWriter.toString();<br>    stringWriter.close();<br><br>    return base64Str;<br>}</pre><blockquote><strong><em>The same isml template works for this approach as well, only with 2 extra steps to read and convert the .pdf file into a base64 encoded value.</em></strong></blockquote><h3>ICS(Internet Calendaring and Scheduling)</h3><p>An ICS file allows users to add events directly to their calendar application (like Google Calendar, Outlook, or Apple Calendar) by simply opening the attached file in their email.</p><p>ICS Implementation is very similar to PDF, but the .ics Creation is a little different.</p><h3>Code: ICS Attachments</h3><pre>function sendICS(emailObj, template, context) {<br>  let Mail = require(&#39;dw/net/Mail&#39;);<br>  let renderTemplateHelper = require(&#39;*/cartridge/scripts/renderTemplateHelper&#39;);​<br><br>  let sd = (((new Date().toISOString()).split(&quot;T&quot;)[0]).split(&quot;-&quot;)).join(&quot;&quot;);<br>  let ed = (((new Date().toISOString()).split(&quot;T&quot;)[0]).split(&quot;-&quot;)).join(&quot;&quot;);<br><br>  let eventDate = {<br>      start: (((new Date().toISOString()).split(&quot;T&quot;)[0]).split(&quot;-&quot;)).join(&quot;&quot;) + &quot;T000000&quot;,<br>      end: (((new Date((new Date()).valueOf() + 1000*3600*24).toISOString()).split(&quot;T&quot;)[0]).split(&quot;-&quot;)).join(&quot;&quot;) + &quot;T000000&quot;,<br>  };<br>  let summary = &#39;Sample Summary for the .ics file&#39;;<br>  let description = &#39;Sample .ics description&#39;;<br>  let location = &#39;Sample location&#39;;<br>  let icsFile = makeIcsFile(eventDate, summary, description, location, emailObj);<br><br>  context.icsAttachment = encodeBase64ForEmail(icsFile, &#39;ISO-8859-1&#39;); //same util used above for converting string into base64 encoded value<br><br>  let email = new Mail();<br>  email.addTo(emailObj.to);<br>  email.setSubject(emailObj.subject);<br>  email.setFrom(emailObj.from);<br>  email.setContent(renderTemplateHelper.getRenderedHtml(context, template), &#39;multipart/mixed; boundary=mailwithattachment&#39;, &#39;UTF-8&#39;);<br>  email.send();<br>}<br><br>function makeIcsFile(date, summary, description, location, emailObj) {<br>  let ics =<br>  &quot;BEGIN:VCALENDAR\r\n&quot; +<br>  &quot;CALSCALE:GREGORIAN\r\n&quot; +<br>  &quot;METHOD:PUBLISH\r\n&quot; +<br>  &quot;PRODID:-//Sample Org Name//EN\r\n&quot; + //Sample Org Name e.g. xyz<br>  &quot;VERSION:2.0\r\n&quot; +<br>  &quot;BEGIN:VEVENT\r\n&quot; +<br>  &quot;UID:&quot; + Date.now() + &quot;@xyz.com&quot; + &quot;\r\n&quot; + //sample domain name(xyz.com)<br>  &quot;ATTENDEE:mailto:&quot; + emailObj.to + &quot;\r\n&quot; +<br>  &quot;LOCATION:&quot; + location + &quot;\r\n&quot; +<br>  &quot;DTSTAMP:&quot; + date.start + &quot;\r\n&quot; +<br>  &quot;DTSTART:&quot; + date.start + &quot;\r\n&quot; +<br>  &quot;DTEND:&quot; + date.end + &quot;\r\n&quot; +<br>  &quot;SUMMARY:&quot; + summary + &quot;\r\n&quot; +<br>  &quot;DESCRIPTION:&quot; + description + &quot;\r\n&quot; +<br>  &quot;END:VEVENT\r\n&quot; +<br>  &quot;END:VCALENDAR&quot;;<br><br>  return ics;<br>}</pre><p>The same logic may be leveraged to send .csv files as well.</p><p>Good Luck sending enhanced emails directly from SFCC :)</p><blockquote><strong>Useful Links &amp; Sources used to create a working example</strong></blockquote><ol><li><a href="https://help.salesforce.com/s/articleView?id=000391171&amp;language=en_US&amp;type=1">https://help.salesforce.com/s/articleView?id=000391171&amp;language=en_US&amp;type=1</a></li><li><a href="https://www.rhino-inquisitor.com/mail-attachments-in-b2c-commerce-cloud/">https://www.rhino-inquisitor.com/mail-attachments-in-b2c-commerce-cloud/</a></li><li><a href="https://salesforcecommercecloud.github.io/b2c-dev-doc/">https://salesforcecommercecloud.github.io/b2c-dev-doc/</a></li><li><a href="https://sfcc-unofficial.slack.com/archives/CAT794PC3/p1616592855270900">https://sfcc-unofficial.slack.com</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2807afa6794c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>