简体   繁体   English

有没有办法用 wp_mail 将表单输入(pdf)作为附件发送?

[英]Is there any way to send form input (pdfs) as attachments with wp_mail?

I'm building a site for an HR Agency.我正在为一家人力资源机构建立一个网站。 For every job ad they have on their site, the candidate needs to be able to apply through a form.对于他们网站上的每个招聘广告,候选人都需要能够通过表格进行申请。 The form gets content like name, wage as string and int.该表单获取诸如名称、工资作为字符串和整数等内容。 But it is also important to upload a pdf.但上传pdf也很重要。 The content and pdf is supposed to be send via wp_mail() to the agency.内容和 pdf 应该通过 wp_mail() 发送给该机构。 Everything works perfectly except the PDF.除了PDF,一切都完美无缺。 I have been searching for a solution for about 2 weeks, but I am still stuck.我一直在寻找解决方案大约 2 周,但我仍然被困住了。

Help would be more than awesome to receive :)收到帮助会非常棒:)

To fix the problem I already tried several Wordpress Methods to upload the pdf to my Wordpress uploads and my plan was afterwards to send the uploaded pdf via wp_mail().为了解决这个问题,我已经尝试了几种 Wordpress 方法将 pdf 上传到我的 Wordpress 上传文件,然后我的计划是通过 wp_mail() 发送上传的 pdf。

Functions I already tried:我已经尝试过的功能:

  • wp_upload_bits() wp_upload_bits()
  • wp_handle_upload() wp_handle_upload()
  • move_uploaded_file() move_uploaded_file()

This is the form without all the inputs which are already working:这是没有所有已经在工作的输入的表单:

<form method="POST" action="https://domainname.com/form-sent/">                                                              
 <div class="form-group">                                        
  <label for="files">Bewerbungsunterlagen</label>                                        
  <input type="file" class="form-control-file" name="files" 
  required>                                      
 </div>                                                                          
 <button class="btn btn-primary" type="submit">Jetzt Bewerben! 
 </button>                              
</form>

This is the relevant php of the success message page, where the user is directed after he submits the form:这是成功消息页面的相关php,用户提交表单后被引导到这里:

$to = "emailOfAgency";
$from = $_POST['email'];
$files = $_FILES['files'];
$headers = "From:" . $from;

wp_mail($to, $subject, $message, $headers, $files);

The email is perfectly send with wp_mail(), but there is no attachment send with it.电子邮件与 wp_mail() 完美发送,但没有附件发送。

Perfect result would be: Mail sent with attachments.完美的结果是:发送的邮件带有附件。

For anyone who faces the same problem here is what fixed my code:对于在这里遇到同样问题的人来说,修复了我的代码:

In the form input I changed post to lowercase and added enctype="multipart/form-data".在表单输入中,我将 post 更改为小写并添加了 enctype="multipart/form-data"。 In addition to that I added accept="application/pdf" on the type input field.除此之外,我在类型输入字段中添加了 accept="application/pdf" 。

<form method="post" action="https://domainname.com/form-sent/" 
enctype="multipart/form-data">                                                              
  <div class="form-group">                                        
     <label for="files">Bewerbungsunterlagen</label>                                        
     <input type="file" class="form-control-file" name="files" 
     accept="application/pdf" required>                                      
  </div>                                                                          
  <button class="btn btn-primary" type="submit">Jetzt Bewerben! 
  </button>                              
</form>

On the success message page I added wp_handle_upload() to upload the pdf to the uploads and then send it with wp_mail():在成功消息页面上,我添加了 wp_handle_upload() 将 pdf 上传到上传,然后使用 wp_mail() 发送:

$to = "emailOfAgency";
$from = $_POST['email'];

// upload files
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }

    $uploadedfile = $_FILES['files'];

    $upload_overrides = array( 'test_form' => false );

    $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

    if ( $movefile && ! isset( $movefile['error'] ) ) {
        $doesItWork = "File is valid, and was successfully uploaded.\n";
    } else {
        /**
         * Error generated by _wp_handle_upload()
         * @see _wp_handle_upload() in wp-admin/includes/file.php
         */
        $doesItWork = $movefile['error'];
    }

    // end upload files

$headers = "From:" . $from;

wp_mail($to, $subject, $message, $headers, $files);

Hope this helps someone in the near future!希望这可以在不久的将来帮助某人! :) :)

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

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