PHP code example of bredabeds / magento-email-attachments

1. Go to this page and download the library: Download bredabeds/magento-email-attachments library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bredabeds / magento-email-attachments example snippets


->setTemplateVars([
    'attachments' => '/path/to/document.pdf'
])

// 'type' and 'filename' are optional
// If 'type' is not set, mime_content_type is used to guess
// If 'filename' is not set, the filename from the path is used
->setTemplateVars([
    'attachments' => [
        'path' => '/path/to/document.xlsx',
        'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'filename' => 'Document.xlsx'
    ]
])

->setTemplateVars([
    'attachments' => [
        '/path/to/document1.pdf',
        [
            'path' => '/path/to/document2.xlsx',
            'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // optional
            'filename' => 'document2.xlsx' // optional
        ],
        '/path/to/document3.docx'
    ]
])

$absolutePath = '/path/to/document1.pdf';
$transport = $this->transportBuilder
    ->setTemplateIdentifier('simple_text_email')
    ->setTemplateOptions([
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => $this->storeManager->getStore()->getId()
    ])
    ->setTemplateVars([
        'email_subject' => $subject,
        'message' => $body,
        'attachments' => $absolutePath // Can be added here via template variables
    ])
    ->setFromByScope($this->contactsConfig->emailSender())
    ->addTo($to)
    ->addAttachment(file_get_contents($absolutePath), basename($absolutePath), 'application/pdf'); // Or here

$transport->getTransport()->sendMessage();

public function __construct(
    protected \BredaBeds\EmailAttachments\Helper\SendBasicEmail $sendBasicEmail,
    protected \BredaBeds\EmailAttachments\Helper\Browserless $browserless
) { }

protected function sendTestEmail()
{
    $absolutePath = $this->browserless->generatePdfFromUrl(
        'https://google.com',   // URL 
        'google.pdf',           // Filename to use
        'pdf/files'             // Location in the "var" path
    ); // Saves a PDF to: "/path/to/magento/var/pdf/files/google.pdf"

    $this->sendBasicEmail->sendEmail(
        '[email protected]',     // To
        'Your PDF Document',    // Subject
        'Your PDF document is attached. Please contact us if you have any questions or concerns.',  // Body
        [$absolutePath] // Optional: Sets attachments via template variables, see methods 1-3 above.
    ); // Sends an email with the previously saved PDF
}