Build Your Own PHP Web Mailer | WebReference

Build Your Own PHP Web Mailer

By Voja Janjic


In this tutorial you will learn how to create your own PHP Web mailer. You will learn the necessary steps for creating an email account and making connections to the server protocols for sending and receiving email. You also will get a listing of PHP commands that you can use to customize the Web mailer to your specifications. Assuming you have a basic knowledge of PHP, let's begin.

First, we need to create an actual email account, which we will do by opening a URL for creating mail accounts. Mail accounts of course can be created manually through Cpanel X. You just need to enter your own cpanel username, password, domain and skin, email domain and email quota.

Sending Email

Now that we have created the account, we need to make connections to the servers for sending and receiving mail. Let's begin with the server for sending mail, SMTP. You should stick with the mail() function if you're building webmail only for your domain addresses. Otherwise, your mail very likely will end up marked as spam.

So, we let users input their preferred mail settings, store them in a database, and use fsockopen to connect to the server:

You may use the server IP as well as the domain name. If you need an encrypted connection, add a tls:\ or ssl:\ prefix to server name. Don't forget that the default port number for these connections is different from the unencrypted one.

In order to track sent messages, we have to store them in a database. I suggest using one table with the following structure:

  • userid
  • accounted
  • email_to
  • name_to
  • date_sent

If your users send a great number of messages per day, consider partitioning the table for better performance.