PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mail

Name

mail — Sends a message via email.

Synopsis

bool mail(recipient, subject, message[, extra_headers][, extra_arguments]);
string recipient: Address of the recipient
string subject: Message subject
string message: Body of the message
string extra_headers (optional): Extra headers for the message
string extra_arguments (optional): Extra arguments for the underlying mail program (PHP 4.0.5 and later)

Returns

TRUE on success; FALSE on failure

Description

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.

Version

PHP 3, PHP 4

Example

Example 659. Send email from a PHP script

/* When the following code was executed, I received this email:
 *
 * From: Apache httpd <www@pinc.com>
 * To: torben@php.net 
 * Subject: This is a test 
 * Date: Mon, 20 Aug 2001 16:33:17 -0700 
 *
 * Hi there, 
 *
 * This is a test message. Please disregard. 
 */
$address = 'torben@php.net';
$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 torben@php.net using -f 
 *
 * From: torben@php.net 
 * To: torben@php.net 
 * 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, '', '-ftorben@php.net');

/* Send the same message, but to a blind carbon-copy list. */
mail($address, $subject, $message, 'Bcc: foo@bar.baz, kilgore.trout@hiho.com');



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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.