The following guide worked on an Ubuntu 11.04 machine.
I will assume you have an Apache2 server up and running without problems. If this is not the case, search the Ubuntu forums for LAMP.
Begin with installing the packages sendmail, php-mail and postfix:
sudo apt-get install sendmail php-mail postfix
You might get some prompts during the installation, but just follow the instructions.
Uncomment and change the sendmail_path in your file /etc/php5/apache2/php.ini. The line should now look like this:
sendmail_path = /usr/sbin/sendmail -i -t
This is needed because PHP won’t find the sendmail command otherwise.
You can either use the PHP mail() function now, or use a custom PHP library for sending emails. I recommend the latter. For a fast setup, use the Swift Mailer. Download the library, put it in your public website folder and start making scripts. Here is an example script that uses Swift:
// --- Create the Transport ---
$transport = Swift_SmtpTransport::newInstance('smtp.yourdomain.com', 587)
// --- Create the Mailer using your created Transport ---
$mailer = Swift_Mailer::newInstance($transport);
// --- Create a message ---
$message = Swift_Message::newInstance($subject)
->setFrom(array('firstname.lastname@example.org' => 'Your Name'))
->setBody('This is the message body.');
// --- Send message ---
$status = $mailer->send($message);
if($status) echo "Success!";
else echo "Failure";
Put this code into a PHP file, change the domain and SMTP server data, and run it. Hopefully, an email should be sent to the given e-mail address.