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 blanjamudah.com

Setting Up Configuration

Open postfix configuration
$sudo vim /etc/postfix/main.cf
change the line 
myhostname = localhost
to 
myhostname = blanjamudah.com
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
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 blanjamudah.com ESMTP Postfix (Ubuntu)
ehlo localhost
250-blanjamudah.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from: root@localhost
250 2.1.0 Ok
rcpt to:your_personal_email@blabla.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
Subject: My first mail
this is the content
.
250 2.0.0 Ok: queued as 5597361356
quit

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 = system@blanjamudah.com
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. 

<?php
$to = "your_email@yourdomain.com";
$subject = "subject from php script";
$message = "just testing email from my server blanjamudah.com";
$from = "system@blanjamudah.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
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 !

reference