This article teaches you how to make Ubuntu PHP localhost mail function work, using PHP to send mail from localhost is easy. First, you need to install some PEAR mail packages.
In terminal, run the following command one by one:
sudo pear install mail
sudo pear install Net_SMTP
sudo pear Auth_SASL
sudo pear install mail_mime
After installing the pear packages, you need to install and configure postfix, first, run the following command to install postfix:
sudo apt-get install postfix
after installation, a configuration window will be displayed, you need to configure it:
Step 1: Choose Internet Site
Step 2: Enter localhost at the input area
For the rest of the steps, just follow the default settings. After complete the configuration, it’s time to test the PHP script. Create a PHP file called sendmail.php at your localhost, and copy and paste the code below into it (remember to change the email addresses to yours)
<?
include('Mail.php');
include('Mail/mime.php');
// Constructing the email
$sender = "shi <shichuanr@msn.com>";
$recipient = "Leigh <shichuanr@gmail.com>";
$subject = "Test Email";
$text = 'This is a text message.';
$html = '<html><body><p>This is a html message</p></body></html>';
$crlf = "\n";
$headers = array(
'From' => $sender,
'Return-Path' => $sender,
'Subject' => $subject
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
// Set body and headers ready for base mail class
$body = $mime->get();
$headers = $mime->headers($headers);
// SMTP params
$smtp_params["host"] = "localhost"; // SMTP host
$smtp_params["port"] = "25"; // SMTP Port (usually 25)
// Sending the email using smtp
$mail =&amp; Mail::factory("smtp", $smtp_params);
$result = $mail->send($recipient, $headers, $body);
if($result == 1)
{
echo("Your message has been sent!");
}
else
{
echo("Your message was not sent: " . $result);
}
?>
Now you should be able to receive the mail sent from your localhost. Good Luck! :-)













































#1 by musiclover on February 13, 2009 - 6:09 pm
Hello. And Bye.
#2 by Cowboyrob on February 19, 2009 - 8:10 am
Hello. And Bye.
#3 by Iordan Bedjev on April 3, 2009 - 12:02 pm
Thank you.
#4 by Вячеслав Сенников on May 26, 2009 - 10:58 pm
На таких громких заголовках и подобной шумихе можно делать и не такие успехи :)
#5 by shai on July 23, 2009 - 2:21 pm
Thanks for the guide!
You forgot “install” on #3
sudo pear Auth_SASL
should be
sudo pear install Auth_SASL
#6 by juiev on November 2, 2009 - 3:48 am
hi, i couldn’t do that. i’ve installed the packages above but still can’t mail. these are the installed packages:
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.2 stable
Console_Getopt 1.2.3 stable
Mail 1.1.14 stable
Net_SMTP 1.3.1 stable
Net_Socket 1.0.8 stable
PEAR 1.7.1 stable
Structures_Graph 1.0.2 stable
please help, thanks
#7 by Halim on December 14, 2009 - 6:24 pm
I’m getting an error from the code:
Parse error: syntax error, unexpected ‘;’, expecting T_PAAMAYIM_NEKUDOTAYIM in /opt/lampp/htdocs/crystalpalace/sendmail.php on line 28
The code is pasted from the article above.
Line 28: $mail =& Mail::factory(“smtp”, $smtp_params);
Hj
#8 by Serg on December 25, 2009 - 4:27 am
Thanks! Helped very much!
#9 by Surinder on January 5, 2010 - 10:52 am
Thank you. That worked just fine.
#10 by powertower on January 20, 2010 - 8:07 am
You need to remove “amp;amp;”
#11 by Thithi32 on February 6, 2010 - 12:53 pm
Thank you! it worked fine for me.
#12 by Ben on March 19, 2010 - 2:06 pm
This looks nice however it does not work for me, I get the correct message saying email sent.
Where are these included files coming from, where can i get them.
Also, surely i cannot just enter my email in the from variable because i could enter anybodies email couldn’t I.
Do i have to tell my pc or the script that i am the owner of that email account anywhere ?
sorry if i’ve missed anything obvious in the tutorial
#13 by maurice on April 5, 2010 - 11:59 am
Thanks mate. Worked for me.
Iv been trying to sort this out for ages.
#14 by Gonzo on May 18, 2010 - 7:52 pm
Hi, that’s great. Many thanks.
Line 28 needs correcting. It should be:
$mail = Mail::factory(“smtp”, $smtp_params);
The two “&” are removed
#15 by isogashii on July 29, 2010 - 7:58 am
Oh! at last. you can’t imagine how frustrating this was.
lately, I am working on scripts that should deliver notifications to emails, and no results with sendmail, installed postfix (without PEAR packages, i am using php mail funciton) and I can see in the email that the end server is replying now.
Thanks a lot.