0

我正在wordpress中制作一个 php 表单。我想在这个表单中附加一个记事本或 PDF文件。例如,我在电子邮件中使用正文变量中的邮件函数获取所有这些

$body =" Attachment: $Attachmentfile";

我收到的不是文件,而是用户上传的文件名而不是文件。我正在尝试在电子邮件中接收文件。实际上,我也在学习如何去做。

我分配给 htm 文件标签的 php 代码是,

    if(trim($_POST['Attachmentfile']) === '') {
$AttachmentfileError = 'Please enter a Pdf, Notepad or Word file.';
$hasError = true;
} 
else 
{
if($_FILES["Attachmentfile"]["name"] != "")  
{  
$strFilesName = $_FILES["Attachmentfile"]["name"];  
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["Attachmentfile"]["tmp_name"])));  
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
$strHeader .= "Content-Transfer-Encoding: base64\n";  
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
$strHeader .= $strContent."\n\n";  
}
else
{
$Attachmentfile = trim($_POST['Attachmentfile']);
}
}

和我用于获取文件的 Html 代码,

<li class="left">
<label for="CV">Attachments :</label><span class="error">*</span><br>
<input class="txt" type="file" name="Attachmentfile" id="Attachmentfile" value="<?php if(isset($_POST['Attachmentfile']))   echo $_POST['Attachmentfile'];?>">
</li>

这是我的邮件功能代码,

$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
$headers .= "\r\n" . 'Content-type: text/html';
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;

你能帮忙修改我的代码以便在电子邮件中获取文件吗?谢谢朋友。

4

1 回答 1

0
function property_add() {
    $data = $_POST['data'];
    $data = array_map('trim',$data);
    extract($data);
    $to  = "reciever mail here ";
    $subject = "subject here";

    $message ='Message here';


    $attachments = array();  // initialize attachment array 
    $upload_dir = wp_upload_dir();  // look for this function in wordpress documentation at codex 
    $upload_dir = $upload_dir['path'];

    foreach ($_FILES["images"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {

            $tmp_name = $_FILES["images"]["tmp_name"][$key]; // Get temp name of uploaded file
            $name = time().'_'.$_FILES["images"]["name"][$key];  // rename it to whatever
            move_uploaded_file($tmp_name, "$upload_dir/$name"); // move file to new location 
            $attachments[] =  "$upload_dir/$name";  //  push the new uploaded file in attachment array
        }
    }

    add_filter( 'wp_mail_content_type', 'set_html_content_type' ); // allow html tags in mail
    if(wp_mail($to, $subject, $message,'',$attachments)) {
        echo "any success message>";
    } else {
        echo "failure message ";
    }
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );  // remove filter to avoid conflicts 

    if(!empty($attachments)) {
        foreach($attachments as $attachment) {
            @unlink($attachment); // delete files after sending them
        }
    }


} 

这是我的一个项目的摘录.. 根据您的需要更改它我写了评论,以便您变得更好更好..

在表单提交上调用此函数

于 2013-10-02T11:14:21.733 回答