Sending Email

I l @ ve RuBoard

Among the numerous things you can do with great ease in PHP is send emails.

 mail("mailto", "subject", "body"); 

The mail() function uses the server's email application (such as sendmail on Unix) to send out the messages. This function can take another argument through which you can add more details to the email, including a From address, email priority, and carbon copy addresses.

 mail ("mailto", "subject", "body",     "From: fromaddress"); 

To send email with PHP:

  1. Create a new PHP document in your text editor (Script 13.9):

     <?php 
    Script 13.9. In PHP you can send emails with only one function mail(). I have wrapped the mail() call within different conditionals to make sure that the email is only sent when appropriate.

    graphics/13sc09.jpg

  2. Assign the page title and include the header file.

     $PageTitle = "Sending Emails"; require ("header.php"); 
  3. Create a conditional to process the form if it has been submitted.

     if ($BeenSubmitted) { 
  4. Check to make sure that a recipient's email address has been submitted and, if so, send the email.

     if ($MailTo) {          if (mail($MailTo, $Subject,             $Body, "From: $MailFrom")) {              print ("<B><CENTER>                 <FONT COLOR=BLUE>                 Your email has been                 successfully sent!                 </FONT></CENTER>                 </B>\n");          }else{             print ("<B><CENTER>                <FONT COLOR=RED>                Your email was not                successfully sent due                to a system error!                </FONT></CENTER>                </B>\n");          } 

    I've put the mail() function within a conditional so that a message will be displayed indicating whether or not the mail was successfully sent.

  5. Complete the conditionals.

     }else{       print ("<B><CENTER>          <FONT COLOR=RED>Please          enter the recipient's mail          to address!</FONT>          </CENTER></B>\n");    } } 
  6. Close the PHP section and create an HTML form that takes two email addresses, the subject, the body, and a hidden value to determine whether or not the form has been submitted.

     ?> <FORM ACTION="email.php" METHOD=POST> Recipient's  Email Address:     <INPUT TYPE=TEXT NAME="MailTo"    SIZE="50"><BR> Your Email Address: <INPUT TYPE=TEXT    NAME="MailFrom" SIZE="50"><BR> Email Subject: <INPUT TYPE=TEXT    NAME="Subject" SIZE="80"><BR> Email Body:<TEXTAREA NAME="Body"    ROWS="10" COLS="50"> </TEXTAREA><P> <INPUT TYPE=HIDDEN NAME=BeenSubmitted    VALUE=TRUE> <INPUT TYPE=SUBMIT NAME="SUBMIT"    VALUE="Submit!"> 
  7. Close the form, then include the footer file.

     </FORM> <? require ("footer.php"); ?> 
  8. Save the script as email.php, upload it to the server, and test it in your Web browser (Figures 13.11, 13.12, 13.13, and 13.14).

    Figure 13.11. This very simple HTML form, combined with the power of PHP, will allow you to send emails from your Web browser.

    graphics/13fig11.gif

    Figure 13.12. If the PHP script was able to send the email, a message will be printed stating such.

    graphics/13fig12.jpg

    Figure 13.13. If you forget to enter a recipient's email address, the script will not try to send the email and will just print an error message instead.

    graphics/13fig13.jpg

    Figure 13.14. This is the email I received in Eudora (an email client) after submitting the form in Figure 13.11.

    graphics/13fig14.gif

Tip

It is possible to send emails with attachments, although that requires far more sophisticated coding (normally involving objects). Fortunately a number of programmers have already developed workable solutions which are available for use. See Appendix C, PHP Resources, for more information.


I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

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