我正在为人力资源代理商建立一个站点。对于他们网站上的每个招聘广告,候选人都需要能够通过表格进行申请。 表格获取内容,例如姓名,工资,字符串和整数。但上传pdf也很重要。 内容和pdf应该通过wp_mail()发送给代理商。 除PDF之外,其他所有功能均正常运行。 我一直在寻找解决方案大约2个星期,但仍然遇到问题。
获得帮助将不胜枚举:)
为解决此问题,我已经尝试了几种Wordpress方法将pdf上传到我的Wordpress上传文件中,而我的计划是随后通过wp_mail()发送上传的pdf。
我已经尝试过的功能:
这是没有所有输入的表格:
<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>
这是成功消息页面的相关php,在用户提交表单后,用户将被定向到该页面:
$to = "emailOfAgency";
$from = $_POST['email'];
$files = $_FILES['files'];
$headers = "From:" . $from;
wp_mail($to, $subject, $message, $headers, $files);
使用wp_mail()可以完美发送电子邮件,但是没有附件发送。
完美的结果将是: 带有附件发送的邮件。
答案 0 :(得分:0)
对于遇到相同问题的任何人,这是修复我的代码的原因:
在表单输入中,我将帖子更改为小写并添加了enctype =“ multipart / form-data”。除此之外,我还在类型输入字段上添加了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>
在成功消息页面上,我添加了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);
希望这会在不久的将来对某人有所帮助! :)