1

我正在使用 wordpress 创建我的网站。有人可以帮我弄清楚为什么 php 邮件附件无法从我的 wordpress 文件夹以外的其他位置附加文件吗???这是我的代码:

$subject="Enquiry";
$to = "veena@phenomtec.com";
$from = $_POST['email'];
$url=$_POST['resume']; 
$attachment = chunk_split(base64_encode(file_get_contents($url)));
$filename = basename($url);
$parts=explode("?",$filename);
$filename = $parts[0]; 

    $boundary =md5(date('r', time())); 
    $headers = "From: $from\r\nReply-To: $from";
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
    $message="This is a multi-part message in MIME format.
    --_1_$boundary
     Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

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

$message

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

$attachment
--_1_$boundary--";


if ( mail($to, $subject, $message, $headers) ){

    echo "<script>alert('Your email message successfully sent.')</script>";
}
else{

 echo "<script>alert('Sorry, message delivery failed. Contact webmaster for more info.')</script>";
 }

我可以从我的 wordpress 文件夹中附加文件。但我无法从其他位置附加文件。有人请帮助我。提前致谢。

4

1 回答 1

0

首先确保您的表单中有 enctype="multipart/form-data" :

<form method="post" enctype="multipart/form-data">

然后试试这段代码:

$subject="Enquiry";
$to = "veena@phenomtec.com";
$from = $_POST['email'];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['resume']['tmp_name'])));
    $filename = $_FILES['resume']['name'];

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

    $headers = "From: $from\r\nReply-To: $from";
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

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

$message

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

$attachment
--_1_$boundary--";


if ( mail($to, $subject, $message, $headers) ){

    echo "<script>alert('Your email message successfully sent.')</script>";
}
else{

 echo "<script>alert('Sorry, message delivery failed. Contact webmaster for more info.')</script>";
 }

希望这会奏效:)

于 2013-04-05T09:58:43.763 回答