<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
* Form Mail is a PHP script which provides you with the ability to create any type
* of form (e.g. contact, support, feedback, etc.) on your website and have the
* information sent to one or any number of specified email addresses. Form Mail is
* flexible enough to handle any type of form you can imagine - from a simple
* contact form, to a more complex enquiry form and more!
*
* PHP versions 4 and 5
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*
* @category   PHP
* @package    PHP Form Mail
* @author     Robert Love <robert@signified.net>
* @copyright  2007 Robert Love - Signified
* @license    http://www.gnu.org/licenses/gpl.txt
* @version    1.0
* @link       http://signified.net/software/php-form-mail/
*/

function formMail()
{
    // To
    $to = implode(', ', $_POST['to']);

    // Subject
    $subject = isset($_POST['subject']) ? trim($_POST['subject']) : '';

    // Body
    $body = '';

    if (isset($_POST['body'])) {

        foreach ($_POST['body'] as $key => $value) {

            $value = trim($value);

            $body .= empty($value) ? "$key:\r\nNOT SPECIFIED\r\n\r\n" : "$key:\r\n$value\r\n\r\n";

        }

    } else {

        $body .= "\r\n";

    }

    // Headers
    $headers = '';
    $headers .= isset($_POST['from']) ? 'From: ' . trim($_POST['from']) . "\r\n" : '';
    $headers .= isset($_POST['cc']) ? 'Cc: ' . implode(', ', $_POST['cc']) . "\r\n" : '';
    $headers .= isset($_POST['bcc']) ? 'Bcc: ' . implode(', ', $_POST['bcc']) . "\r\n" : '';

    // Send
    @mail($to, $subject, wordwrap($body, 70), $headers);

    return true;
}

function isEmail($email)
{
    if (!ereg('^[^@]{1,64}@[^@]{1,255}$', $email)) {
        return false;
    }
    $ary_email = explode('@', $email);
    $ary_local = explode('.', $ary_email[0]);
    for ($i = 0; $i < count($ary_local); $i++) {
        if (!ereg('^(([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-][A-Za-z0-9!#$%&\'*+/=?^_`{|}~\.-]{0,63})|("[^(\\|")]{0,62}"))$', $ary_local[$i])) {
            return false;
        }
    }
    if (!ereg('^\[?[0-9\.]+\]?$', $ary_email[1])) {
        $ary_domain = explode('.', $ary_email[1]);
        if (count($ary_domain) < 2) {
            return false;
        }
        for ($i = 0; $i < count($ary_domain); $i++) {
            if (!ereg('^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$', $ary_domain[$i])) {
                return false;
            }
        }
    }
    return true;
}

$xhtml = '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>Form Mail</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css" media="screen">@import url("http://signified.net/stylesheets/core/simple.css");</style></head><body>%s</body></html>';

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    printf($xhtml, '<h1>Form Mail Error</h1><p>Please change the <code>method</code> attribute value of your form to &quot;post&quot;:</p><pre>&lt;form action=&quot;formmail.php&quot; method=&quot;post&quot;&gt;</pre>');
    exit;
}

if (!isset($_POST['to'])) {
    printf($xhtml, '<h1>Form Mail Error</h1><p>Please specify the <em>To</em> email address:</p><pre>&lt;input type=&quot;hidden&quot; name=&quot;to[]&quot; value=&quot;me@myemail.net&quot; /&gt;</pre>');
    exit;
}

foreach ($_POST['to'] as $key => $value) {
    if (!isEmail($value)) {
        printf($xhtml, '<h1>Form Mail Error</h1><p>The specified <em>To</em> email address (' . $value . ') is invalid.</p>');
        exit;
    }
}

if (isset($_POST['cc'])) {
    foreach ($_POST['cc'] as $key => $value) {
        if (!isEmail($value)) {
            printf($xhtml, '<h1>Form Mail Error</h1><p>The specified <em>Cc</em> email address (' . $value . ') is invalid.</p>');
            exit;
        }
    }
}

if (isset($_POST['bcc'])) {
    foreach ($_POST['bcc'] as $key => $value) {
        if (!isEmail($value)) {
            printf($xhtml, '<h1>Form Mail Error</h1><p>The specified <em>Bcc</em> email address (' . $value . ') is invalid.</p>');
            exit;
        }
    }
}

if (!isset($_POST['redirect'])) {
    printf($xhtml, '<h1>Form Mail Error</h1><p>Please specify the <em>Redirect URL</em>:</p><pre>&lt;input type=&quot;hidden&quot; name=&quot;redirect&quot; value=&quot;thankyou.html&quot; /&gt;</pre>');
    exit;
}

formMail();

header('location: ' . $_POST['redirect']);

?>
