Do I need to configure anything to use PHP mail function on fresh LAMP install?

Posted November 12, 2015 46.4k views
UbuntuPHPGetting StartedEmail


I’m new to Digital Ocean and managing my own server.

Now that I’m comfortable with the Ubuntu 14.04 CLI, I’m going to set up a fresh LAMP installation.

Do I need to configure anything to get PHP’s mail function to work or should it work right from the new installation?

If there’s a guide that will help me, please link it for me.

Thanks for the help!



These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

@RoyPenrod - PHP’s mail() function requires access to the sendmail binary during compile time. If you use postfix or another alternative, you’ll need to specify this when compiling as, by default, PHP will look for sendmail and will throw an exception when compiling if it’s not available or an alternative has not been defined.

They also recommend that the path to the binary be available in your $PATH. You can run echo $PATH when logged in to the CLI to view what has been defined for the path variable. The output will look something like:


Of course, the above does not have the path to sendmail defined, though you can add an export to ~/.bashrc which will be called the next time you login, or immediately if you run source ~/.bashrc after saving the file.

You can add the path to sendmail to ~/.bashrc by running:

echo "" >> ~/.bashrc
echo "export /usr/sbin/sendmail:$PATH" >> ~/.bashrc

The first line simply adds a blank line to the end of your ~/.bashrc file (I normally do this simply to keep things neatly spaced out). The last line adds the actual export .

Wrapping it all together, you could simply do:

export SMAIL=$(which sendmail)
echo "" >> ~/.bashrc
echo "export $SMAIL:$PATH" >> ~/.bashrc
source ~/.bashrc

