I’m encountering an issue with the WooCommerce PDF Invoice plugin. I’ve configured it to generate an invoice upon order completion. On the thank you page, I want customers to see two buttons: one to download the invoice and another to email it to them. While the download link works perfectly, the emailed attachment lacks an extension. Attempting to open it in Adobe Acrobat results in an error message stating it’s either unsupported or damaged. Even after manually adding the “.pdf” extension, the file remains unreadable.
Details:
- I’m using the WooCommerce PDF Invoice plugin for generating invoices.
- The download link for the generated PDF works as expected.
- When emailing the PDF as an attachment, it lacks a file extension.
- Adding the “.pdf” extension manually doesn’t resolve the issue.
- Attempting to open the attachment results in an
error.
Attempts Made:
- Manually adding the “.pdf” extension to the emailed attachment.
- Testing with a different PDF from the media library, which also lacks
an extension when sent as an attachment. (which works after adding extension)
Code Snippet (for Emailing the Attachment)
echo '<button id="sendEmailBtn"> Send to email</button>';
<script>
document.getElementById("sendEmailBtn").addEventListener("click", function() {
var filePath = '<?php echo $documentUrl; ?>';
var customerEmail = '<?php echo $customer_email; ?>';
// Send AJAX request to WordPress endpoint
var xhr = new XMLHttpRequest();
xhr.open("POST", "<?php echo admin_url('admin-ajax.php'); ?>");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// Handle response here if needed
console.log(xhr.responseText);
}
};
xhr.send("action=send_pdf_email&file_path=" + encodeURIComponent(filePath) + "&customer_email=" + encodeURIComponent(customerEmail));
});
</script>
Php function
// AJAX handler function to send email
add_action('wp_ajax_send_pdf_email', 'send_pdf_email');
add_action('wp_ajax_nopriv_send_pdf_email', 'send_pdf_email'); // Allow non-logged-in users to access this endpoint
function send_pdf_email() {
$file_path = isset($_POST['file_path']) ? $_POST['file_path'] : '';
$customer_email = isset($_POST['customer_email']) ? $_POST['customer_email'] : '';
// Retrieve PDF content from the URL
$pdf_content = file_get_contents($file_path);
if ($pdf_content === false) {
echo 'Failed to retrieve PDF content.';
wp_die();
}
// Create a temporary file to store the PDF content
$temp_pdf_path = tempnam(sys_get_temp_dir(), 'invoice_pdf_');
if ($temp_pdf_path === false) {
echo 'Failed to create temporary file.';
wp_die();
}
// Write PDF content to the temporary file
if (file_put_contents($temp_pdf_path, $pdf_content) === false) {
echo 'Failed to write PDF content to file.';
unlink($temp_pdf_path); // Clean up
wp_die();
}
// Send email with PDF attachment
$subject = 'Your Payment Receipt';
$message = 'Thank you for your payment!';
$headers = array('Content-Type: text/html; charset=UTF-8');
$attachments = array($temp_pdf_path);
$mail_sent = wp_mail($customer_email, $subject, $message, $headers, $attachments);
// Delete the temporary PDF file after sending the email
unlink($temp_pdf_path);
if ($mail_sent) {
echo 'Email sent successfully!';
} else {
echo 'Failed to send email.';
}
}
Expected Behavior
- The emailed attachment should have the “.pdf” extension and be
readable by PDF viewers.
I appreciate any insights or solutions you can provide to resolve this issue. Thank you!