Location: PHPKode > scripts > EmailIt > emailit-1-5/emailit.php
<?php
/*
EmailIt version 1.5
Made by Jan Hlavenka ©2006
Dou you like this script and want to use it? Please send me URL where is used.

Jan HLAVENKA
hide@address.com
http://jan.hlavenka.cz
Author is not responsible for problems or damages made by this script.

This PHP script is usefull for sending email from HTML page to your mailbox and mobile phone.
Add on: IP address, name of sender's computer and type of browser in attachment

change list:
1.5:  script extended for sending email in format: plain text, HTML, HTML+plain text
      easier configuration via definitions - below
      processing all $_POST parameters
      optional attachment with PC info (simple or extended)
      easy encoding change
1.4:  sending parameters via $_POST variable
1.3:  header bug fixed
1.2:  updated to RFC specification
	  added host information
	  input field check
*/

// --- configuration ---

// name of HTML input: <input type="...." name="name">
define('INPUT_NAME', 'name');        // name
define('INPUT_EMAIL', 'email');      // email
define('INPUT_SUBJECT', 'subject');  // subject
define('INPUT_MESSAGE', 'message');  // message

define('OUTPUT_NAME', 'My website');         // Your name or name of your web page
define('OUTPUT_EMAIL', 'hide@address.com'); // Your email
define('OUTPUT_WWW_SHOW', true);             // show page when email sent
define('OUTPUT_WWW', 'http://your_domain.com/sent.html'); // web page when email sent

define('DEFAULT_NAME', 'no name');           // default name if HTML one is clear
define('DEFAULT_EMAIL', 'hide@address.com');  // default emailif HTML one is clear
define('DEFAULT_SUBJECT', 'WWW message');    // default subject if HTML one is clear

define('EMAIL_TYPE', 3);          // email type: 1 - text; 2 - html; 3 - html+text

define('ENCODE', false);          // encoding
define('ENCODING', 'ISO-8859-2'); // encoding style

define('INPUT_SIMPLE', true);     // true - simple message (using getMessage and getHTMLMessage)
                                  // false - show all $_POST parameters (useful in debugging or troubles)
define('PC_INFO', true);          // PC info attachment
define('PC_INFO_SIMPLE', false);  // true - simple info
                                  // false - full info
define('MOBILE', false);          // copy of email sent on mobile
define('MOBILE_EMAIL', 'hide@address.com'); // mobile email
define('CONFIRM_EMAIL', false);   // sending 2 emails - 1. is same as before
                                  //  2. is an confirmation email for visitor (you are sender and visitor is recipient)
                                  //  useful when implementing online shop


// --- script -------------------------------------------------------

function getPost($variable) {
  if (isset($_POST[$variable])) return $_POST[$variable];
}

// text message body
function getMessage() {
  if (INPUT_SIMPLE) {
    $out = "Name:\t" . getPost(INPUT_NAME) . "\n"
         . "E-mail:\t" . getPost(INPUT_EMAIL) . "\n"
         . "Message:\n" . getPost(INPUT_MESSAGE) . "\n";
  } else {
    foreach($_POST as $key => $value)
      $out .= '$_POST[' . $key . "] => \t" . $value . "\n";
  }

  return $out;
}

// HTML message body
function getHTMLMessage() {
  $out ='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' . "\n"
      . '<html><head><meta content="text/html;charset=' . ENCODING . '" http-equiv="Content-Type"></head>' . "\n"
      . '<body bgcolor="#ffffff" text="#000000">' . "\n"
      . '<p style="font-family:verdana, arial, helvetica, sans-serif; font-size:12px">' . "\n";
  if (INPUT_SIMPLE) {
    $out .= '<table width="80%" border="0" cellspacing="0" cellpadding="0">' . "\n"
         . ' <tbody style="font-family:verdana, arial, helvetica, sans-serif; font-size:12px">' . "\n"
         . '  <tr>' . "\n"
         . '   <td><b>Name:</b> ' . getPost(INPUT_NAME) . '</td>' . "\n"
         . '   <td><b>E-mail:</b> ' .  getPost(INPUT_EMAIL) . '<br><br></td>' . "\n"
         . '  </tr>'. "\n"
         . '  <tr>' . "\n"
         . '   <td colspan="2"><b>Message:</b> ' . getPost(INPUT_MESSAGE) . '</td>'. "\n"
         . '  </tr>'. "\n"
         . ' </tbody>'. "\n"
         . '</table>'. "\n";
  } else {
    $out .= '<table width="80%" border="0" cellspacing="0" cellpadding="0">' . "\n"
         . ' <tbody style="font-family:verdana, arial, helvetica, sans-serif; font-size:12px">' . "\n";

    foreach($_POST as $key => $value) {
      $out .= '  <tr>' . "\n"
           . '   <td><b>' . $key . ':</b></td><td>' . $value . "</td>\n"
           . '  </tr>'. "\n";
    }
    $out .= ' </tbody>'. "\n"
         . '</table>'. "\n";
  }
  $out .= '</p>' . "\n"
      . '</body>' . "\n"
      . '</html>' . "\n";

  return $out;
}

// PC info
function getPCInfo() {
  if (PC_INFO_SIMPLE) {
   $out .= "PC name: " . getenv("REMOTE_HOST") . "\n"
         . "IP address: " . getenv("REMOTE_ADDR") . "\n"
         . "Port: " . getenv("REMOTE_PORT") . "\n"
         . "Browser: " . getenv("HTTP_USER_AGENT") . "\n"
	 . "Connection: " . getenv("HTTP_CONNECTION") . "\n";
  } else {
    foreach($_SERVER as $key => $value)
      $out .= $key . ": \t" . $value . "\n";
  }

  return $out;
}

