mail() allows you to send email directly from a PHP script. recipient can be either a single email address or a comma-delimited list of addresses. If you want to set extra headers - for instance, in order to use Cc: or Bcc: - these may be placed in a newline-delimited string in the extra_headers parameter. As of PHP 4.0.5, you can also specify extra arguments to the system mail program in the extra_arguments parameter. For example, this is useful if you want to set the envelope From: header so that it doesn't look like email is coming from your web server daemon. If you do this, however, you may want to add your daemon process to the trusted users list in your sendmail configuration (if using sendmail); otherwise, sendmail will add an X-Authentication-Warning: header to the email, indicating that an untrusted user has modified the envelope.
Because you can modify the headers, it's possible to send attachments or HTML emails using PHP; however, detailing this usage is beyond the scope of this book. There are classes available on the web that will do this for you; check out the code gallery at Zend.com to start with, and be sure to thoroughly read RFC 2049.
TRUE is returned if the function completes successfully; otherwise, FALSE is returned. However, this gives no indication as to whether the email ever reached its destination. This function fails if any of recipient , subject , or message is left out, or if the system's mail program fails for some reason.
/* When the following code was executed, I received this email: * * From: Apache httpd <firstname.lastname@example.org> * To: email@example.com * Subject: This is a test * Date: Mon, 20 Aug 2001 16:33:17 -0700 * * Hi there, * * This is a test message. Please disregard. */ $address = 'firstname.lastname@example.org'; $subject = 'This is a test'; $message = 'Hi there, This is a test message. Please disregard. '; mail($address, $subject, $message); /* Now, tell it that I want it to look like it's from me, using * the extra_arguments parameter. * The email I got back from this one was as follows. However, * because this was run on a web page and the httpd is not a * sendmail-trusted user, the resulting email also included this * header: * X-Authentication-Warning: shanna.outlander.ca: www set sender to email@example.com using -f * * From: firstname.lastname@example.org * To: email@example.com * Subject: This is a test * Date: Mon, 20 Aug 2001 16:47:56 -0700 * * * Hi there, * * This is a test message. Please disregard. * */ mail($address, $subject, $message, '', '-firstname.lastname@example.org'); /* Send the same message, but to a blind carbon-copy list. */ mail($address, $subject, $message, 'Bcc: email@example.com, firstname.lastname@example.org');
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.