Saturday, July 19, 2008

Send email in PHP

On linux, a tool called sendmail can be used to send emails. You don't need a SMTP server to send emails out. An interesting thing about sendmail is that you can not set some common email fields easily. For example, you can not set email subject directly on command line by using a switch. Instead following syntax should be used:

sendmail destination@domain.com
Subject: This is the subject.
Main body goes here
.

In PHP, users can send email easily because PHP relies on sendmail. See this page for instructions. Main configuration options in php.ini:

Mail configuration options
Name Default Changeable Changelog
SMTP "localhost" PHP_INI_ALL  
smtp_port "25" PHP_INI_ALL Available since PHP 4.3.0.
sendmail_from NULL PHP_INI_ALL  
sendmail_path "/usr/sbin/sendmail -t -i" PHP_INI_SYSTEM