• 4
name Punditsdkoslkdosdkoskdo

Setup ubuntu server to send the mail()

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.

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.

  • 1
Reply Report

Once you've got your server up and running the last thing you want to be doing is logging onto it every 5 minutes to check everything is ok. That said, some people actually enjoy doing this and if you're one of those people then you can skip this page completely. For the rest of you, read on!

If you've been following this guide from the start you'll know I've configured my server to send me an email if either the CPU or any of the hard drives exceed my pre-determined temperatures. I also get emails when any of my Torrents have downloaded. I also get emails to tell me about the state of my SnapRAID Array. Infact you can get email alerts on pretty much anything you desire!

Whilst you can install and configure a fully featured email system you really don't need to do so if all you want to do is send emails and not receive them too. I use ssmtp which is a simple Mail Transfer Agent (MTA). It's not rocket science to install ssmtp, it's one simple command, although configuring it can be a bit more challenging.

  • 1
Reply Report

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).

  • 1
Reply Report