How to send transactional emails using APIs
- Published : April 18, 2023
- Last Updated : October 27, 2023
- 624 Views
- 6 Min Read
Ever wondered how you can browse videos on your laptop? how you are able to play hundreds of songs on the music app in your mobile? or even how an order confirmation email reaches you? Thanks to APIs, this is accomplished by blocks of code being easily accessed by applications or browsers.
API or Application programming interface, as the name suggests, creates a connection between two applications or even software. It enables them to share the required information. This way, a developer or an application owner can easily access the functionalities of their own application or different applications by just making an API call to them from their code.
Take the case of a travel website. You can look for information about your destination, hotels, flights, etc. Whenever you search for a flight or hotel, you get to see the consolidated information about different aircrafts and hotels. All this is done with the help of APIs. The website accesses the airlines' API and the hotels' API to give you the results.
Why use an API?
APIs are versatile and can be adapted to suit your purposes. You can either use your own API or a third party one. Either way, APIs make your life much easier.
Since APIs allow you to access the functionality of other applications. This saves the time you would spend on writing codes to build that functionality within your product.
The flexibility to use third-party APIs helps you add better functionality to your application.
How do APIs work?
In simple terms, APIs act as a connection between two applications. This is done in the form of various commands between them. A typical process involves the client sending a Request signal to an application. If the requested data is available, the application sends a Response.
Take the following example :
- Say you own an e-commerce application and Harry is your client. He makes a purchase. Let us take a look at how your application uses API to send the invoice email to Harry.
- Your application uses the email sending functionality of an email service provider (like ZeptoMail) to send out emails. This means, emails will be sent by the email service provider on behalf of you.
- The emails sent from an application reach their respective servers from where an API call (request) goes to the email service provider's API endpoint along with Harry's details and an API key.
- The API key is necessary to tell the email service provider (ESP) that your application is authorised to send the request. Otherwise, the request will not be processed and an error message will be shown.
- Once verified, the ESP accesses the relevant email and sends it to the customer. In this case, the invoice email.
This is how APIs, or any type of API, communicate to relay the necessary information. In the above example, you would have come across certain terminologies which are often used in the context of APIs. Knowledge about these will help you get a better understanding of the workings of these APIs.
Some of the commonly used terminologies are :
API call : An API call is the request that is sent from a server for a particular service or information.
API endpoint : An API call goes to an API endpoint. In simple terms, this is the point in the receiving server where an API request is made. API endpoints are publicly hosted and anyone trying to use a particular API can refer to their corresponding endpoints.
API key : An API key authenticates your identity to the receiving server while the API makes a request call. If you are using an email service provider like ZeptoMail, you will be able to view and access it from your account.
Overview of REST API :
The above sections give you an overview of how APIs work. However, it is necessary to outline certain rules to streamline the whole process. Multiple protocols have been developed to define how an API should be built and deployed. The most popular and commonly used protocol is the REST protocol.
REST or Representational state transfer protocol gives a set of constraints which an API should follow. Any API which complies with all these constraints is said to be RESTful. Owing to its popularity, REST has become very much synonymous with APIs so much so that they are often interchanged.
How do REST APIs work?
If you are familiar with how the web works, you will be able to understand the working of REST APIs too. REST APIs too make use of the Hyper Text Transfer Protocol (HTTP) to send out requests to other applications APIs. A client (application ), connects with a server for resources using certain commands. A resource can be any information the client looks for, like an email, folder, video, etc. The receiving server looks for the resource and relays it to the client.
HTTP uses four commands, which indicates a distinct request. They go by an acronym called CRUD (Create, Read, Update and Delete) although we use a slightly different set of commands. The commands used are :
- GET : Used to retrieve a particular data.
- POST : Used to send a particular data or information to a server.
- PUT : Used to modify the contents of an existing data.
- DELETE : Used to delete a particular data or information.
These commands, also known as methods will be sent along with the HTTP header. Numerous applications use REST for its's advantage of being flexible. They are easy to build and can be easily used with any application. ZeptoMail too uses this API to connect with your application and to send out emails.
Setting up email APIs in ZeptoMail :
Email APIs allow applications to access the services of an email service provider. Apart from sending emails, email APIs in ZeptoMail can also be used to upload your data to a file cache.
Consider this example of how your app can be integrated with ZeptoMail to send emails. Say you own an e-commerce apparel business and a user makes a purchase. An email intimation has to be sent to the customer regarding this. Here is how it goes about :
- Since you will be using ZeptoMail to send emails for you, you should have created an account and integrated your application with it.
- To send an email regarding order purchase, your application will access ZeptoMail's API.
- As you have already integrated your application, ZeptoMail will identify that the API request is coming from your application.
- It will then process your request, i.e., sending the order details to the customer.
Let us take a look at how you can use API to send emails in ZeptoMail :
Send mail token :
Before you start sending emails, you should generate a send mail token/ API key. To generate your API key :
- Navigate to the Mail Agents tab on the left panel of your ZeptoMail account and select a Mail Agent that you want to connect to your application.
- Click the Setup Info tab on the top pane. This will open the Setup Details page.
- From here you can copy your Mail Agent specific send mail token under the API tab.
- You can use the default Send Mail Token or generate a new one.
Sending emails using API :
The following is an example API header and the body of a request command in ZeptoMail.
curl "https://api.zeptomail.com/v1.1/email" \ -X POST \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -H "Authorization:Zoho-enczapikey ***" \ -d {" bounce_address:bounces@info.zylker.com, from: { 'address': 'accounts@info.zylker.com', 'name': 'Paula'}, to: [{'email_address': {'address': 'rudra.d@zylker.com','name': 'Rudra'}}], subject:Account Confirmation, htmlbody:'
Kindly click on Verify Account to confirm your account'
}"
This is the header of the API calling function.
curl "https://api.zeptomail.com/v1.1/email" \
-X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization:Zoho-enczapikey ***" \
It consists of :
- The URL that will be used to call the API.
- The content type in which the data will be sent and received. In this case, JSON.
- The API token sent for authorisation.
The header will be followed by the body which has the other details like from, to addresses and the email content.
By incorporating this API block within your code, your application will be able to send emails via ZeptoMail. You can further refer our help documentation to start sending emails using API.
APIs are ever-evolving and even if you do not work with them directly, familiarising yourself about them will help you include various useful functionalities within you application and make your work easier.