15.4 Sending E-mail


You want to send an e-mail from your PHP script.

Technique

Use PHP's built-in mail() function:

 <?php /* submitted data is $email, $name,$subject, $message and $to */ mail($to,     $subject,     $message,     "From: $email\r\nReply-to: $email\r\n"); ?> 

Comments

The mail() function takes three required arguments: the e-mail address to which you want to send the e-mail, the subject of the e-mail, and the body of the message. The fourth argument is optional, but it enables you to give any extra headers, which should be separated by the standard <CRLF> ( "\r\n" ).

The preceding script sends an e-mail based on the user 's input. A form that might trigger this script would be something like the following ( assuming the script in the solution is named mailer.php):

 <form action="mailer.php" method="POST"> To: <input type="text" name="to"><br> From:  <input type="text" name="email"><br> Subject: <input type="text" name="subject"><br> Message Body: <br> <textarea rows="10" cols="40" name="message"></textarea><br> <input type="submit" value="Send your message"> </form> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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