Sending Email


Your PHP scripts can send email as well. To enable email, edit this section in php.ini:

  [mail function] ; For Win32 only. SMTP = localhost ; For Win32 only. sendmail_from = me@localhost.com ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path = 

Windows users should list the SMTP host they want to use (such as mail.ispname.com or smtp.ispname.com) and set their return address. Linux and Unix users may not have to make any changes, but if things don't work as they are, uncomment the sendmail_path and set it to the appropriate value (such as /usr/bin/sendmail).

To actually send mail, you use the mail function:

 mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]]) 

This sends mail to the email address in to, with subject subject and message message. You can also set additional mail headers and parameters to mail. You can see an example in phpemail.html, Example 9-7 and in phpemail.php, Example 9-8.

Example 9-7. Sending email, phpemail.html
 <HTML>     <HEAD>         <TITLE>             Send me some email         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Send me some email</H1>             <BR>             <FORM METHOD=POST ACTION="phpemail.php">                Please enter your message and click Submit:                <BR>                <TEXTAREA NAME="message" COLS="50" ROWS="5"></TEXTAREA>                <BR>                <BR>                <INPUT TYPE="SUBMIT" VALUE="Submit">             </FORM>           </CENTER>     <BODY> </HTML> 

The phpemail.html page displays a text area for the text to send in the email, as you see in Figure 9-8.

Figure 9-8. Accepting email.


The message is sent using the mail function, as shown in phpemail.php, Example 9-8.

Example 9-8. Sending email, phpemail.php
 <HTML>     <HEAD>         <TITLE>Sending email</TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Sending email</H1>             Your email was sent.             <BR>             <?php                 $result = mail("steve@ispname.com", "Web mail",                     $_REQUEST["message"]);             ?>          </CENTER>     <BODY> </HTML> 

And that's itwe've sent some email from a PHP script. Very cool.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net