php邮件附件在wordpress中失败

时间:2013-04-05 07:30:56

标签: wordpress attachment php

我正在使用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文件夹中附加文件。但我无法附加其他位置的文件。有人请帮帮我。 提前谢谢。

1 个答案:

答案 0 :(得分: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>";
 }

希望这会奏效:)