5 best practices developers should follow when coding email send functions
Emails are the best communication medium for applications with their users. As developers, we need to often send emails from applications like OTP, transactions emails, marketing emails, etc. It is very important to follow the best practices in email sending as a small issue can end up in the user not receiving an important email like OTP which is required for login. Here we have tried to put the best practices you need to follow to build a reliable email sending function.
1. Using a fake SMTP server
The first and most important step while developing an email send function is to use a fake SMTP server like Mailnest to test your emails in a development environment. This will make your email testing really easy and risk-free. You can use whatever email ids to test and it will reach only the fake SMTP server inbox. So you don’t run the risk of accidentally sending test emails to real customers. It will also make it very easy to collaborate with your QA team on getting the email functions tested effectively.
2. Using config files for settings
Using config files to store the settings of the email server is a best practice you should follow because you might want to change your email service provider multiple times and this will help you easily change your provider without affecting any other parts of your code. Also if you are using a fake SMTP server, you can provide 2 different configs for the development and the production environment which will automatically switch considering which environment the application is currently running in. This is a practice you need to follow.
3. Using queueing systems
One of the issues you might encounter while sending bulk emails from your application will be blocking up user time and server resources for that function. This might cause even server downtime if you are running on a low spec as the server may not process other requests. The best way to avoid this is by sending asynchronous emails. You can use a queueing system like AWS SQS or if you are using any frameworks, they might also be providing Queueing functions. The main advantage of using Queues is all the emails from the application will be sent to the queue and the queue will process it. Also when queueing your emails, please remember to set priorities. We don’t want an OTP email that is below in the queue to be processed after processing all the marketing emails which are above the OTP email in the queue. So we should set priorities so that high-priority emails get sent quickly even if it is below in the queue. You can also use multiple queues for a single application for categorizing and prioritizing emails if required.
4. Serve SSL URLs
Always use SSL URLs for resources used in the email content. Most of the emails clients won’t display the resources that are served by a non-SSL URL. So you don’t want your email to look unprofessional and the main part of your email might be that beautiful image you created which won’t even get displayed for the user. So before using images please make sure that the URL is SSL.
5. Use Inline CSS
Inline CSS is the way in which we give the style of an HTML element inside the tag itself. Even though it is not a good practice when you develop a web application, it is the best practice when you design and develop an email. This is because some of the email clients do not accept embedded or external CSS. Again you don’t want your email to look like some crazy artwork without styling. So please make sure that you have written your styles inline which is accepted by all the email clients.
So, please let me know in the comments if you are following any other process that has helped you improve your email function sending coding. If you are not following the above processes, please try to include them in your coding practices so that you can have a well-oiled email sending machine.