2013-02-25 97 views
2

我有這個腳本,只適用於全名和附件文件。但是,如果我再添加一個字段,例如「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'做任何事情......: - ?您定義它然後忽略它。 – 2013-02-25 11:21:38

回答

14

有沒有必要重新發明車輪。使用phpmailer類,然後你可以就這麼簡單

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); 

} 

這裏是你的腳本,改寫使用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

也許你是對的。但它對我來說很複雜......我無法理解如何配置它。我的例子是工作,我只需要添加一個提交! – user1978483 2013-02-25 10:28:40

+0

我更新了我的答案,使用phpmailer重寫您的腳本。在我看來,它更短更簡單,但要明白它不是你想要的方向。 – sjdaws 2013-02-25 10:37:27

+0

謝謝嘗試!祝你好運 – user1978483 2013-02-25 10:39:47

-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-混色
添加附件 內容類型:multipart/alternative; border =「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"; 
?>