What is SMTP?

Ask anyone ‘What is SMTP?’ and the first thing that pops to mind is ‘Simple Mail Transfer Protocol’. Let’s go beyond the proverbial definition and understand what exactly is SMTP and why we need it.

If you are familiar with how email works, you will know that sending and receiving emails is possible because of SMTP.

All the different programming languages and logical skills you’ve learnt as developer run only on your local computer/ server. When you want to connect to the outside network and send an email/ SMS/ notification, you’ll need a special protocol called SMTP. Most email systems use SMTP to send email from one server to another.

Why SMTP when we have Gmail/ Yahoo/ Outlook?

It’s true most of us use web interface like Gmail, Yahoo, Outlook etc to send our day-to-day personal and business emails. But one thing we need to understand is that all these platforms are built for sending 1:1 manual communications and are not designed for scale.

Scale ??

Yes, if you run an online business, you’ll be sending thousands of automated emails which are triggered basis users’ activity on website – like sign up, cart abandonment, etc. What you need is a system that can scale efficiently and in this scenario, sending emails from a web interface is definitely not an option.

Sending emails using SMTP

You can either setup your own SMTP server or you can connect your email client with one of the SMTP server providers (one that is cloud based and scalable, like Pepipost) and grant permission to deliver emails directly.

Some of you might think of setting up your own SMTP server to save cost. But come to think of it, it really doesn’t. Because each of the SMTP relay servers will have some limit on the number of emails it can deliver on various ISPs. Even if you write a code that helps you scale and send millions of emails, sometimes ISPs will start blocking these emails thinking they are Spam because a high volume of emails is sent by one sender at one time.

On the other hand, if you opt to go with a good Email Service Provider (ESP), you can easily scale your email volumes and also rely on their email delivery expertise and high IP reputation to get emails inboxed. Choosing the right ESP is therefore very critical here. The ESP must be following various best practices to ensure that the reputation of sending IPs and sender domains remains high. While sending emails in bulk, it is very important to measure the performance of your emails because this will help build the reputation of your sender domain. Hence instead of building the technology from scratch, you can opt for a good ESP which will help you with all user activities like email opens, clicks, bounces, spam complaints.

How to really send emails using SMTP ?

It’s really very easy to send emails using SMTP. Almost all programming languages have an in-build function to make a connection to the SMTP relay server:

        PHP –> mail() read more
        Perl –> mail read more
        Java –> JavaMail API read more

Below are the parameters which you require to make an SMTP connection:

        Hostname: SMTP server location e.g. smtp.pepipost.com
        Username: Username to authenticate the smtp server
        Password: Password to autheticate the smtp server
        Port no: 25, 587 or 2525

Using these four parameters you will be able to connect to any SMTP relay server and post that you need to build the email file which consists of the Sender address, Recipient, Subject and Body. Creating this email file is also not rocket science, as every programming language has the functions to generate this file.

The above process is very similar to first making a database connection and then firing the sql query :)

Challenges most developers face while integrating an SMTP

In case you are on a shared hosting server, the ports 25 and 587 remain blocked. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.

Most of the hosting providers block these email ports to protect their network from sending any type of spam emails.

Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections

For most users we suggest port 587 to avoid rate limits set by some hosting providers.

Hope that by now you got an idea of what is SMTP and how it works. While this seems to be easy, there are some challenges you’ll face while using the default SMTP function.

In case your email volume is very less (maybe in few thousands), then default SMTP is ok, but in case you looking to generate millions of emails then default SMTP might not scale.

The reason is – Through one SMTP connection you can send only one email. Hence to deliver a million emails, you’ll need to make 1 Million SMTP connections! That’s a lot of bandwidth and most importantly time consuming, isn’t it?

You can get rid of making multiple SMTP connections using couple of hacks :)

#Hack 1: Persistent SMTP Connection

You can send multiple emails from just one SMTP connection. This concept is known as SMTP Keep Alive or Persistent SMTP connection.

The name itself is self-explanatory, make one SMTP connection and using the persistent SMTP function you can keep the same connection alive till it reaches the timeout situation.

One of the example of using persistent SMTP connection in PHP:

       $phpMailer = New PHPMailer();
       $phpMailer->isSMTP();
       $phpMailer->SMTPKeepAlive = true;
       for ( … ) {
               // Send your emails right away
               [ … ]
       }
      $phpMailer->SmtpClose();


#Hack 2: Have multiple processes running on the same machine

In case you are operating a business where your volume of emails run into double digit millions, then there are further tricks to optimize the same.

Let’s take an example server on which 5 IP addresses are configured. Then write 5 mailing processes using SMTP persistent code each configured to connect with one separate IP. Hence, eventually, you will have 5 Persistent SMTP connections ready to fire emails.

Let’s say each of these persistent connections is capable of sending 100 emails/sec. That means, all 5 together will be able to send 500 emails in a second’s time.

The actual throughput of emails completely depends on the configuration of the server, network speed, resource consumption and lots more. You can further optimize the connections by using multiple physical machines or multiple machines under NAT.

One must understand that while this method of optimization can help you scale up your email volumes, you might get into trouble and be identified as a spammer, thereby causing your web servers to block your IP address. Hence, the above method of optimization is only advised when you are sending pure legitimate transactional/digest/notification type of emails on an opt-in user base.

Conclusion

While sending one email is fairly simple, for larger organizations that send millions of marketing or transactional emails, the SMTP process can get much more complicated. In that case, the best way to get started with SMTP email sending is setting up a local mail server, such as Postfix. It is Open Source and easy to configure :)

You can submit all your emails to local Postfix from your application and configure the SMTP of any ESP like Pepipost as smart host in the relay settings. Here the Postfix is just been used as a queuing system. Even if the internet connectivity between your servers and ESP drops, your postfix installation is smart enough to queue up the emails and resend to ESP once the connection is resumed. So, you don’t have to worry about building the intelligence into your sending application for handling all these queues.

If you have any questions related to SMTP, or if you want to know more about how Pepipost as an ESP uses SMTP to send billions of email every month, contact our 24×7 live chat support.

要查看或添加评论,请登录

Dibya Prakash Sahoo的更多文章

社区洞察

其他会员也浏览了