0

Why isn't this script sending an email? It will redirect after sucessfully completed form to success page, but email is not delivered. I'm using similar code on other site (without javascript and autoload inputs) and there is working correctly. I tried to use static variable $mailto but nothink happend too.

<?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>

Thanks a lot for help.

b4hand
  • 9,550
  • 4
  • 44
  • 49
Michal
  • 35
  • 7
  • 1
    That is a lot of code. Do you have error reporting turned on for your PHP code? Is the server configured to send out emails ? – Maximus2012 May 18 '15 at 15:50
  • 1
    Hi, first of all do you have a mail server configured correctly on your website? – Sourabh Kumar Sharma May 18 '15 at 16:03
  • If I change mailto variable to address like 'test@test.com' it will send email. But this script gets email, where to post from database - $mailto = $_POST["adresat"]; In that input "adresat" is written value before sending and where I want to send email, but it wont send it. – Michal May 20 '15 at 11:55

0 Answers0