Php ile bir eki gönderme

4 Cevap php

Ben bir eki göndermek için izin veren bir e-posta formunu oluşturmak için çalışıyorum. Ben PHP kitap ve çeşitli web sitelerinde bir araya kod parçalı var.

Ben eki bozuk biter ve "$ comments" geçmesi görünmüyor bu formu kullanarak e-posta göndermek.

İşte kullanıyorum kodu:

<?php
$to = $_POST['to'];
$from = $_POST['from'];
$re = $_POST['re'];
$comments= $_POST['comments'];

$att = $_FILES['att'];
$att_path= $_FILES['att']['tmp_name'];
$att_name = $_FILES['att']['name'];
$att_size = $_FILES['att']['size'];
$att_type = $_FILES['att']['type'];

//open, read, then close the file
$fp = fopen( $att_path, "rb" );
$file = fread( $fp, $att_size );
fclose($fp);

#create a boundary string
$num = md5(time());
$str = "==Multipart_Boumdary_x{$num}x";

//encode the data for transit
$file = chunk_split(base64_encode($file));

//define header
$hdr = "MIME-Versio: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed; ";
$hdr .= "boundary=\"{$str}\"\r\n";
$hdr .= "From: $from \r\n";

#define message
$msg = "This is a multi-part message in MIME format\r\n\n";
$msg .= "--{$str}\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n";
$msg .= "$comments\r\n\n";
$msg .= "--{$str}\r\n";

#define the non-text attatchment
$msg .= "Content-Type: {$att_type}; ";
$msg .= "name=\"{$att_name}\"\r\n";
$msg .= "Content-Disposition: attachment; ";
$msh .= "filename=\"{$att_name}\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "$file\r\n\n";
$msg .= "--{$str}";

#sendmail
$ok = mail( $to, $re, $msg, $hdr );
if( $ok ) echo "OK";

?>

Peki, ben ne yanlış yapıyorum?

Teşekkürler!

4 Cevap

Geçiş için dosya kodlamak için bu deneyin.

Satırı değiştirin:

  $file = chunk_split(base64_encode($file));

Ile:

  //Encode the file for transit
  $content = '';
  $fp = fopen($att_path, 'r'); 
  do { 
    $data = fread($fp, 8192); 
    if (strlen($data) == 0) break; 
    $content .= $data; 
  } while (true); 
  $file = chunk_split(base64_encode($content));

Ben bunu daha kolay PHPMailer http://phpmailer.worxware.com/index.php?pg=phpmailer gibi bir şey kullanmak bulacağını düşünüyorum

$str = "==Multipart_Boumdary_x{$num}x";

Umarım (sınır yazım fark) denedim:

$str = "==Multipart_Boundary_x{$num}x";