目的是允許用戶將自己的新文章(文本和檔案)材料發送到管理員的電子郵件。
有一個表格:
<form class="form form-send" enctype="multipart/form-data">
<div class="send__inputs-block">
<div class="sliding-label__block send__textarea-block">
<textarea name="comment" id="comment" class="input textarea send__textarea" placeholder=" "></textarea>
<label for="comment" class="text text--fz-18 sliding-label">
<?php echo get_post_meta( get_the_ID(), 'joint_send_comment_text', true ); ?>
</label>
</div>
<div class="sliding-label__block">
<input id="name" type="text" name="name" class="input input-text" placeholder=" ">
<label for="name" class="text text--fz-18 sliding-label">
<?php echo get_post_meta( get_the_ID(), 'joint_send_name_text', true ); ?>
</label>
</div>
</div>
<div class="send__file-block">
<input id="file" type="file" name="file" class="input-file" multiple>
<label for="file" class="label label-file">
<i class="joint-upload icon"></i>
<span class="text text--fz-14 upload__text">
<?php echo get_post_meta( get_the_ID(), 'joint_send_file_text', true ); ?>
</span>
</label>
</div>
<button type="submit" class="button form-button">
<span class="button-text">
<?php echo get_post_meta( get_the_ID(), 'joint_send_submit_text', true ); ?>
</span>
</button>
</form>
這是JS代碼:
function formSend(e, form) {
e.preventDefault()
if (formValidate(form)) return
let backFile = jointAjax.ajaxurl
let curPage = ''
let formData = new FormData(form)
if (form.classList.contains('form-contacts')) {
curPage = 'contacts'
}
else if (form.classList.contains('form-send')){
curPage = 'send'
let uploadFiles = []
for (let single of form.file.files) {
uploadFiles.push(single)
}
console.log(uploadFiles[0])
formData.append('file', uploadFiles)
}
else {
return
}
formData.append('action', curPage)
fetch(backFile, {
method: 'POST',
body: formData,
})
.then(form.reset())
.catch(error => {
error.json().then(response => {
alert(response.message)
})
})
}
這是PHP代碼:
add_action('wp_ajax_send', 'joint_send_send_form');
add_action('wp_ajax_nopriv_send', 'joint_send_send_form');
function joint_send_send_form() {
global $joint_settings;
$data = json_encode($_POST);
$data = json_decode($data, true);
$attachment = array();
if (isset($_FILES['file'])) {
foreach($_FILES['file'] as $key => $file) {
$attachment[] = $file['tmp_name'] . $file['name'];
}
}
$mailBody = '';
foreach ($data as $key => $value) {
if ($key === 'action' || $key === 'file') continue;
if (!empty($data[$key]))
$mailBody .= '<p><strong>' . ucfirst($key) . ':</strong> ' . esc_html($value) . '</p>';
}
$headers = array(
'From: Joint Admin <' . SMTP_FROM . '>',
'content-type: text/html'
);
wp_mail(
$joint_settings['send_mail_to'],
$joint_settings['send_mail_theme'],
$mailBody,
$headers,
WP_CONTENT_DIR . '\\' . $_FILES['file']['name']
);
// wp_send_json_success($_FILES['file']['tmp_name'] . '\\' . $_FILES['file']['name']);
}
我已經查看了各種論壇、文章和視頻,但我似乎無法完成任務。
在 wp_mail 中,我們必須將完整路徑傳遞給檔案,但是我們從哪里獲得該路徑?而且無論我如何嘗試處理多個檔案,函式都只回傳最后一個檔案的名稱作為回復。
SMTP 設定正確。電子郵件進來了,但沒有檔案。
請幫忙 - 我不知道該怎么辦了。
uj5u.com熱心網友回復:
我想通了 - 我查看了 Contact form 7 插件中的實作并理解了這些原則。
基本思路是先將上傳的檔案移動到站點的內部目錄,然后從那里發送到郵箱。
在JS 中,我只更改了一件事(您可以將檔案發送到請求而無需處理):
else if (form.classList.contains('form-send')){
formData.append('file', form.file.files)
curPage = 'send'
}
我的處理程式函式如下所示:
add_action('wp_ajax_send', 'joint_send_send_form');
add_action('wp_ajax_nopriv_send', 'joint_send_send_form');
function joint_send_send_form() {
global $joint_settings;
$data = json_encode($_POST);
$data = json_decode($data, true);
$mailBody = '';
foreach ($data as $key => $value) {
if ($key === 'action' || $key === 'file') continue;
if (!empty($data[$key]))
$mailBody .= '<p><strong>' . ucfirst($key) . ':</strong> ' . esc_html($value) . '</p>';
}
$headers = array(
'From: Joint Admin <' . SMTP_FROM . '>',
'content-type: text/html'
);
$file = $_FILES['file'];
// joint_array_flatten() - converts multi-dimensional array to a flat array
$names = joint_array_flatten( $file['name'] );
$tmp_names = joint_array_flatten( $file['tmp_name'] );
$uploads = wp_get_upload_dir()['basedir'];
$uploads_dir = path_join( $uploads, 'joint_uploads' );
$uploaded_files = array();
foreach ( $names as $key => $filename ) {
$tmp_name = $tmp_names[$key];
if ( empty( $tmp_name ) or ! is_uploaded_file( $tmp_name ) ) {
continue;
}
// joint_antiscript_file_name() - converts a file name to one that is not executable as a script
$filename = joint_antiscript_file_name( $filename );
$filename = wp_unique_filename( $uploads_dir, $filename );
$new_file = path_join( $uploads_dir, $filename );
if ( false === @move_uploaded_file( $tmp_name, $new_file ) ) {
wp_send_json_error( json_encode( array('message' => 'Upload error') ) );
return;
}
// Make sure the uploaded file is only readable for the owner process
chmod( $new_file, 0400 );
$uploaded_files[] = $new_file;
}
wp_mail(
$joint_settings['send_mail_to'],
$joint_settings['send_mail_theme'],
$mailBody,
$headers,
$uploaded_files
);
// Deletes sent files as they are on the email
foreach ( $uploaded_files as $filepath ) {
wp_delete_file( $filepath );
}
}
我從 Contact form 7 中竊取了一些功能。反腳本絕對有用,但是否需要joint_array_flatten - 我不知道,但我已經累得不能再想了。
最后我從目錄中洗掉下載的檔案,這樣它就不會被阻塞。
此外,正如用戶@TangentiallyPerpendicular 正確指出的那樣,您需要在檔案輸入名稱中添加 []才能發送多個檔案:
<input id="file" type="file" name="file[]" class="input-file" multiple>
否則,就是這樣。我希望有一天這會幫助其他人,他們不必像我一樣為類似的任務而苦惱)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409314.html
標籤:
