Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

$service = getservbyport(25, "tcp");


print("$serviceBR>\n");
?>


boolean mail(string recipient, string subject, string body, string
additional_headers)


The mail function sends email. Under UNIX it runs the sendmail shell command. Under
Windows it makes a connection to an SMTP server. The mail is sent to the address
specified in the recipient argument. You may specify multiple recipients by separating
them with commas. You must also provide a subject and a message body.Optionally, you
may provide additional headers in the fourth argument. Each extra header should be
separated by a single newline character. If the mail is sent successfully, true is returned.


On Windows, Date: and From: headers are added to the message automatically, unless
you supply them yourself.


There are a few directives in php.ini for configuring this function. For Windows you can
set the name of the SMTP host using the SMTP directive, and you can set the default
From: header with the sendmail_from directive. It's valid, of course, to point to an
SMTP server on the localhost. For UNIX, you may specify the path to your sendmail
executable, which may have an acceptable default compiled in already. You can't set up
PHP on UNIX to send mail directly to a remote SMTP host. You can configure sendmail
to relay messages to a specific host, but the instructions are outside the scope of this text.


See Chapter 18 for an example that sends attachments.


<?
//define who is to receive the mail
//(in this case, root of the localhost)
$mailTo = "root@". $SERVER_NAME;


//set the subject
$mailSubject = "Testing Mail";


//build body of the message
$mailBody = "This is a test of PHP's mail function. ";
$mailBody .= "It was generated by PHP version ";
$mailBody .= phpversion();


//add a from header
$mailHeaders = "From: php@$SERVER_NAME.com\n";


//send mail
if(mail($mailTo, $mailSubject, $mailBody, $mailHeaders))
{
print("Mail successfull sent to $mailTo.");
}

Free download pdf