Skip navigation

Beginning PHP, Apache, MySQL Web Development

Chapter 10 Code - E-mailing with PHP

Try It Out 1

firstmail.php - Try It Out 1

<?php
mail("your@e-mailaddress.com", "Hello World", "Hi, world. Prepare for our arrival.
We're starving!");
?>

Try It Out 2

postcard.php - Try It Out 2

<html>
<head>
<title>Enter E-mail Data</title>
</head>
<body>
<form name="theform" method="post" action="pc_sendmail.php">
<table>
  <tr>
    <td>To:</td>
    <td><input type="text" name="to" size="50"></td>
  </tr>
  <tr>
    <td>From:</td>
    <td><input type="text" name="from" size="50"></td>
    </tr>
  <tr>
    <td>Subject:</td>
    <td><input type="text" name="subject" size="50"></td>
  </tr>
  <tr>
    <td valign="top">Message:</td>
    <td>
      <textarea cols="60" rows="10" name="message">Enter your message here</textarea>
  </td>
  </tr>
  <tr>
    <td></td>
    <td>
      <input type="submit" value="Send">
      <input type="reset" value="Reset the form">
    </td>
  </tr>
</table>
</form>
</body>
</html>

pc_sendmail.php - Try It Out 2

<html>
<head>
<title>Mail Sent!</title>
</head>
<body>
<?php
$to = $_POST["to"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = "From: " . $from . "\r\n";
$mailsent = mail($to, $subject, $message, $headers);
if ($mailsent) {
  echo "Congrats! The following message has been sent: <br><br>";
  echo "<b>To:</b> $to<br>";
  echo "<b>From:</b> $from<br>";
  echo "<b>Subject:</b> $subject<br>";
  echo "<b>Message:</b><br>";
  echo $message;
} else {
  echo "There was an error...";
}
?>
</body>
</html>

Try It Out 4

pc_sendmail.php - Try It Out 4

<html>
<head>
<title>HTML Mail Sent!</title>
</head>
<body>
<?php
$to = $_POST["to"];
$from = $_POST["from"];

$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "From: " . $from . "\r\n";
$mailsent = mail($to, $subject, $message, $headers);
if ($mailsent) {
  echo "Congrats! The following message has been sent: <br><br>";
  echo "<b>To:</b> $to<br>";
  echo "<b>From:</b> $from<br>";
  echo "<b>Subject:</b> $subject<br>";
  echo "<b>Message:</b><br>";
  echo $message;
} else {
  echo "There was an error...";
}
?>
</body>
</html>

Try It Out 5

postcard.php - Try It Out 5

<html>
<head>
<title>Enter Data</title>
</head>
<body>
<form name="theform" method="post" action="pc_sendmail.php">
<table>
  <tr>
    <td>To:</td>
    <td><input type="text" name="to" size="50"></td>
  </tr>
  <tr>
    <td>From:</td>
    <td>
      <input type="text" name="from" size="50">
    </td>
  </tr>
  <tr>
    <td>CC:</td>
    <td>
      <input type="text" name="cc" size="50">
    </td>
  </tr>
  <tr>
   <td>Bcc:</td>
   <td><input type="text" name="bcc" size="50"></td>
  </tr>
  <tr>
   <td>Subject:</td>
   <td><input type="text" name="subject" size="50"></td>
  </tr>
  <tr>
  <td valign="top">Message:</td>
   <td>
     <textarea cols="60" rows="10" name="message">Enter your message here</textarea>
   </td>
  </tr>
  <tr>
   <td></td>
   <td>
     <input type="submit" value="Send">
     <input type="reset" value="Reset the form">
   </td>
  </tr>
</table>
</form>
</body>
</html>

pc_sendmail.php - Try It Out 5

<html>
<head>
<title>Multipart Mail Sent!</title>
</head>
<body>
<?php
$to = $_POST["to"];
$cc = $_POST["cc"];
$bcc = $_POST["bcc"];
$from = $_POST["from"];
$subject = $_POST["subject"];
$messagebody = $_POST["message"];
$boundary = "==MP_Bound_xyccr948x==";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
$headers .= "CC: " . $cc . "\r\n";
$headers .= "BCC: " . $bcc . "\r\n";
$headers .= "From: " . $from . "\r\n";
$message = "This is a Multipart Message in MIME format\n";
$message .= "--$boundary\n";
$message .= "Content-type: text/html; charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $messagebody . "\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $messagebody . "\n";
$message .= "--$boundary--";
$mailsent = mail($to, $subject, $message, $headers);
if ($mailsent) {
  echo "Congrats! The following message has been sent: <br><br>";
  echo "<b>To:</b> $to<br>";
  echo "<b>From:</b> $from<br>";
  echo "<b>Subject:</b> $subject<br>";
  echo "<b>Message:</b><br>";
  echo $message;
} else {
  echo "There was an error...";
}
?>
</body>
</html>

Try It Out 6

conn_comic.php - Try It Out 6

<?php
$conn = mysql_connect("yourserver", "joeuser", "yourpass");
mysql_select_db("yourdatabase", $conn);
?>

db_insertpics.php - Try It Out 6

<?php
require("./includes/conn_comic.php");
$path = "http" . ($_SERVER["HTTPS"]=="on"?"s":"") .
  "://" . $_SERVER['SERVER_NAME'] .
  strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
$imagepath = $path . "postcards/";
$imgURL = array('punyearth.gif', 'grebnok.gif', 'sympathy.gif',
  'congrats.gif');
$imgDESC = array('Wish you were here!', 'See you soon!',
  'Our Sympathies', 'Congratulations!');

for ($i=0; $i<4; $i++) {
  $sql = "INSERT INTO images ( images.img_url , images.img_desc )
          VALUES ( '$imagepath$imgURL[$i]', '$imgDESC[$i]')";
  $success = mysql_query($sql, $conn) or die(mysql_error());
}
echo "done."
?>

Try It Out 7

db_makeconfirm.php - Try It Out 7

<?php
$conn = mysql_connect("yourserver", "joeuser", "yourpass");
mysql_select_db("postcard", $conn);
$sql = <<<EOD
CREATE TABLE confirm (
  id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
  validator VARCHAR (32) NOT NULL,
  to_e-mail VARCHAR (100) NOT NULL,
  toname VARCHAR (50) NOT NULL,
  from_e-mail VARCHAR (100) NOT NULL,
  fromname VARCHAR (50) NOT NULL,
  bcc_e-mail VARCHAR (100),
  cc_e-mail VARCHAR (100),
  subject VARCHAR (255),
  postcard VARCHAR (255) NOT NULL,
  message text
)
EOD;
$query = mysql_query($sql, $conn) or die(mysql_error());
echo "Table <i>confirm</i> created."
?>

postcard.php - Try It Out 7

<html>
<head>
<title>Enter E-mail Data</title>
</head>
<body>
<form name="theform" method="post" action="sendconf.php">
<center>
<table width="640" border="0" cellpadding="4" cellspacing="0">
 <tr>
  <td colspan="4"><H2>Postcard Sender</H2></td>
 </tr>
 <tr bgcolor="#CCCCCC">
  <td>To:</td>
  <td><input type="text" name="toname" size="30"></td>
  <td>e-mail:</td>
  <td><input type="text" name="to" size="40"></td>
 </tr>
 <tr>
  <td>From:</td>
  <td><input type="text" name="fromname" size="30"></td>
  <td>e-mail:</td>
  <td><input type="text" name="from" size="40"></td>
 </tr>
 <tr bgcolor="#CCCCCC">
  <td>CC:</td>
  <td><input type="text" name="cc" size="40"></td>
  <td>Bcc:</td>
  <td><input type="text" name="bcc" size="40"></td>
 </tr>
 <tr>
  <td colspan="2">Choose a Postcard:
   <select name="postcard[]" onchange="changepostcard(this.value)">
   <?php
    include("./includes/conn_comic.inc");
    $sql = "SELECT * FROM images ORDER BY img_desc";
    $images = mysql_query($sql, $conn) or die(mysql_error());
    $iloop = 0;
    while ($imagearray = mysql_fetch_array($images)) {
      $iloop++;
      //$iid = $imagearray['id'];
      $iurl = $imagearray['img_url'];
      $idesc = $imagearray['img_desc'];
      if ($iloop == 1) {
        echo "<option selected value=\"$iurl\">
         $idesc</option>\n";
        $image_url = $imagearray['img_url'];
      } else {
        echo "<option value=\"$iurl\">$idesc</option>\n";
      }
    }
   ?>
   </select><br>
  </td>
  <td>Subject:</td>
  <td><input type="text" name="subject" size="40"></td>
 </tr>
 <tr>
  <td colspan="2"><img src="<?php echo($image_url)?>" width=320
     height=240 border=0 id="postcard"></td>
  <td valign="top"> </td>
  <td align="right">
   <textarea cols="30" rows="12" name="message"
    >Enter your message here</textarea>
   <input type="submit" value="Send">
   <input type="reset" value="Reset the form">
  </td>
 </tr>
</table>
</center>
</form>
<script language="Javascript">
function changepostcard(imgurl) {
 window.document.theform.postcard.src = imgurl;
}
</script>
</body>
</html>

pc_sendconf.php - Try It Out 7

<html>
<head>
<title>HTML Mail Sent!</title>
</head>
<body>
<?php
$to = $_POST["to"];
$toname = $_POST["toname"];
$cc = $_POST["cc"];
$bcc = $_POST["bcc"];
$from = $_POST["from"];
$fromname = $_POST["fromname"];
$subject = $_POST["subject"];
if (!empty($_POST["postcard"])) {
  foreach($_POST["postcard"] as $value) {
    $postcard = $value;
  }
}
$messagebody = $_POST["message"];
$boundary = "==MP_Bound_xyccr948x==";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
$headers .= "From: no-reply@postcardorama.com\r\n";
$html_msg .= "<center>";
$html_msg .= "<table width=\"500\" border=0 cellpadding=\"4\">";
$html_msg .= "<tr><td>Greetings, $toname!";
$html_msg .= "</td></tr><tr><td>";
$html_msg .= "$fromname has sent you a postcard today.<br>Enjoy!";
$html_msg .= "</td></tr><tr><td align=\"center\">";
$html_msg .= "<img src=\"$postcard\" border=\"0\">";
$html_msg .= "</td></tr><tr><td align=center>";
$html_msg .= $messagebody . "\n";
$html_msg .= "</td></tr></table></center>";
$temp=gettimeofday();
$msec=(int)$temp["usec"];
$msgid = md5(time() . $msec);
$conn = mysql_connect("yourserver", "joeuser", "yourpass");
mysql_select_db("postcard", $conn);
$sql = "INSERT INTO confirm (validator, to_e-mail, toname, from_e-mail, fromname,
bcc_e-mail, cc_e-mail, subject, postcard, message) VALUES (\"$msgid\", \"$to\",
\"$toname\", \"$from\", \"$fromname\", \"$bcc\", \"$cc\", \"$subject\",
\"$postcard\", \"$messagebody\")";
$query = mysql_query($sql, $conn) or die(mysql_error());
$confirmsubject = "Please Confirm your postcard";
$confirmmessage = "Hello " . $fromname . ",\n\n";
$confirmmessage .= "Please click on the link below to confirm that
  you would like to send this postcard:\n\n";
$confirmmessage .= $html_msg . "\n\n";
$confirmmessage .= "<a href=\"http://localhost/wrox/confirmmail.php
  ?id=$msgid\">Click here to confirm</a>";
$textconfirm = "Hello " . $fromname . ",\n\n";
$textconfirm .= "Please visit the following URL to confirm your
  postcard:\n\n";
$textconfirm .= "http://localhost/wrox/confirmmail.php
  ?id=\"$msgid\"";
$message = "This is a Multipart Message in MIME format\n";
$message .= "--$boundary\n";
$message .= "Content-type: text/html; charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $confirmmessage . "\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $textconfirm . "\n";
$message .= "--$boundary--";
$mailsent = mail($from, $confirmsubject, $message, $headers);
if ($mailsent) {
  echo "Here is the postcard you wish to send.<br>";
  echo "A confirmation e-mail has been sent to $from.<br>";
  echo "Open your e-mail and click on the link to confirm that you would
        like to send this postcard to $toname.<br><br>";
  echo "<b>Subject:</b> $subject<br>";
  echo "<b>Message:</b><br>";
  echo $html_msg;
} else {
  echo "There was an error sending the e-mail.";
}
?>
</body>
</html>

pc_confirm.php - Try It Out 7

<?php
$id = $_GET['id'];
require('./includes/conn_comic.inc');
$sql = "SELECT * FROM confirm WHERE validator = '$id'";
$query = mysql_query($sql, $conn) or die(mysql_error());
$pcarray = mysql_fetch_array($query);
if (!is_array($pcarray)) {
  echo "Oops! Nothing to confirm. Please contact
       your administrator";
  exit;
}
$to = $pcarray["to_e-mail"];
$toname = $pcarray["toname"];
$from = $pcarray["from_e-mail"];
$fromname = $pcarray["fromname"];
$bcc = $pcarray["bcc_e-mail"];
$cc = $pcarray["cc_e-mail"];
$subject = $pcarray["subject"];
$postcard = $pcarray["postcard"];
$messagebody = $pcarray["message"];

$boundary = "==MP_Bound_xyccr948x==";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
if (!$cc == "") {
  $headers .= "CC: " . $cc . "\r\n";
}
if (!$bcc == "") {
  $headers .= "BCC: " . $bcc . "\r\n";
}
$headers .= "From: " . $from . "\r\n";

$html_msg .= "<center>";
$html_msg .= "<table width=\"500\" border=0 cellpadding=\"4\">";
$html_msg .= "<tr><td>Greetings, $toname!";
$html_msg .= "</td></tr><tr><td>";
$html_msg .= "$fromname has sent you a postcard today.<br>Enjoy!";
$html_msg .= "</td></tr><tr><td align=\"center\">";
$html_msg .= "<img src=\"$postcard\" border=\"0\">";
$html_msg .= "</td></tr><tr><td align=center>";
$html_msg .= $messagebody . "\n";
$html_msg .= "</td></tr></table></center>";

$message = "This is a Multipart Message in MIME format\n";
$message .= "--$boundary\n";
$message .= "Content-type: text/html; charset=iso-8859-1\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $html_msg . "\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$message .= $messagebody . "\n";
$message .= "--$boundary--";
$mailsent = mail($to, $subject, $message, $headers);
?>
<html>
<head>
<title>Postcard Sent!</title>
</head>
<body>
<?php
if ($mailsent) {
  echo "Congrats! The following message has been sent: <br><br>";
  echo "<b>To:</b> $to<br>";
  echo "<b>From:</b> $from<br>";
  echo "<b>Subject:</b> $subject<br>";
  echo "<b>Message:</b><br>";
  echo $html_msg;
} else {
  echo "There was an error...";
}
?>
</body>
</html>

viewpostcard.php - Try It Out 7

<?php
$id = $_GET['id'];
require('./includes/conn_comic.inc');
$sql = "SELECT * FROM confirm WHERE validator = '$id'";
$query = mysql_query($sql, $conn) or die(mysql_error());
$pcarray = mysql_fetch_array($query);
$path = "http" . ($_SERVER["HTTPS"]=="on"?"s":"") .
  "://" . $_SERVER['SERVER_NAME'] .
  strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
if (!is_array($pcarray)) {
 echo "Oops! Can't find a postcard. Please contact your
   administrator.";
 exit;
}
$to = $pcarray["to_e-mail"];
$toname = $pcarray["toname"];
$from = $pcarray["from_e-mail"];
$fromname = $pcarray["fromname"];
$bcc = $pcarray["bcc_e-mail"];
$cc = $pcarray["cc_e-mail"];
$subject = $pcarray["subject"];
$postcard = $pcarray["postcard"];
$messagebody = $pcarray["message"];

$html_msg .= "<table width=\"500\" border=0 cellpadding=\"4\">";
$html_msg .= "<tr><td>Greetings, $toname!";
$html_msg .= "</td></tr><tr><td>";
$html_msg .= "$fromname has sent you a postcard today.<br>Enjoy!";
$html_msg .= "</td></tr><tr><td align=\"center\">";
$html_msg .= "<img src=\"$postcard\" border=\"0\">";
$html_msg .= "</td></tr><tr><td align=center>";
$html_msg .= $messagebody . "\n";
$html_msg .= "</td></tr></table>";

echo <<<EOD
  <html>
  <head>
  <title>Viewing postcard for $toname</title>
  </head>
  <body>
  $html_msg
  </body>
  </html>
EOD;
?>