Selasa, 27 Agustus 2013

How To Install Postfix in Ubuntu 12.04 LTS and Integrate with PHP

Install Postfix

Login to your linux box, we will install popular MTA (Mail Transfer Agent) Postfix in Ubuntu 12.04 LTS.

$sudo apt-get install postfix
When a dialog box appear, Select “Internet Site”. Then in hostname, fill in with your FQDN hostname, in my case it's

Setting Up Configuration

Open postfix configuration
$sudo vim /etc/postfix/
change the line 
myhostname = localhost
myhostname =
save the file then let's reload our postfix
$sudo /etc/init.d/postfix reload

right now we will test our Postfix MTA by sending test email to our personal email. These bold text is made from our keyboard, write that bold text followed by ENTER button. Be careful with (.) dot character in the of the message and type 'quit' when you're finished. If your install is correct and no errors, you will receive the test email immediately to your personal inbox email (if it's not arrived, check your spam folder).

risnandar@www:~$ telnet localhost 25
Connected to localhost.
Escape character is '^]'.
220 ESMTP Postfix (Ubuntu)
ehlo localhost
250-SIZE 10240000
250 DSN
mail from: root@localhost
250 2.1.0 Ok
250 2.1.5 Ok
354 End data with <CR><LF>.<CR><LF>
Subject: My first mail
this is the content
250 2.0.0 Ok: queued as 5597361356

This is the screenshot email i receive from our test server

thats conclude our Postfix installation. Now we will proceed to integrate Postfix with PHP. In this server im using Nginx and PHP FPM

PHP and Postfix

After our Postfix installed, our next step is allow PHP using Postfix as our default MTA. First, edit your php.ini to make changes.
$sudo vim /etc/php5/fpm/php.ini
If you're not sure where is the location for php.ini, you can try to output the phpinfo() then look at location of php.ini file.

search for setting sendmail_path and change to this line
sendmail_path = "/usr/sbin/sendmail -t -i"
if you want to change the default from_to email when sending email with PHP script, modify this line to your email address such as system@yourdomain or noreply@yourdomain
sendmail_from =
there is no need to restart our PHP, now we will try to send our email using PHP. We don't have to use web browser but we will command line PHP instead. For this example just create one PHP file for our tutorial
$vim testmail.php

write this script in that file then save it to your home folder. Don't forget to change $to or the script will not deliver our test email. 

$to = "";
$subject = "subject from php script";
$message = "just testing email from my server";
$from = "";
$headers = "From:" . $from;
echo "Mail Sent.";

Last test ! Run our new PHP script through command line 
$php testmail.php
you will receive text 'Mail Sent', wait a minutes before our mail arrived in our inbox
This is screenshot when i receive the email

This tutorial do not include how to create or setting another email accounts because i just need Postfix to send mailing list with Amazon SES
That's it. Your PHP is ready to roll !


2 komentar: