PHP Email

We can use PHP mail function to send mail to different address. This function is required for sending newsletters, designing feedback forms, signup welcome messages and in many more uses.

Description :

bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

The mail() function
requires three strings representing the recipient of the mail, the mail subject, and
the message.

mail() returns false if it encounters an error and true if operation was successful.

If you are running PHP on a UNIX system, mail() will use Sendmail. On other
systems, the function will connect to a local or remote SMTP mail server. You should
set this using the SMTP directive in the php.ini file.

Besides the 3 main parameters , there are also 2 optional parameters.

If a fourth string argument is passed, this string is inserted at the end of the header.

This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

With the fifth parameter you can set additional command line parameters to the actual mailer.

The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting.

This option is rarely used.

Important : When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

Sample code :

$recipient .= "camelia@aisoftware.ro";

$subject = "Test PHP mail";

$message .= "This is a test mail \n";

/* additional header pieces for errors, From cc's, bcc's, etc */

$headers .= "From: Camelia \n";

$headers .= "To-Sender: \n";

$headers .= "X-Mailer: PHP\n"; // mailer

$headers .= "X-Priority: 1\n"; // Urgent message!

$headers .= "Return-Path: \n"; // Return path for errors


$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type

$headers .= "cc:ccemail@yahoo.com\n"; // CC to

$headers .= "bcc:bccemail@yahoo.com\n"; // BCCs to


mail($recipient, $subject, $message, $headers);

Note:

Something you may have noticed from the example is that the From line ended with \n. This is acutally a very important character when sending e-mail. It is the new line character and tells PHP to take a new line in an e-mail. It is very important that this is put in after each header you add so that your e-mail will follow the international standards and will be delivered.

admin – Thu, 2005 – 07 – 07 11:29