|[ Team LiB ]|
Sending Mail with the mail() Function
PHP can automate the sending of Internet mail for you. 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. In the following fragment, we send an email:
$to = "firstname.lastname@example.org"; $subject = "hi"; $message = "just a test message! "; mail( $to, $subject, $message ) or print "Could not send mail";
If you are running PHP on a Unix system, mail() uses a mail application such as Sendmail. On other systems, the function connects to a local or remote SMTP mail server. You should set this using the SMTP directive in the php.ini file.
You are not limited to the mail headers implied by the mail() function's required arguments. You can include as many mail headers as you want in an optional fourth string argument. These should be separated by CRLF characters ('\r\n'). In the following example, we include a From field in our mail message, as well as an X-Priority header that some clients recognize:
$to = "email@example.com"; $from = "firstname.lastname@example.org"; $subject = "hi"; $message = "just a test message! "; mail( $to, $subject, $message, "From: $from\r\nX-Priority: 1 (Highest)" ) or print "Could not send mail";
As of PHP 4.0.5, an additional fifth optional parameter can be used. This enables you to pass command-line-style arguments directly to the mailer.
|[ Team LiB ]|