Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Sending Email

One of my absolute favorite things about PHP is how easy it is to send an email. On a properly configured server, the process is a simple as using the mail() function.

mail ($to, $subject, $body);

The $to value should be an email address or a series of addresses, separated by commas. The $subject value will create the email's subject line and $body is where you put the contents of the email. Use the newline character (\n) within double quotation marks when creating your body to make the text go over multiple lines.

The mail() function takes a fourth, optional parameter for additional headers. This is where you could set the From, Reply-To, Cc, Bcc, and similar settings. For example,

mail ('phpmysql@dmcinsights.com', 'Question regarding Script 3.12', $body, 'From:person@address.com');

To use multiple headers of different types in your email, separate each with \r\n:

$headers = 'From: John@Doe.com\r\n';
$headers .= 'Cc: Jane@Doe.com, Joe@Doe.com\r\n';
mail ('phpmysql@dmcinsights.com', 'Question regarding Script 3.12', $body, $headers);

To send email:

  1. Open register.php (refer to Script 3.11) in your text editor.
  2. Where the registration process would occur (line 47), add (Script 3.12)
    $body = "Thank you for registering with our site!\nYour username is '{$_POST['username']}' and your password is '{$_POST['password1']} '.\n\nSincerely,\nUs";
    mail ($_POST['email'],'Thank you for registering!', $body, 'From: admin@site.com');
    
    To send the email, I first build up my email body and assign that to the $body variable, then I use the mail() function. In Chapter 8, "Security," you'll see how to use regular expressions to help validate a submitted email address.
  3. If desired, change the registration's echo statement to reflect the new change in the script.
    echo '<p>You are now registered. An email has been sent to your email address confirming the information.</p>';
    
  4. Save the file as register.php, upload to your Web server, and test in your Web browser ( Figures 3.17 , 3.18 , and 3.19 ).
    03fig17.gif

    Figure 3.17 Make sure you use a real email address here to receive the sent message.

    03fig18.gif

    Figure 3.18 The registration page now sends an email to the new user.

    03fig19.gif

    Figure 3.19 The email I received from PHP's mail() function.

Example 3.12. PHP's mail() function is surprisingly easy to use.

1   <?php # Script 3.12 - register.php
2
3   // Set the page title and include the HTML header.
4   $page_title = 'Register!';
5   include ('./header.inc');
6
7   if (isset($_POST['submit'])) { // Handle the form.
8
9       // Check for a name.
10      if (strlen($_POST['name']) > 0) {
11          $name = TRUE;
12      } else {
13          $name = FALSE;
14          echo '<p>You forgot to enter your name!</p>';
15      }
16
17      // Check for an email address.
18      if (strlen($_POST['email']) > 0) {
19          $email = TRUE;
20      } else {
21          $email = FALSE;
22      echo '<p>You forgot to enter your email address!</p>';
23      }
24
25      // Check for a username.
26      if (strlen($_POST['username']) > 0) {
27          $username = TRUE;
28      } else {
29          $username = FALSE;
30          echo '<p>You forgot to enter your user name!</p>';
31      }
32
33      // Check for a password and match against the confirmed password.
34      if (strlen($_POST['password1']) > 0) {
35          if ($_POST['password1'] == $_POST['password2']) {
36                  $password = TRUE;
37          } else {
38              $password = FALSE;
39              echo '<p>Your password did not match the confirmed password!</p>';
40          }
41      } else {
42          $password = FALSE;
43          echo '<p>You forgot to enter your password!</p>';
44      }
45
46      if ($name && $email && $username && $password) { // If everything's okayOKS.
47          // Register the user.
48
49          // Send an email.
50          $body = "Thank you for registering with our site!\nYour username is 
'{$_POST['username']}' and your password is '{$_POST['password1']}'.\n\nSincerely,\nUs";

   51          mail ($_POST['email'], 'Thank you for registering!', $body, 'From: admin@site.
com');
52
53          echo '<p>You are now registered. An email has been sent to your email address 
confirming the information.</p>';
54      } else {
55          echo '<p>Please go back and try again.</p>';
56      }
57
58  } else { // Display the form.
59  ?>
60
61  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
62  <fieldset><legend>Enter your information in the form below:</legend>
63
64  <p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40" /></p>
65
66  <p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="60" /></p>
67
68  <p><b>User Name:</b> <input type="text" name="username" size="20" maxlength="40" /></p>
69
70  <p><b>Password:</b> <input type="password" name="password1" size="20" maxlength="40" /></p>
71
72  <p><b>Confirm Password:</b> <input type="password" name="password2" size="20" 
maxlength="40" /></p>
73  </fieldset>
74
75  <div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
76
77  </form><!-- End of Form -->
78
79  <?php
80  }
81  include ('./footer.inc'); // Include the HTML footer.
82  ?>
  • + Share This
  • 🔖 Save To Your Account