2017-04-21 99 views
0

我有我的fucntions.php文件中创建和验证表单的shortcode。表单提交并验证后,数据将存储在SESSION变量中。 SESSION变量用于将信息传递到自定义PHP模板页面。此页面使用wp_mail()通过电子邮件发送表单数据并显示一条感谢信。wp_mail() - 从附件发送'文件'输入类型

我的问题是,窗体有一个'文件'输入类型的图片上传。我已经验证了正确,但传输上传的图像,然后通过电子邮件将它作为'$附件“在wp_mail()函数中是我正在努力。

另外我知道我应该将上传的图像保存在temp文件夹,而不是将其存储在SESSION变量中,然后我只需在发送电子邮件后删除图像

我的问题是这样的:所有这些代码都很长,所以这里是简短的甜蜜的版本:

的functions.php(在简码功能)

<?php 
$file = ""; 
$fileErr = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    //file upload is required, make sure its not empty 
    if(empty($_FILES['pmFile'])){ 
     $fileErr = "This is required!"; 
    } 
    else{ 
     $file = $_FILES['pmFile'];//I validate here, but lets just say its OK  
    } 

    //If the error message is empty, store data in SESSION variables, and 
    //uploaded file in temp folder + redirect to thank you page 
    if(empty($fileErr)){ 

     //HOW DO I SAVE THE FILE TEMPORARILY FOR USE ON THE NEXT PAGE??? 

     wp_redirect('../wp-content/themes/rest of the path/thankYouPage.php'); 
     exit(); 

    }else{ /*display errors*/} 

} 

//down here is where I wrote the code for the form. YES method=post, YES I 
//included enctype="multipart/form-data, YES the file input name is in fact 
//'pmFile'. 
?> 

thankYouPage.php

//initial page stuff 
//start email setup 
$to = '[email protected]'; 

$email_from = '[email protected]'; 
$email_subject = "New Price Match Request"; 
$email_body = "I display the html and data here"; 

//for attachments 
//HOW DO I PULL THAT UPLOADED FILE FROM THE TEMP FOLDER AND SEND IT AS AN ATTACHMENT? 
$attachments = (the file in temp folder); 
//NOW HOW TO I DELETE IT FROM THE TEMP FOLDER 

$headers = "From: $email_from \r\n"; 
$headers .= "Reply-To: $email \r\n"; //$email is a SESSION variable pulled from before 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

//set to html 
add_filter('wp_mail_content_type',create_function('', 'return "text/html"; ')); 

//send 
wp_mail($to, $email_subject, $email_body, $headers, $attachments); 

// Reset content-type to avoid conflicts 
remove_filter('wp_mail_content_type', 'wpdocs_set_html_mail_content_type'); 

我知道很多它删除代码,但电子邮件适用于所有的形式等信息通过Session变量被转移。我真的只需要知道如何传输这个上传的文件并通过电子邮件发送。谢谢!

回答

0

如果我理解正确的...

对于上载的临时文件夹中的文件使用move_uploaded_filewp_handle_upload

然后附加文件到邮件$attachments = array(WP_CONTENT_DIR . '/uploads/file.txt');

发送邮件,并与unlink删除。

+0

好的答案,简短而甜美。我实际上正在尝试这个,手指交叉。 –

+0

您的回答有效!现在唯一的问题是,我收到一个错误:“_File为空,请上传更多的内容,这个错误也可能是由于php.ini中的上传被禁用或者post_max_size被定义为小于php.ini._中的upload_max_filesize “我编辑了php.ini文件,确保我的文件夹权限正确,并且有足够的硬盘空间。我无法在任何地方找到解决方案。 –

0

我只是使用PHPMailer,它非常简单,并有一个很好的文档,发送附件也很容易。

+0

是的我已经遇到过多次PHPMailer试图解决这个问题。我试了一下,然后打了另一面墙,所以我尝试了wp_mail()。我原本是用mail()做所有这些。当我如此接近时,我只是不想重做所有这些。 –