PHP-Mail Functions

PHP contains two dedicated mail functions, which are built into PHP by default. The mail() function allows for the sending of email directly from a script, and ezmlm_hash() provides a hash calculation useful for interfacing a script to an  mailing list system.
The mail() function requires an installed and working email subsystem for sending mail. The program to be used is defined by configuration directives in the php.ini file. A common pitfall is that these are not set up correctly, rendering mail() inoperable. Note that the first two directives are for use only on Windows systems; the third is for use only on Unix-type systems.
  • SMTP: The host to which to connect to send mail. Can be either a fully qualified hostname or an IP address. Used only on Windows systems.
  • sendmail_from: The email address from which sent mail should appear to have been sent. Used only on Windows systems.
  • sendmail_path: Full pathname of the mail executable to run when sending mail. This can also include command-line arguments. The default is created during the precompilation configuration: For example, if a sendmail executable is found in /usr/sbin, the default will be usr/sbin/sendmail -t -i. If no sendmail executable is found, one must be specified here. Used only on Unix-type systems.

    mail

    bool mail(string recipient, string subject, string message, 
     [string extra_headers], [string extra_arguments]) 
    recipient
    Address of the recipient
    subject
    Message subject
    message
    Body of the message
    extra
    Extra headers for the message
    extra_arguments
    Extra arguments for the underlying mail program (PHP 4.0.5 and later)
    Sends a message via email.
    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.

    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 
     
     * 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, '', 'ftorpden@php.net'); 
    
    /* Send the same message, but to a blind carbon-copy list. */ 
    mail($address, $subject, $message, 'Bcc: fif@bar.brz, king.ttrfgrut@abc.com');