// --- message -----------------------------------------------------

if (getPost(INPUT_NAME)=='') $_POST[INPUT_NAME] = DEFAULT_NAME;
if ((getPost(INPUT_EMAIL)=='@')or(getPost(INPUT_EMAIL)=='')) $_POST[INPUT_EMAIL] = DEFAULT_EMAIL;

// from - sender
$headstd  = 'From: ';
if (ENCODE) $headstd .= '=?' . ENCODING . '?B?' . base64_encode(getPost(INPUT_NAME)) . '?=';
else $headstd .= getPost(INPUT_NAME);
$headstd .= '<' . getPost(INPUT_EMAIL) . ">\n";
$headstd .= 'Reply-To: ';
if (ENCODE) $headstd .= '=?' . ENCODING . '?B?' . base64_encode(getPost(INPUT_NAME)) . '?=';
else $headstd .= getPost(INPUT_NAME);
$headstd .= '<' . getPost(INPUT_EMAIL) . ">\n";

// from - you (response email)
if (CONFIRM_EMAIL) {
  $headrev  = 'From: ';
  if (ENCODE) $headrev .= '=?' . ENCODING . '?B?' . base64_encode(OUTPUT_NAME) . '?=';
  else $headrev .= OUTPUT_NAME;
  $headrev .= '<' . OUTPUT_EMAIL . ">\n";
  $headrev .= 'Reply-To: ';
  if (ENCODE) $headrev .= '=?' . ENCODING . '?B?' . base64_encode(OUTPUT_NAME) . '?=';
  else $headrev .= OUTPUT_NAME;
  $headrev .= '<' . OUTPUT_EMAIL . ">\n";
}

// header
$header  = "MIME-Version: 1.0\n";
switch (EMAIL_TYPE) {
  case 1:
  case 2:
     if (PC_INFO) $header .= "Content-Type: multipart/mixed; boundary=\"=_JanHlavenka_mail_system\"\n";
     else {
       if (EMAIL_TYPE==1)
         $header .= "Content-Type: text/plain; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit";
       else
         $header .= "Content-Type: text/html; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit";
     }
     break;
  case 3:
     if (PC_INFO)
        $header .= "Content-Type: multipart/mixed; boundary=\"=_JanHlavenka_mail_system_mix\"\n";
     else
        $header .= "Content-Type: multipart/alternative; boundary=\"=_JanHlavenka_mail_system\"\n";
     break;
}
$header .= "X-Mailer: JanHlavenka mailer v.1.5\n";


// subject
if (isset($_POST[INPUT_SUBJECT])) $subject = getPost(INPUT_SUBJECT);
else $subject = DEFAULT_SUBJECT;
if (ENCODE) $subject = '=?' . ENCODING . '?B?' . base64_encode($subject) . '?=';

// message
switch (EMAIL_TYPE) {
  case 1:
  case 2:
     if (PC_INFO) {
       $message .= "--=_JanHlavenka_mail_system\n";
       if (EMAIL_TYPE==1)
         $message .= "Content-Type: text/plain; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit\n\n";
       else
         $message .= "Content-Type: text/html; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit\n\n";
     }

     if (EMAIL_TYPE==1) $message .= getMessage();
     else $message .= getHTMLMessage();

     if (PC_INFO) {
       $message .= "--=_JanHlavenka_mail_system\n"
                .  "Content-Type: application/octet-stream; name=\"info.txt\"\n"
                .  "Content-Transfer-Encoding: 7bit\n"
                .  "Content-Disposition: attachment; filename=\"info.txt\"\n\n";
       $message .= getPCInfo();
       $message .= "--=_JanHlavenka_mail_system--";
     }
     break;
  case 3:
     if (PC_INFO) {
       $message .= "--=_JanHlavenka_mail_system_mix\n"
                .  "Content-Type: multipart/alternative; boundary=\"=_JanHlavenka_mail_system\"\n\n";
     }
     $message .= "--=_JanHlavenka_mail_system\n"
              .  "Content-Type: text/plain; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit\n\n";
     $message .= getMessage();
     $message .= "--=_JanHlavenka_mail_system\n"
              .  "Content-Type: text/html; charset=" . ENCODING . "\n" . "Content-Transfer-Encoding: 7bit\n\n";
     $message .= getHTMLMessage();
     $message .= "--=_JanHlavenka_mail_system--\n";
     if (PC_INFO) {
       $message .= "\n"
                .  "--=_JanHlavenka_mail_system_mix\n"
                .  "Content-Type: application/octet-stream; name=\"info.txt\"\n"
                .  "Content-Transfer-Encoding: 7bit\n"
                .  "Content-Disposition: attachment; filename=\"info.txt\"\n\n";
       $message .= getPCInfo();
       $message .= "--=_JanHlavenka_mail_system_mix--\n";
     }
     break;
}

// mobile phone message
if (MOBILE)
  mail(MOBILE_EMAIL, getPost(INPUT_SUBJECT), getPost(INPUT_NAME) . ', ' . getPost(INPUT_MESSAGE));

// sending email
if (mail(OUTPUT_EMAIL, $subject, $message, $headstd . $header)) {
  // confirmation email
  if (CONFIRM_EMAIL) mail(getPost(INPUT_EMAIL), $subject, $message, $headrev . $header);
  // jump to sent page
  if (OUTPUT_WWW_SHOW) header('Location: ' . OUTPUT_WWW);
}

?>
Return current item: EmailIt