Send Emails from PHP on Ubuntu localhost LAMP setup

In this article, I will walk you through the process of configuring your local, Ubuntu based development machine to send emails from within PHP and also from the command line terminal. In a nutshell, you need to install and configure a mail server on your local computer. The mail server that we are going to use is Postfix. So without any further delay, lets get started.

Open up the terminal and, first of all, enter the following command:

It will make available the “mail” command on your terminal and also will install all the programs that it depends on. Our desired mail server Postfix is one of them. During installation, accept the defaults as configuration options of Postfix when asked.

Once the installation is complete, type following command:

It will launch configuration wizard for Postfix and you will see its first screen similar to the following screenshot.

Postfix Configuration Screen 1Select <Ok> and press Enter.

Note: You can select <Ok> by pressing right or left arrow key on your keyboard.

On the next screen, select “Internet Site” and proceed.

Postfix Configuration Screen 2On next screen, enter any valid domain name that you own (e.g. I entered anas.pk) or simply enter something like mail.example.com if you don’t own any domain name.

Note: On a screen with space to enter as this one, you can select <Ok> using Up or Down arrow key on your keyboard instead of the Left or Right arrow key as on a menu based screen.

Postfix Configuration Screen 3 Next, enter the username of a valid user account on the system on which you are doing these installations. It is the username that a user uses to log in to ubuntu on this system.

Postfix Configuration Screen 4On next screen, accept whatever the wizard comes up with. For example, here is what wizard gave me in a previous configuration session. Postfix Configuration Screen 5On next screen, select <No> and press Enter.

Postfix Configuration Screen 6Accept defaults on all remaining four configuration screens.

Postfix Configuration Screen 7 Postfix Configuration Screen 8 Postfix Configuration Screen 9 Postfix Configuration Screen 10When you press Enter after selecting <Ok> on this last screen, configuration wizard will close and new configuration will be loaded for the Postfix server automatically.

At this point, you can start trying to send email from both command line terminal and from the php mail() function. But for me, mail was only going to the accounts hosted with Google Apps and also it went a couple of times to normal Gmail accounts. Other than that, it was not reaching inboxes of yahoo and outlook email accounts at all and also it was not reaching normal Gmail (@gmail.com) accounts most of the time.

To solve this problem, I further configured my newly installed mail server to use my Gmail account for all outgoing email. I did this by following the awesome instructions provided in this tutorial.

I am summarizing that procedure here for your quick reference but all credit goes to the original author of that tutorial.

Open the file /etc/postfix/main.cf in your favourite text editor as root user. Here is a sample command to open it in nano :

However, if you prefer a Graphical interface, you can also type the following command to open it up in gedit :

Add following lines to the end of this file, save, and then close it.

Open the /etc/postfix/sasl_passwd file in a text editor if it already exists or create a new file with this name at that location and then open it up. A sample command to do both of this is given below:

Add following line to this file:

Replace USERNAME, and PASSWORD with your Gmail username and password respectively and then save and close this file too.

Now, run following few commands in order to finish up this whole configuration process.

Now create a new PHP script file in your web server document root directory and paste following code in it.

Change the value of variable $to to one of your real email addresses where you would like to receive this test email. Run this script by accessing it through your web browser. For example, I saved it as mailtest.php under /var/www/ . Therefore, I executed it by entering http://localhost/mailtest.php in the address bar of my web browser.

Hopefully you will see a success message on this web page and will receive an email with the subject “Test mail” on the address that you specified in the above script.

You can also now send email directly from the command line terminal of your computer. To do this, enter following command:

where “recepient@example.com” is the address to which you want to send this email.

You can leave the Cc: blank. Enter your desired heading when prompted for Subject: . When your subject completes, press Enter. Whatever you type from this point forward will become the body of your email. When you are finished typing the body of your email, insert a new line break by pressing the Enter key and then press Ctrl+D. Your email will be hopefully sent.

Let me know in the comments if any of the steps described above don’t work correctly for you. I will try my best to help you in every possible way.

Thanks for visiting. Have a nice day 🙂

12 thoughts on “Send Emails from PHP on Ubuntu localhost LAMP setup”

  1. Worked perfectly. But One problem I need to turn on access for less secure apps on google’s account settings. Is there a way to make it work securely. ?

  2. I receave “Mail sent successfully!” when sending via php file example, and I don’t get an error message when I send via command line. Only the message WARNING: gnome-keyring:: couldn’t connect to: /tmp/keyring-rgI6w9/pkcs11: file or directory not found.

Leave a Reply