Запрос JS Ajax просто для примера
Код: Выделить всё
submitData(form) {
let formData = new FormData(form[0]);
jQuery.ajax({
url: '/wp-json/my-plugin/v1/send_email/',
type: 'POST',
data: formData,
processData: false,
contentType: false,
beforeSend: function () {
// validate fields
},
success: function (response) {
// success actions
},
error: function () {
// build error messages
}
});
return false;
}
Код: Выделить всё
{
"form_fields": {
"name": {
"file": "beatifull-picture.jpeg"
},
"full_path": {
"file": "beatifull-picture.jpeg"
},
"type": {
"file": "image/jpeg"
},
"tmp_name": {
"file": "C:\\Windows\\Temp\\phpD363.tmp"
},
"error": {
"file": 0
},
"size": {
"file": 364006
}
}
}
Код: Выделить всё
protected function send_mail($mail_data, $file){
// ..
$attachment = $this->prepare_attachments($file);
$email = wp_mail(
$mail_data['mail_to'],
$mail_data['mail_subject'],
$mail_data['mail_message'],
$mail_data['headers'],
$attachment,
);
}
// KEY FUNCTION
protected function prepare_attachments($files) {
$attachments = [];
// Iterate through the files array to extract attachments
if (isset($files['form_fields']['tmp_name']['file'])) {
$tmp_name = $files['form_fields']['tmp_name']['file'];
$error = $files['form_fields']['error']['file'];
if ($error === UPLOAD_ERR_OK) {
// Log the path of each file
error_log('Attachment: ' . $tmp_name);
$attachments[] = $tmp_name;
}
}
return $attachments;
}
Заранее спасибо и извините за длинные коды
Подробнее здесь: https://stackoverflow.com/questions/784 ... -extension
Мобильная версия