5 Useful SendGrid Tips

I recently started working on a concept of a message list subscription service, to be integrated directly into new product features so that people can add themselves to the list to receive information of updates on specific features. This led me down the path of using Azure Storage and SendGrid, building 2 raw prototype apps (in WinForms at this early stage) to achieve both the subscription and the sending out of data to the subscribers. Writing this on.NetFramework 4.8 with C# in a fairly native way has given me a great way to investigate and utilise the SendGrid platform to send emails to multiple subscribers.

I wanted to share my findings, rather than my code at this stage, and here are my Top 5 SendGrid tips:

  1. Do review Microsoft’s guide on how to get started with SendGrid via Azure, this simple article gives you a great base on how to set up the back-end and start writing C# around it: https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email
  2. Use Unsubscribe groups – these allow you to stay within GDPR in Europe, appending an Unsubscribe and Manage Preferences link to the bottom of the emails that are sent. i.e.
Unsubscribe preferences from SendGrid
  1. Write both plain text and HTML content for your email, I ended up creating an HTML builder so I could convert Email addresses to <a href="mailto:EmailAddress"> and any HTTP(s):// links to <a href:"http://link"> this was a great experiment with regular expressions. Note, I found that not all Email clients auto-convert, e.g. Outlook will convert an Email address but not a URL. I was creating my body text in a Rich Text Box, so it was just one big string!
  2. Look into Personalisations (https://sendgrid.com/docs/for-developers/sending-email/personalizations/) these are crucial if you want to customise your output (the emails themselves). I created variables to use in subject and body lines so I could pass in {customer} and {product}. SendGrid has a substitution capability which comes as part of personalisation. I’ll share a snippet of code here, as I found a solution on StackOverflow (https://stackoverflow.com/a/53292550) but it needed a slight tweak, changing the Tos.Add to a msg.AddTo as the To: Email address needs to be a part of the personalisation, I wasn’t sure if this was to do with API changes or not! Another tip, if you want the subject to be the same, without substitutions you can use the SetGlobalSubject method
var personalizationIndex = 0;
                foreach (var subscriber in subscriberEntities)
                {
                    msg.AddTo(new EmailAddress(subscriber.RowKey, subscriber.Name), personalizationIndex);
                    msg.AddSubstitution("{product}", product.Description, personalizationIndex);
                    msg.AddSubstitution("{customer}", subscriber.Name, personalizationIndex);
                    personalizationIndex++;
                    
                }

  1. Create a test email method, that uses the same logic as your core one, but only sends to one email address. You don’t want to be sending bulk emails out without checking them first!

I hope this new style of thread has been an interesting read, I hope to do more very soon!

Linux Virus Scan – Daily Email Script

Yes, that’s right, Linux can get viruses too, in fact they can harbour windows based viruses if the system is used as a web and email server! Something I’ve been becoming more and more familiar with. So, our systems already used ClamAV one of the more popular Linux based virus scanners. But whilst it runs a scan on incoming emails etc. It doesn’t really give me a nice visual output (no GUI). My solution? Automated daily scan with emailed results…

clamscan -r /var/www > /root/scanresults.txt

cat /root/scanresults.txt | mail -s ” Scan Results” hello@example.com

cat /root/scanresults.txt | grep FOUND | mail -s “Viruses Found” hello@example.com

So what does this do?

well, it scans the web directories (recursively) for any viruses that are listed in the virus DB (updated twice daily) – it then puts all the results into a text file. this text file is then read into an email command which is sent to the hello@example.com email address.

However, this isn’t much use as there are thousands of files and directories, what I really want to know is whether viruses were found… the solution to this is GREP out the value “FOUND” which is appended to the file name if a virus is found to be in it – this is then read into the same email command as before leaving me a nice list of only the files found with viruses!

I love a nice quick and easy script – I used cron tab to run this at 00:05 and 12:05 every day!

Exchange 2010 SP2

Microsoft have just released Exchange 2010 SP2 – a little over a month since I built both the new email servers on SP1 – how annoying is that! – anyway, after reading the new features list provided here: http://www.theregister.co.uk/2011/12/05/redmond_exchange_2010_sp2/ I am not too fussed, and will not be planning an upgrade anytime soon, maybe 6-12 months time I think…