How to enable php mail() in your ubuntu

In my last post I showed you how you can set up an ubuntu server for apache, php, mysql, phpmyadmin and wordpress multiste. Below is the link if you want to take a look:

How to setup a VPS (Ubuntu) for WordPress hosting – A to Z tutorial

Today, we will learn how we can enable email in that server, so that we can use php mail() to send mail from our system. Today I won’t go in that detail but just to set a simple mail server.

The simplest way to enable mail in your ubuntu is to run the following command:

[html]
sudo apt-get install sendmail
[/html]

It will install a copy of sendmail that listens locally. If you install that, then php can use the built in mail() function. There also won’t be a risk of becoming a remote relay if you install like this as well. If you plan on sending mail out to other internet domains, you will need to make sure the IP address of the machine sending mail has a reverse DNS entry (PTR record) set up as well. Most large ISP’s will reject your mail if your IP does not have a PTR record or one that points to a generic host (e.g. 1.2.3.3.domain.isp.com). It should reverse to something like mail.yourdomain.com

In other case, also I prefer to use postfix. It is also easy to install. To install run this command:

[html]
sudo apt-get install postfix
[/html]

During the installation a dialogue box will be appeared where you need to select “Internet Site” and the next box give your domain name as system domain.

Now to make it fully functional, we need to edit configuration file. Open the configuration file:

[html]
sudo nano /etc/postfix/main.cf
[/html]

Make the following changes:

[html]
myhostname = yourdomain.com
virtual_alias_maps = hash:/etc/postfix/virtual (you need to replace the line which has alias_maps)
mydestination = localdomain, localhost, localhost.localdomain, localhost, yourdomain.com
relayhost = (can be blank by default)
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Reload the configuration:

[html]
sudo /etc/init.d/postfix reload
[/html]

Also, you can add users to the alias file. Open up the the alias database:

[html]
sudo nano /etc/postfix/virtual
[/html]

Enter the names and emails of the user like:

[html]
system@domain.com user1
support@example.com user2
[/html]
Press ‘cntrl x’ to exit the file and then ‘Y’ to save the file.

Once you are finished, save, exit, and run the following command to enable:

[html]
postmap /etc/postfix/virtual
[/html]

Again restart the postfix:

[html]
sudo /etc/init.d/postfix reload
[/html]

Now we need to test. Create a file in your server root, called – mail.php, for example. Add the following code:

[php]
<?php
$headers = ‘From: yourmail@gmail.com’ . “rn” .
‘Reply-To: yourmail@gmail.com’ . “rn” .
‘X-Mailer: PHP/’ . phpversion();
if(mail(“targetMail@gmail.com”, “Test Email”, “Email is set up”, $headers)) echo “Correct”;
else echo “Wrong”;
[/php]

And run the file by http://yourdomain.com/mail.php

If it prints “Correct”, then email is setup and check the inbox :)

You also can try sending email from your terminal. Run the following command:

[html]
sendmail targetMail@domain.com
[/html]

Then write the email contents and press Ctrl + D to send email.

Now, play with emails in your server :)

Leave a Reply

%d bloggers like this: