Rodomantsev
1/31/2016 - 10:23 AM

mail.php

<?php
session_start();
  if(isset($_POST["send"])){
      $from = $_POST["from"];
      $to = $_POST["to"];
      $subject = $_POST["subject"];
      $message = $_POST["msg"];

      $_SESSION["from"] = $from;
      $_SESSION["to"] = $to;
      $_SESSION["subject"] = $subject;
      $_SESSION["msg"] = $message;

      $error_from ="";
      $error_to ="";
      $error_subject ="";
      $error_msg ="";
      $error="";
      if(!preg_match("/^[a-z0-9][a-z0-9\.-_]*[a-z0-9]*@([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i", $from)){
        $error_from = "Некоректный e-mail";
        $error = true;
      }
      if(!preg_match("/^[a-z0-9][a-z0-9\.-_]*[a-z0-9]*@([a-z0-9]+([a-z0-9-]*[a-z0-9]+)*\.)+[a-z]+/i", $to)){
        $error_to = "Некоректный e-mail";
        $error = true;
      }
      if(strlen($subject)==0){
        $error_subject = " Не написана тема";
        $error = true;
      }
      if(strlen($message)==0){
        $error_msg = " Не написано сообщение";
        $error = true;
      }
      if(!$error){
        $subject = "=?utf-8?B?".base64_decode($subject)."?=";
        $headers = "From: $from\r\nReplay-to: $from\r\nContent-type: text/html; charset=utf-8\r\n";
        mail($to, $subject, $message,$headers);
        header("Location: success.php?send=1");
        exit;
      }
  }
?>
<!DOCTYPE html>
<html lang="ru">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
  <form class="myform" action="index.php" method="post">
    <table>
      <tr>
        <td>От кого:</td>
        <td>
          <input type="text" name="from" value="<?=$_SESSION["from"] ?>">
        </td>
        <td>
          <span style="color:red;"><?=$error_from; ?></span>
        </td>
      </tr>
      <tr>
        <td>Кому:</td>
        <td>
          <input type="text" name="to" value="<?=$_SESSION["to"] ?>">
        </td>
        <td>
          <span style="color:red;"><?=$error_to; ?></span>
        </td>
      </tr>
      <tr>
        <td>Тема:</td>
        <td>
          <input type="text" name="subject" value="<?=$_SESSION["subject"] ?>">
        </td>
        <td>
          <span style="color:red;"><?=$error_subject; ?></span>
        </td>
      </tr>
      <tr>
        <td>Сообщение:</td>
        <td>
            <textarea name="msg" rows="8" cols="22"><?=$_SESSION["msg"] ?></textarea>
        </td>
        <td>
          <span style="color:red;"><?=$error_msg; ?></span>
        </td>
      </tr>
      <tr>
        <td>
            <input type="submit" name="send" value="send">
        </td>
      </tr>
    </table>
  </form>
</body>
</html>