为什么没有发送电子邮件?

时间:2015-05-18 15:48:02

标签: php forms email send

为什么这个脚本不发送电子邮件?它会在成功完成表单后重定向到成功页面,但不会发送电子邮件。我在其他网站上使用类似的代码(没有javascript和自动加载输入),并且工作正常。我试图使用静态变量$ mailto,但也没有发生。

<?php
if (session_id() == "")
{`enter code here`
   session_start();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   if (isset($_POST['captcha_code'],$_SESSION['random_txt']) && md5($_POST['captcha_code']) == $_SESSION['random_txt'])
   {
      unset($_POST['captcha_code'],$_SESSION['random_txt']);
   }
   else
   {
      echo '<b>Nesprávene opísaný kód z obrázka!</b><br>';
      echo '<a href="javascript:history.back()">Späť</a>';
      exit;
   }
}
?>
<?php
   function ValidateEmail($email)
   {
      $pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
      return preg_match($pattern, $email);
   }

   if ($_SERVER['REQUEST_METHOD'] == 'POST')
   {
      $mailto = $_POST["adresat"];
      //$mailto = 'michal.palus1@gmail.com';
      $mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
      $subject = 'Nový Feedback';
      $message = 'Napísali Vám:';
      $success_url = '/success.html';
      $error_url = '/error.html';
      $error = '';
      $eol = "\r\n";
      $boundary = md5(uniqid(time()));

      $header  = 'From: '.$mailfrom.$eol;
      $header .= 'Reply-To: '.$mailfrom.$eol;
      $header .= 'MIME-Version: 1.0'.$eol;
      $header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
      $header .= 'X-Mailer: PHP v'.phpversion().$eol;
      if (!ValidateEmail($mailfrom))
      {
         $error .= "Nesprávna emailová adresa!\n<br>";
      }

      if (!empty($error))
      {
         $errorcode = file_get_contents($error_url);
         $replace = "##error##";
         $errorcode = str_replace($replace, $error, $errorcode);
         echo $errorcode;
         exit;
      }

      $internalfields = array ("submit", "reset", "send", "captcha_code");
      $message .= $eol;
      $message .= "IP Address : ";
      $message .= $_SERVER['REMOTE_ADDR'];
      $message .= $eol;
      foreach ($_POST as $key => $value)
      {
         if (!in_array(strtolower($key), $internalfields))
         {
            if (!is_array($value))
            {
               $message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
            }
            else
            {
               $message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
            }
         }
      }

      $body  = 'This is a multi-part message in MIME format.'.$eol.$eol;
      $body .= '--'.$boundary.$eol;
      $body .= 'Content-Type: text/plain; charset=UTF-8'.$eol;
      $body .= 'Content-Transfer-Encoding: 8bit'.$eol;
      $body .= $eol.stripslashes($message).$eol;
      if (!empty($_FILES))
      {
          foreach ($_FILES as $key => $value)
          {
             if ($_FILES[$key]['error'] == 0 && $_FILES[$key]['size'] <= $max_filesize)
             {
                $body .= '--'.$boundary.$eol;
                $body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
                $body .= 'Content-Transfer-Encoding: base64'.$eol;
                $body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
                $body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
             }
         }
      }
      $body .= '--'.$boundary.'--'.$eol;
      if ($mailto != '')
      {
         mail($mailto, $subject, $body, $header);
      }
      $search = array("ä", "Ä", "ö", "Ö", "ü", "Ü", "ß", "!", "§", "$", "%", "&", "/", "\x00", "^", "°", "\x1a", "-", "\"", " ", "\\", "\0", "\x0B", "\t", "\n", "\r", "(", ")", "=", "?", "`", "*", "'", ":", ";", ">", "<", "{", "}", "[", "]", "~", "²", "³", "~", "µ", "@", "|", "<", "+", "#", ".", "´", "+", ",");
      $replace = array("ae", "Ae", "oe", "Oe", "ue", "Ue", "ss");
      foreach($_POST as $name=>$value)
      {
         $name = str_replace($search, $replace, $name);
         $name = strtoupper($name);
         $form_data[$name] = $value;
      }
      header('Location: '.$success_url);
      exit;
   }
?>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>   
<script type="text/javascript">
function Validatecontact(theForm)
{
   var regexp;

   regexp = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;
   if (!regexp.test(theForm.Editbox3.value))
   {
      alert("Zlý formát emailu");
      theForm.usermail.focus();
      return false;
   }
   if (theForm.usermail.value == "")
   {
      alert("Zlý formát emailu");
      theForm.usermail.focus();
      return false;
   }
   return true;
}
</script>
<script type="text/javascript">
$(function(){
$(".search").keyup(function() 
{ 
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
    $.ajax({
    type: "POST",
    url: "search.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#result").html(html).show(); 
    }
    });   
}return false;    
}); 


jQuery("#result").live("click",function(e){ 
    var $clicked = $(e.target);
    var $name = $clicked.find('.name').html();
    var decoded = $("<div/>").html($name).text();

  var dataString = 'search='+ decoded;

  if(decoded!='')
  {
    $.ajax({
    type: "POST",
    url: "search2.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#text").html(html).show(); 
    }
    });   
  }
});

jQuery("#result").live("click",function(e){ 
    var $clicked = $(e.target);
    var $name = $clicked.find('.name').html();
    var decoded = $("<div/>").html($name).text();
  var $email = $clicked.find('.email').html();
    var decoded2 = $("<div/>").html($email).text();
    $('#searchid').val(decoded); 
  $('#adresat').val(decoded2); 
});
jQuery(document).live("click", function(e) { 
    var $clicked = $(e.target);
    if (! $clicked.hasClass("search")){   
    jQuery("#result").fadeOut(); 
  document.getElementById('text').style.visibility='visible';
    }
});
$('#searchid').click(function(){
    jQuery("#result").fadeIn();
  document.getElementById('text').style.visibility='hidden';
});


});
</script>
</head>

<body>

<div class="content">

<form name="contact" method="post" accept-charset="UTF-8" action="<?php echo basename(__FILE__); ?>" enctype="multipart/form-data" id="Form1" onsubmit="return Validatecontact(this)">

<span style="color:#000000;font-family:Arial;font-size:13px;">Prevádzka :</span>
<input type="text" class="search" id="searchid" placeholder="Search for place" />  <br />
<div id="result">
</div>

<span style="color:#000000;font-family:Arial;font-size:13px;">Váš Email:</span>
<input type="text" id="usermail" name="email" value="">                            <br />

<span style="color:#000000;font-family:Arial;font-size:13px;">Opíšte kód z obrázka:</span></div>
<img src="captcha1.php" alt="Nový obrázok" title="Nový obrázok" style="cursor:pointer;" onclick="this.src='captcha1.php?'+Math.random()">
<input type="text" id="Captcha1Edit" name="captcha_code" value="">

<br />
<br />

<input type="text" id="adresat" name="adresat" value="" disabled>    
<input type="submit" id="submit" name="" value="Odoslať">
</form>

</div>

非常感谢您的帮助。

0 个答案:

没有答案