• 05-02-2008, 00:07:44
    #1
    Merhaba,

    Arkadaslar asagidaki mail fonksiyonunu php.net ornek fonksiyonlardan bulmustum. Burada coklu dosya upload dosyasi ayri bir dosyada o kisimda sorun yok fakat bu fonksiyonda bir yerde ben hata yapiyorum sanirim duzenlerken sole bir hata oluyor.

    Mail alanlari duzgun doldurulmus ise maili gonderiyor hicbir mesaj vermeden bos sayfa geliyor ve ayni sekilde form eksik doldurulunca maili gondermiyor fakat herhangi bir hata mesajida yazmiyor mesela mail basarisiz gibi, yada ben yazdiramiyorum sanirim oynarken bir yerlerde hata yaptim ve simdi isin icinden cikamiyorum.

    Bu mailin amaci kullancinin doldurdugu formu ve ekledigi dosyalari topluca mailime gondermek bu islevini yerine getiriyor sorun yok. Fakat mail gonderilince gonderildi eger formda eksik bir yer varsa mail gonderilemedi mesaji vermiyor.

    Yardimci olursaniz sevinirim. Kodu aynen kullandigim gibi asagiya ekledim sadece mail kismini degistirdim o kadar.

    Kolay gelsin

    <?php if(isset($_POST['form2'])) { 
    
    $name = htmlspecialchars($_POST[name]);
    $phone = htmlspecialchars($_POST[phone]);
    $message = htmlspecialchars($_POST[message]);
    $email = $_POST[email];
    
    
    if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message']) && empty($_POST['mygiz']) &&  (eregi("^[_a-z0-9-]+(\.[a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $email))) {
    
    //========================================================
    
    function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
    {
    $eol="\r\n";
    $mime_boundary=md5(time());
    
    
    # Common Headers
    $headers .= 'From: MyName<'.$fromaddress.'>'.$eol;
    $headers .= 'Reply-To: MyName<'.$fromaddress.'>'.$eol;
    $headers .= 'Return-Path: MyName<'.$fromaddress.'>'.$eol;
    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
    $headers .= "X-Mailer: PHP v".phpversion().$eol;
    # Boundry for marking the split & Multitype Headers
    $headers .= 'MIME-Version: 1.0'.$eol;
    $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
    
    
    # Open the first part of the mail
      $msg = "--".$mime_boundary.$eol;
     
      $htmlalt_mime_boundary = $mime_boundary."_htmlalt"; //we must define a different MIME boundary for this section
      # Setup for text OR html -
      $msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;
    
      # Text Version
      $msg .= "--".$htmlalt_mime_boundary.$eol;
      $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol;
    
      # HTML Version
      $msg .= "--".$htmlalt_mime_boundary.$eol;
      $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $msg .= $body.$eol.$eol;
    
      //close the html/plain text alternate portion
      $msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
    
    
     if ($attachments !== false)
      {
        for($i=0; $i < count($attachments); $i++)
        {
          if (is_file($attachments[$i]["file"]))
          {  
            # File for Attachment
            $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
           
            $handle=fopen($attachments[$i]["file"], 'rb');
            $f_contents=fread($handle, filesize($attachments[$i]["file"]));
            $f_contents=chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode();
            $f_type=filetype($attachments[$i]["file"]);
            fclose($handle);
           
            # Attachment
            $msg .= "--".$mime_boundary.$eol;
            $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;  // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
            $msg .= "Content-Transfer-Encoding: base64".$eol;
            $msg .= "Content-Description: ".$file_name.$eol;
            $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
            $msg .= $f_contents.$eol.$eol;
          }
        }
      }
    
      # Finished
      $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.
    
    # SEND THE EMAIL
    ini_set(sendmail_from,$fromaddress);
    $gonder= mail($emailaddress, $emailsubject, $msg, $headers);
    ini_restore(sendmail_from);
    
    if ($gonder) {
    echo "<div class=\"sucess\">Basarili.</div>";
    } else { 
    echo "<div class=\"sucess\">Basarisiz.</div>";
    
     }
    
    }
    
    # kime gidecek
    $emailaddress="benim@mailim.com";
    
    # kimden
    
    $name=$_POST["name"];
    $fromaddress = $_POST["email"];
    
    # konu
    $emailsubject=$_POST["subject"];
    
    $dosyaad=$_POST["dosisim"];
    $bolerek = explode("?",$dosyaad);
    
    for($i=0;$i<count($bolerek);$i++) {
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="jpg") {
    $content[$i]="application/jpeg";
    }
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="doc") {
    $content[$i]="application/msword";
    }
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="xls") {
    $content[$i]="application/msxls";
    }
    }
    $attachments = Array(
    Array("file"=>'upload/'.$bolerek[0], "content_type"=>$content[0]),
    Array("file"=>'upload/'.$bolerek[1], "content_type"=>$content[1]),
    Array("file"=>'upload/'.$bolerek[2], "content_type"=>$content[2])
    );
    
    # mesaj
    $body= $_POST[message];
    
    send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
    
    
    
    }
    //========================================================
    } else { ?>
    
    <form method="post" action="" name="form2">
    
    <div class="formline clearfix">
    <div class="labels"><label for="name"><span class="red">*</span>Ad soyad:</label></div>
    <div class="inputs"><input name="name" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="phone">Telefon:</label></div>
    <div class="inputs"><input name="phone" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="email"><span class="red">*</span>E-mail:</label></div>
    <div class="inputs"><input name="email" class="input" type="text" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="subject"><span class="red">*</span>Konu:</label></div>
    <div class="inputs"><input name="subject" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="message"><span class="red">*</span>Message:</label></div>
    <div class="inputs"><textarea name="message" class="textarea" rows="" cols=""></textarea></div>
    </div>
    
    <div class="formline clearfix">
    <INPUT type='button' onClick="window.open('upload.php','goster', 'toolbar=no,location=no,directories=no,status=no,m enubar=no,scrollbars=yes, resizable=yes,copyhistory=no,width=300,height=400' ); return false;" value='Dosya Yükle'>
    </div>
    <input type="text" name="disim" size="20" disable>
    <input type="hidden" name="dosisim" size="20" type="hidden">
    
    <input name="form2" value="1" type="hidden">
    <input name="mygiz" value="" type="hidden">
    
    
    <input type="image" src="images/sendbut.gif" alt="send contact form"  name="Button" class="submitbtn" value="send" /><br /><br />
    (*) pola wymagane
                  
            </form> 
    <?php } ?>
  • 05-02-2008, 04:40:49
    #2
    Üyeliği durduruldu
       <?php 
       
     function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
    {
    $eol="\r\n";
    $mime_boundary=md5(time());
    
    
    # Common Headers
    $headers .= 'From: MyName<'.$fromaddress.'>'.$eol;
    $headers .= 'Reply-To: MyName<'.$fromaddress.'>'.$eol;
    $headers .= 'Return-Path: MyName<'.$fromaddress.'>'.$eol;
    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
    $headers .= "X-Mailer: PHP v".phpversion().$eol;
    # Boundry for marking the split & Multitype Headers
    $headers .= 'MIME-Version: 1.0'.$eol;
    $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
    
    
    # Open the first part of the mail
      $msg = "--".$mime_boundary.$eol;
     
      $htmlalt_mime_boundary = $mime_boundary."_htmlalt"; //we must define a different MIME boundary for this section
      # Setup for text OR html -
      $msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;
    
      # Text Version
      $msg .= "--".$htmlalt_mime_boundary.$eol;
      $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol;
    
      # HTML Version
      $msg .= "--".$htmlalt_mime_boundary.$eol;
      $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
      $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $msg .= $body.$eol.$eol;
    
      //close the html/plain text alternate portion
      $msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;
    
    
     if ($attachments !== false)
      {
        for($i=0; $i < count($attachments); $i++)
        {
          if (is_file($attachments[$i]["file"]))
          {  
            # File for Attachment
            $file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));
           
            $handle=fopen($attachments[$i]["file"], 'rb');
            $f_contents=fread($handle, filesize($attachments[$i]["file"]));
            $f_contents=chunk_split(base64_encode($f_contents));    //Encode The Data For Transition using base64_encode();
            $f_type=filetype($attachments[$i]["file"]);
            fclose($handle);
           
            # Attachment
            $msg .= "--".$mime_boundary.$eol;
            $msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;  // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
            $msg .= "Content-Transfer-Encoding: base64".$eol;
            $msg .= "Content-Description: ".$file_name.$eol;
            $msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
            $msg .= $f_contents.$eol.$eol;
          }
        }
      }
    
      # Finished
      $msg .= "--".$mime_boundary."--".$eol.$eol;  // finish with two eol's for better security. see Injection.
    
    # SEND THE EMAIL
    ini_set(sendmail_from,$fromaddress);
    $gonder= mail($emailaddress, $emailsubject, $msg, $headers);
    ini_restore(sendmail_from);
    
    if ($gonder) {
    echo "<div class=\"sucess\">Basarili.</div>";
    } else { 
    echo "<div class=\"sucess\">Basarisiz.</div>";
    
     }
    
    }
    
    if(isset($_POST['form2'])) { 
    
    $name = htmlspecialchars($_POST[name]);
    $phone = htmlspecialchars($_POST[phone]);
    $message = htmlspecialchars($_POST[message]);
    $email = $_POST[email];
    
    
    if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message']) && empty($_POST['mygiz']) &&  (eregi("^[_a-z0-9-]+(\.[a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $email))) {
    
    //========================================================
    
    
    
    # kime gidecek
    $emailaddress="benim@mailim.com";
    
    # kimden
    
    $name=$_POST["name"];
    $fromaddress = $_POST["email"];
    
    # konu
    $emailsubject=$_POST["subject"];
    
    $dosyaad=$_POST["dosisim"];
    $bolerek = explode("?",$dosyaad);
    
    for($i=0;$i<count($bolerek);$i++) {
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="jpg") {
    $content[$i]="application/jpeg";
    }
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="doc") {
    $content[$i]="application/msword";
    }
    if (strtolower(substr(strrchr($bolerek[$i], '.'), 1))=="xls") {
    $content[$i]="application/msxls";
    }
    }
    $attachments = Array(
    Array("file"=>'upload/'.$bolerek[0], "content_type"=>$content[0]),
    Array("file"=>'upload/'.$bolerek[1], "content_type"=>$content[1]),
    Array("file"=>'upload/'.$bolerek[2], "content_type"=>$content[2])
    );
    
    # mesaj
    $body= $_POST[message];
    
    send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
    
    
    
    }
    else {
    echo "boş alan bıraktınız yada mail geçersiz...";
    }
    //========================================================
    } else { ?>
    
    <form method="post" action="" name="form2">
    
    <div class="formline clearfix">
    <div class="labels"><label for="name"><span class="red">*</span>Ad soyad:</label></div>
    <div class="inputs"><input name="name" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="phone">Telefon:</label></div>
    <div class="inputs"><input name="phone" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="email"><span class="red">*</span>E-mail:</label></div>
    <div class="inputs"><input name="email" class="input" type="text" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="subject"><span class="red">*</span>Konu:</label></div>
    <div class="inputs"><input name="subject" type="text" class="input" /></div>
    </div>
    
    <div class="formline clearfix">
    <div class="labels"><label for="message"><span class="red">*</span>Message:</label></div>
    <div class="inputs"><textarea name="message" class="textarea" rows="" cols=""></textarea></div>
    </div>
    
    <div class="formline clearfix">
    <INPUT type='button' onClick="window.open('upload.php','goster', 'toolbar=no,location=no,directories=no,status=no,m enubar=no,scrollbars=yes, resizable=yes,copyhistory=no,width=300,height=400' ); return false;" value='Dosya Yükle'>
    </div>
    <input type="text" name="disim" size="20" disable>
    <input type="hidden" name="dosisim" size="20" type="hidden">
    
    <input name="form2" value="1" type="hidden">
    <input name="mygiz" value="" type="hidden">
    
    
    <input type="image" src="images/sendbut.gif" alt="send contact form"  name="Button" class="submitbtn" value="send" /><br /><br />
    (*) pola wymagane
                  
            </form> 
    <?php } ?>
    olabilirmi?
  • 05-02-2008, 14:48:21
    #3
    merhaba,

    Tesekkurler simdi calisiyor sanirim su ifleri ve donguleri nasil kullanacagimi daha iyi ogrenmem lazim. Birde birsey daha somak istiyorum konuyla alakali oldugu icin, bu fonksiyon dosyalari gecici olarak serverdaki upload klasorune atiyor ayni zamanda maile gonderiyor. Fakat ben belirli bir zaman gectikten sonra mesela form doldurulup gonderildikten 5 dk. sonra yada 1 dk. sonra ne bileyim belkide maile dosyalar gonderildikten sonra direk bu upload klasorune gelen dosyalarin silinmesini istiyorum bunu nasil yapabilirim ?