<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Francisco Hanna</title>
    <description>The latest articles on DEV Community by Francisco Hanna (@franciscohanna92).</description>
    <link>https://dev.to/franciscohanna92</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F509018%2Fe318d4b1-101a-4b23-929a-acdc130f5e94.png</url>
      <title>DEV Community: Francisco Hanna</title>
      <link>https://dev.to/franciscohanna92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/franciscohanna92"/>
    <language>en</language>
    <item>
      <title>Configuring a custom SSL forward proxy for click tracking your branded link emails in Sendgrid</title>
      <dc:creator>Francisco Hanna</dc:creator>
      <pubDate>Fri, 06 Nov 2020 01:40:08 +0000</pubDate>
      <link>https://dev.to/franciscohanna92/configuring-a-custom-ssl-forward-proxy-for-click-tracking-your-emails-in-sendgrid-569n</link>
      <guid>https://dev.to/franciscohanna92/configuring-a-custom-ssl-forward-proxy-for-click-tracking-your-emails-in-sendgrid-569n</guid>
      <description>&lt;p&gt;When you setup your Sendgrid emails to have branded links, that is, that the &lt;code&gt;mail from&lt;/code&gt; that your customers see when receiving an email from you says something like &lt;em&gt;From: &lt;a href="mailto:bob@yourdomain.com"&gt;bob@yourdomain.com&lt;/a&gt;&lt;/em&gt;, then, to enable click tracking of your emails, you need to pass the requests through a SSL forward proxy before sending the tracking requests to Sendgrid (at sendgrid.net). This proxy must live under your custom domain, or in a subdomain of it.&lt;/p&gt;

&lt;p&gt;In this article I will show you how to setup a custom SSL configuration to enable click tracking over branded links in Sendgrid. &lt;/p&gt;

&lt;p&gt;I'm writing this because, while configuring the click tracking for our JSON API service (&lt;a href="https://jornalia.net"&gt;Jornalia&lt;/a&gt;, a news aggregator API for argentinian sources) emails, the official &lt;a href="https://sendgrid.com/docs/ui/account-and-settings/custom-ssl-configurations/"&gt;Sengrid documentation on this topic&lt;/a&gt; it's not clear enough, specially for people not too familiar with networks and systems administration.&lt;/p&gt;

&lt;p&gt;I'm assuming that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have an understanding of a Unix shell&lt;/li&gt;
&lt;li&gt;You are using Ubuntu Server 20.04 on your server instance.&lt;/li&gt;
&lt;li&gt;You have installed &lt;a href="https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04-es"&gt;NGINX&lt;/a&gt; and &lt;a href="https://nodejs.org/es/download/package-manager/"&gt;NodeJS&lt;/a&gt; on your server.&lt;/li&gt;
&lt;li&gt;You have already verified your custom domain to brand your links on Sendgrid. Follow &lt;a href="https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-link-branding/"&gt;their guide&lt;/a&gt; if you haven't do this yet.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Setting up the SSL forward proxy
&lt;/h2&gt;

&lt;p&gt;For this, we are going to use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A NGINX instance acting as a reverse proxy that will handle the requests and sending them to a NodeJS app.&lt;/li&gt;
&lt;li&gt;A NodeJS app built with Express, that will forward the requests to &lt;code&gt;sendgrid.net&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configuring a NGINX reverse proxy
&lt;/h3&gt;

&lt;p&gt;Open your NGINX's default &lt;code&gt;sites-available&lt;/code&gt; file, running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/default 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following server block to the &lt;code&gt;default&lt;/code&gt; file, where &lt;code&gt;url1234.yourdomain.net&lt;/code&gt; is your branded link for tracking that you obtained while validating your custom domain on Sendgrid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;url1234.yourdomain.net&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://localhost:7000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_http_version&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="s"&gt;.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Upgrade&lt;/span&gt; &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Connection&lt;/span&gt; &lt;span class="s"&gt;'upgrade'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Host&lt;/span&gt; &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="kn"&gt;proxy_cache_bypass&lt;/span&gt; &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding a CNAME for your branded link.
&lt;/h3&gt;

&lt;p&gt;Add the following records to your domain's DNS zone (the first one may already be there, &lt;code&gt;63.98.25.184&lt;/code&gt; would be your server's IP):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Content&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A&lt;/td&gt;
&lt;td&gt;yourdomain.com&lt;/td&gt;
&lt;td&gt;63.98.25.184&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CNAME&lt;/td&gt;
&lt;td&gt;url1234.yourdomain.com&lt;/td&gt;
&lt;td&gt;yourdomain.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Build and run a forward proxy with Node and Express
&lt;/h3&gt;

&lt;p&gt;Crete a new folder on your server named &lt;code&gt;mail-proxy&lt;/code&gt;. Move inside that folder and run:&lt;br&gt;
&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
Then, install the following packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i express morgan http-proxy-middleware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an &lt;code&gt;index.js&lt;/code&gt; file and add the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;morgan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;morgan&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createProxyMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http-proxy-middleware&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;SENDGRID_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://sendgrid.net&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;morgan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;createProxyMiddleware&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SENDGRID_URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;changeOrigin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;secure&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Host&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;url1234.yourdomain.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Starting Proxy at &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple Express app listens for requests on &lt;code&gt;PORT 7000&lt;/code&gt; and the handler for the index &lt;code&gt;/&lt;/code&gt; is a &lt;code&gt;http-proxy-middleware&lt;/code&gt; instance, that forwards every request to &lt;code&gt;sendgrid.net&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, install &lt;code&gt;pm2&lt;/code&gt; (a Node process manager), and run your forward proxy, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -g pm2
pm2 start index.js --watch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Securing the proxy with SSL
&lt;/h3&gt;

&lt;p&gt;For this, we are going to use &lt;a href="https://certbot.eff.org/"&gt;Certbot&lt;/a&gt; to generate a Let's Encrypt certificate. To install Certbot, follow the &lt;a href="https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx"&gt;official guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After you have installed Certbot, run the following command on the terminal to generate a SSL certificate your for branded links domain, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;certbot --nginx -d url1234.yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Contact Sendgrid
&lt;/h2&gt;

&lt;p&gt;After you have configured your forward proxy, you need to &lt;a href="https://sendgrid.com/docs/ui/account-and-settings/support/"&gt;contact Sendgrid's Support&lt;/a&gt; for them to enable the click tracking feature for branded links. Make sure you also &lt;a href="https://sendgrid.com/docs/ui/account-and-settings/tracking/#click-tracking"&gt;enable the Click Tracking setting&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;That's all. Hope it helps!&lt;/p&gt;

</description>
      <category>custom</category>
      <category>ssl</category>
      <category>sendgrid</category>
      <category>email</category>
    </item>
  </channel>
</rss>
