Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried searching a lot but unable to find how to actually setup a ubuntu server, so that I can send mails through php using mail() function in php.

I have apache2, mysql and php5 installed on my server.

Thank You.

share|improve this question

migrated from Mar 4 '10 at 10:03

This question came from our site for professional and enthusiast programmers.

up vote 45 down vote accepted

I also agree there is a lack of tutorials for people who just need a script to send, e.g. error emails, and don't need a full-blown mail server.

First, if postfix not already installed do:

sudo apt-get install postfix

It prompts with a couple of questions. For the first I chose "Internet site"; for a machine behind a firewall I might choose smarthost instead. For the second question it defaults to the machine name; I appended a domain name that I control (so I can set DNS for it later, should I need to).

At this point you should be able to use "mail" from the commandline to send a test. (I usually follow instructions on first, otherwise I have to use the -f flag to /usr/bin/sendmail. I also like to create /etc/aliases with entries for root and my normal user, and then run newaliases)

Then under /etc/php5/conf.d create a file (e.g. mailconfig.ini) with these contents:

sendmail_from = ""
sendmail_path = "/usr/sbin/sendmail -t -i -f"

Change to your email address. They mean all email will look like it is sent by you, which can help prevent it being rejected. This is sufficient for just sending error emails to a developer.

(The above instructions tested on Ubuntu 10.04, 11.04, 11.10, 12.04)

P.S. As pointed out by razzed in the comments, mail is not always there (e.g. on Ubuntu 11.10 it is missing). This does not actually affect the above instructions, you only need mail for the test, and you can use sendmail just as well for that. But mail is also useful for reading email, so it is usually worth installing it, with: apt-get install mailutils (as root).

share|improve this answer
Thanks! This worked perfectly. Tested on Ubuntu 12.04 LTS x64. – Trent Scott Sep 9 '12 at 21:39
This resolves find using NGINX too! And PHP5-FPM – TheBlackBenzKid Nov 2 '12 at 11:06
Note on my host, the mail command-line utility is not installed on a clean Ubuntu install, and installing PostFix does not install it. Try sudo apt-get install mailutils to get the mail command, or use alternatives such as Mutt, or ssmtp – razzed Feb 20 '13 at 15:27
@razzed Thanks, I updated the answer. – Darren Cook Feb 21 '13 at 0:30
Finally! A straight forward and simple solution. Works on Ubuntu 14.04. – Knyri Jun 9 '14 at 17:12

You'll need to setup the ubuntu server to be able to send mail ... as an example, you could install and configure postfix.

The Ubuntu documentation has a nice page at

share|improve this answer
any tutorial you can link me to? – Shishant Mar 3 '10 at 14:02
added a tutorial link – Dominik Mar 3 '10 at 14:13

Either go through the painful process of setting up a mail server directly on your server (postfix, etc) or use a library that will allow you to connect directly to a smtp server.

share|improve this answer

You should first install xmail package, and if it still does not work, have a look over here.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.