Отправка письма с вложенным файлом jspdf
Добрый вечер. Помогите решить проблему. Я создал тесты, после прохождения которых генерируется файл pdf (с помощью jsPDF) с результатом ответов. Внимание вопрос: как сделать так что бы этот файл отправлялся пользователю на почтовый ящик после того как он введёт свой email. (можно ли это сделать через Contact Form 7)
Но так отправляет только заголовок , а сам файл не отсылается
Стандартная функция отправки сообщений в вордпресс может отправлять файлы:
// файл $attachments = array(WP_CONTENT_DIR . '/uploads/file.zip'); // отправка wp_mail( $to, $subject, $message, $headers = '', $attachments = array() );
Вы используете ее для отправки?
Я реализовал так:
Генерирую pdf файл:
var doc = new jsPDF();
doc.fromHTML($('.testdiv').get(0), 10, 20, {
'width': 185
});
var pdf = btoa(doc.output());
$.post("email.php", { data: pdf }, function (response,status) {
console.log(response);
});
php скрипт для отправки:
<?php
function MailWithAttachment($to, $subject, $message, $senderMail, $senderName, $files){
$from = $senderName." <".$senderMail.">";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
if(count($files) > 0){
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $senderMail;
//send email
$mail = @mail($to, $subject, $message, $headers, $returnpath);
//function return true, if email sent, otherwise return fasle
if($mail){ return TRUE; } else { return FALSE; }
}
if(!empty($_POST['data'])){
//email variables
$to = 'мой email';
$from = 'from-email@email.com';
$from_name = 'PDF FIle';
//attachment files path array
$file = base64_decode($_POST['data']);
$subject = 'PHP Email with attachment';
$html_content = '<h1>PHP Email with attachment</h1>';
//call MailWithAttachment() function and pass the required arguments
$send_email = MailWithAttachment($to,$subject,$html_content,$from,$from_name,$file);
//print message after email sent
echo $send_email?"<h1> Mail Sent</h1>":"<h1> Mail not SEND</h1>";
} else {
echo "No Data Found"; }
Так вот на почту приходит только:
$html_content = '<h1>PHP Email with attachment</h1>';
А самого файла нет. Что я делаю не так?

можете после строки:
//attachment files path array $file = base64_decode($_POST['data']);
добавить:
var_dump($file);
что бы посмотреть что у вас содержится в переменной. Возможно у вас не правильный путь к файлу. Проверьте доступен файл по ссылке которая выведется после отправки или нет.