That’ll set SMAIL to the path where the sendmail binary is located (for the current session, which is all we need it for), add the blank line to the ~/.bashrc file, add the export to the ~/.bashrc file ($SMAIL & $PATH will expand when echo'ed in to the file) and finally call the ~/.bashrc file so that the changes take effect immediately.

  • @jtittle … I’m pretty sure I can follow your instructions, but sendmail isn’t installed yet.

    When I run which sendmail, it doesn’t return anything. And when I run sendmail –version, it gives me a list of the packages that contain sendmail.

    Can you tell which package I should install?

    I’m so new to Linux that I don’t know which one I should use. I’m going to run a low-traffic web server for my own web development portfolio on this server.

    • @RoyPenrod

      First, if you’ve not already done so, I would recommend running:

      sudo apt-get update && sudo apt-get upgrade -y

      This will update/synchronize the OS repositories for your release and then upgrade all existing packages on the server that need to be. This ensures that you’re running the latest stable packages and that any patches or security fixes that have been released since the OS image was created (by DigitalOcean) are applied.

      I’ll normally run the above command each time I login and before I install anything simply to ensure everything is as up to date as possible.

      You can use the following command to install sendmail:

      sudo apt-get install sendmail -y

      Once installed, simply run each line (from my comment above) as an individual command via the CLI, starting with:

      export SMAIL=$(which sendmail)

      (Hit Enter/Return to Execute)


      echo "" >> ~/.bashrc

      (Hit Enter/Return to Execute)

      Followed by:

      echo "export $SMAIL:$PATH" >> ~/.bashrc

      (Hit Enter/Return to Execute)

      and finally:

      source ~/.bashrc

      (Hit Enter/Return to Execute)

      That should take care of sendmail for you :-). The below is a few basics to help you with software management.

      When it comes to software management, you’ll use a series of commands.

      sudo apt-get install package-name

      … to install various software packages - specifically “package-name” in this example (similar to the above where we installed sendmail).

      sudo apt-get update

      … to synchronize system packages with the distro’s repositories

      sudo apt-get upgrade

      … to upgrade either a specific package or all packages; running the above command with only -y as an argument means upgrade all available packages while specifying a package, such as php5-fpm (as an example) will upgrade that package and it’s dependencies, but nothing else.

      sudo apt-get remove package-name

      … to remove a package named “package-name” and its dependencies, though configuration will remain in place - to fully remove a package, you’ll need to run the next command.

      sudo apt-get purge package-name

      … to completely remove a package named “package-name” (files, directories, configuration etc) - there’s no recovery option when using this command, so only use it when you are 100% sure that you want to remove a package entirely.

      sudo apt-cache search package-name

      … allows you to search through all available packages for a package named “package-name” - if no package exists, nothing will be returned - if a package does exist, the package name shown on the left is what you’ll use in place of “package-name” if you want to install it.

      sudo apt-cache search package-*

      … note the asterisk - it functions as a WildCard, meaning that the results returned will be based on “package-” instead of “package-name” - the WildCard can be used in the other commands as well (specifically install, remove and purge), though I limit it’s use primarily to searching due to the reason outlined for the purge command - this means that a search on “php5-” will return all packages that begin with “php5-” (i.e. php5-cli, php5-fpm, php5-json, php5-mcrypt etc).

      sudo apt-cache search package-name --names-only

      … this functions similar to the WildCard, except you’re not setting a WildCard - instead of using “php5-”, we can run this command on “php5” (without the - or *) and get the same results.

      • @jtittle … Thanks for the detailed reply. One last question and I think I’ve got it.

        Should I install sendmail before or after setting up DNS?


        • @RoyPenrod

          Unless you’re setting up a cluster or some sort of advanced setup from the start, there’s really not much to configure when it comes to DNS.

          The web server (Apache, NGINX, Caddy …) will handle routing for you (you don’t really need to install BIND or a similar DNS server unless something specifically requires it).

          Basic DNS configuration for just your domain would look something like:

          A           @           DROPLET_PUBLIC_IP
          A           *           DROPLET_PUBLIC_IP
          A           SERVER      DROPLET_PUBLIC_IP
          CNAME       www         yourdomain.ext.

          Where DROPLET_PUBLIC_IP should be replaced with your Droplet’s Public IP, SERVER should be replaced with your server name (without .yourdomain.ext - i.e. for, you would replace SERVER with host only) and yourdomain.ext should be replaced with your full domain name, sans http://www. (i.e. without the protocol, “sub-domain”, or . prefix).

          The CNAME www, in the example above, routes requests for www.yourdomain.ext to yourdomain.ext. Of course, you’ll also need to setup Apache, NGINX or Caddy to handle this routing. This can be done with a WildCard or a redirect (WildCard is my personal choice so I don’t have to fool around with two configurations for the same domain over a non-secure protocol). With NGINX, this is as simple as setting yourdomain.ext *.yourdomain.ext in the server block.

          You’ll notice that I have a WildCard DNS A entry as well. This basically enforces all requests to any-subdomain.yourdomain.ext to yourdomain.ext unless a server block has been configured for the requested subdomain. For instance, if the only server block (read: virtual host if dealing with Apache), is for yourdomain.ext, then all sub-domain requested will be routed to your domain instead of displaying an error. If, however, a request was made to blog.yourdomain.ext and there was a server block or virtual host setup for this sub-domain, then the request will stop at the blog sub-domain.

          I find that that the WildCard is more beneficial than setting up a dozen DNS entries for a dozen sub-domains. The web server, whether it’s Apache, NGINX or Caddy (or any other), should be capable of handling the routing and deciding whether the request is valid or not (and each of those three do so with each, as long as they are properly configured).

          As for DNS before Sendmail, if you’re using DigitalOceans Nameservers (as I reference in the above DNS Zone), the speed at which their nameservers resolve is so quick that it doesn’t really matter which you do first :-). I’ve had near-instant (i.e. 2-5 seconds) resolution on new DNS entries and I’ve only had to wait 3-5 minutes for modifications to existing.

          That said, setup depends on what you need. If all you’re going to use is PHP’s mail() function, it shouldn’t matter. If you were hosting an e-mail server which relied on MX entries and DNS resolution / connecting locally to SMTP, then making sure your DNS was properly configured would be more of an urgent matter.

          • Thanks for your time and your help, Jonathan. Your last instructions are a little over my head right now, but I’ll keep working at it until I learn it. Seriously appreciate your help.


      • Ok, I install sendmail and ran the commands you gave me, but I ran into an error when I called source ~/.bashrc :

        -bash: export: `/usr/sbin/sendmail:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier

        So what went wrong and how do I fix it?

@jtittle thank you so much for your detail explication, despite everything I got stuck on the same error as you @RoyPenrod

-bash: export: `/usr/sbin/sendmail:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier

could you explain me how you had solve it? You said “I didn’t include PATH= before the path list.” but I still could not understand how to go over this issue.

  • He means that the following line should be different:

    echo "export /usr/sbin/sendmail:$PATH" >> ~/.bashrc

    It should look like this:

    echo "export PATH=/usr/sbin/sendmail:$PATH" >> ~/.bashrc

    You should edit your ./bashrc file (by typing “sudo nano ~./bashrc” and edit the last line that says:

    echo "export /usr/sbin/sendmail:$PATH" >> ~/.bashrc

    to include “PATH=” before /usr/sbin.......

Is there any update for this question?
It no longer works.....
Have tried everything here and another dozen guides. Nothing works.