Problem with email attachment Woocommerce PDF Invoice plugin

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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật