简体   繁体   English

wp_mail()-从表单发送“文件”输入类型作为附件

[英]wp_mail() - Sending 'file' input type from form as attachment

I have shortcode that creates and validates a form in my fucntions.php file. 我在fucntions.php文件中有创建和验证表单的简码。 After the form is submitted and validated, the data is then stored in SESSION variables. 提交并验证表单后,数据将存储在SESSION变量中。 The SESSION variables are used to carry the information to a custom PHP template page. SESSION变量用于将信息携带到自定义PHP模板页面。 This page emails the form data using wp_mail() and displays a thank you note. 此页面使用wp_mail()通过电子邮件发送表单数据,并显示感谢信。

My issue is that the form has a 'file' input type for an image upload. 我的问题是表单具有用于图像上传的“文件”输入类型。 I have it validated correctly, but transferring the uploaded image and then emailing it as an '$attachments" in the wp_mail() function is something I am struggling with. 我已经对其进行了正确的验证,但是我正在努力处理上载的图像,然后将其作为wp_mail()函数中的“ $ attachments”通过电子邮件发送给我。

Also I know I should save the uploaded image in a temp folder instead of storing it in a SESSION variable. 我也知道我应该将上传的图像保存在临时文件夹中,而不是将其存储在SESSION变量中。 And then I will just have to delete the image after the email is sent. 发送电子邮件后,我只需要删除图像即可。

My question is HOW? 我的问题是如何? The code for all of this is very long so here are the short and sweet versions: 所有这些的代码很长,因此这里是简短的版本:

functions.php (in a shortcode function) 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 ThankYouPage.php

//initial page stuff 
//start email setup
$to = 'XXXXX@gmail.com';

$email_from = 'XXXXX@gmail.com';
$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' );

I know a lot of the code it removed, but email works for all of the other information in the form being transferred by SESSION variables. 我知道它删除了很多代码,但是电子邮件可以通过SESSION变量传输形式的所有其他信息。 I really just need to know how to transfer this uploaded file and email it. 我真的只需要知道如何传输此上传文件并通过电子邮件发送即可。 Thanks! 谢谢!

If I understood correctly... 如果我正确理解...

For uploading file from temp folder use move_uploaded_file or wp_handle_upload . 要从临时文件夹上传文件,请使用move_uploaded_filewp_handle_upload

Then attach file to the mail $attachments = array( WP_CONTENT_DIR . '/uploads/file.txt' ); 然后将文件附加到邮件$attachments = array( WP_CONTENT_DIR . '/uploads/file.txt' );

Send mail and delete with unlink . 发送邮件并通过取消链接删除。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM