A attachment file with a php form in wordpress

2019-09-05 09:15发布

问题:

I am making a php form in wordpress.I would like to attach a notepad or PDF file along this form.I am getting all this in email using mail function in a body variable for example

$body =" Attachment: $Attachmentfile";

Instead of file i am receiving file name that user uploading instead of file.I am trying to receive file in email.Actually i am learning too that how to do it.

My php code for assigning to htm file tag is,

    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']);
}
}

and my Html code for getting file ,

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

and this is my mail function code,

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

Can you help and modify my code in order to get file in email.Thanks friends.

回答1:

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
        }
    }


} 

this is an extract of one of my project .. change it according to your needs i wrote comment so that you get better better ..

call this function on form submit



标签: php wordpress