2013-02-25 2 views
2

Fullname 및 첨부 파일로만 작동하는이 스크립트가 있습니다. 그러나 "tel"와 같은 필드를 하나 더 추가하면 첨부 파일을 제외하고 스크립트가 전송됩니다. 어떤 사람이 제발 나를 도울 수 있습니까?php 첨부 파일이있는 이메일 보내기

<?php 
    if(isset($_POST['submit'])) 
    { 
     //The form has been submitted, prep a nice thank you fullname 
     $output = '<h1>Thanks for your file and fullname!</h1>'; 
     //Set the form flag to no display (cheap way!) 
     $flags = 'style="display:none;"'; 

     //Deal with the email 
     $to = 'email here'; 
     $subject = 'a file for you'; 

     $fullname = strip_tags($_POST['fullname']); 
     $tel = strip_tags($_POST['tel']); 

     $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name']))); 
     $filename = $_FILES['file']['name']; 

     $boundary =md5(date('r', time())); 

     $headers = "From: [email protected]\r\nReply-To: [email protected]"; 
     $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; 

     $fullname="This is a multi-part fullname in MIME format. 

--_1_$boundary 
Content-Type: multipart/alternative; boundary=\"_2_$boundary\" 

--_2_$boundary 
Content-Type: text/plain; charset=\"iso-8859-1\" 
Content-Transfer-Encoding: 7bit 

$fullname 

--_2_$boundary-- 
--_1_$boundary 
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment 
--_1_$boundary--"; 



     mail($to, $subject, $fullname, $headers); 
    } 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>MailFile</title> 
</head> 

<body> 

<?php echo $output; ?> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
<p><label for="fullname">Full name</label> <input name="fullname" id="fullname" /></p> 
<p><label for="file">File</label> <input type="file" name="file" id="file"></p> 
<p><input type="submit" name="submit" id="submit" value="send"></p> 
</form> 
</body> 
</html> 
+0

'$ tel' ...와 관련하여 아무 것도하지 않는 것 같습니다. 당신은 그것을 정의한 다음 그것을 무시합니다. –

답변

14

휠을 다시 발명 할 필요가 없습니다. phpmailer 클래스를 사용하여 당신은 다시 스크립트가, phpmailer.class

<?php 

    if(isset($_POST['submit'])) 
    { 

     //The form has been submitted, prep a nice thank you fullname 
     $output = '<h1>Thanks for your file and fullname!</h1>'; 

     //Set the form flag to no display (cheap way!) 
     $flags = 'style="display:none;"'; 

     // include and start phpmailer 
     require_once('PHPMailer_5.2.4/class.phpmailer.php'); 
     $mail = new PHPMailer(); 

     //Deal with the email 
     $mail->From = "[email protected]"; // from 
     $mail->AddReplyTo("[email protected]", "Webmaster"); // reply to address/name 

     $mail->AddAddress('[email protected]'); // to address 

     $mail->Subject = 'A file for you'; // subject 

     $mail->Body = $fullname; // body 

     $mail->AddAttachment($_FILES['file']['tmp_name']); // attach uploaded file 
    } 

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>MailFile</title> 
</head> 

<body> 

<?php echo $output; ?> 

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> 
<p><label for="fullname">Full name</label> <input name="fullname" id="fullname" /></p> 
<p><label for="file">File</label> <input type="file" name="file" id="file"></p> 
<p><input type="submit" name="submit" id="submit" value="send"></p> 
</form> 
</body> 
</html> 
+0

아마 맞을 것입니다. 하지만 나를 위해 복잡 ... 나는 그것을 구성하는 방법을 이해할 수 없습니다. 내 예제가 작동, 난 단지 onemore을 추가해야합니다! – user1978483

+0

phpmailer를 사용하여 스크립트를 다시 작성하기 위해 필자의 대답을 업데이트했습니다. 제 의견으로는 훨씬 짧고 단순하지만, 원하는 방향이 아닌지 이해하십시오. – sjdaws

+0

시험 사용해 주셔서 감사합니다! 행운을 빌어 요. – user1978483

-1
<?php 
//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 

?> --PHP - 혼합형
를 사용

다음
function emailWithAttachment($to, $subject, $message, $attachment) { 

    $mail = new PHPMailer(); 

    $mail->AddAddress($to); 

    $mail->From   = "[email protected]"; 
    $mail->FromName  = "Your Name"; 
    $mail->Subject  = $subject; 
    $mail->Body   = $message; 

    $mail->AddAttachment($attachment); 

} 

로 간단하게 첨부 파일을 추가 할 수 있습니다 콘텐츠 유형 : multipart/대체; boundary = "PHP-alt-"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 
관련 문제