Calling Developers!
We are reenergizing our code contribution process! Learn More

Attachment into the mail

Options
bhupendra.doniwal
bhupendra.doniwal Posts: 41 🧑🏻‍🚀 - Cadet

"Hello Team,

I have generated a PDF file and stored it in the directory "data/mypdf/test.pdf". Now, I want to attach this file to an email that I'm sending out. However, I'm having trouble using the MailAttachmentTransfer methods. I'm not able to set the type of attachment or the content. Can anyone help me achieve my goal?

Thank you."

Best Answer

  • fsmeier
    fsmeier Senior Software Engineer & Developer Enablement Advocate Sprykee Posts: 1,016 ⚖️ - Guardians (admin)
    edited March 4 Answer ✓
    Options

    Heyhey @bhupendra.doniwal ,

    ootb Spryker is not supporting email-attachments. ☝️

    I was just able to make a POC anyway 😎

    First step is to create a file like vendor/spryker/newsletter/src/Spryker/Zed/Newsletter/Communication/Plugin/Mail/NewsletterSubscribedMailTypeBuilderPlugin.php and add a code-block like the following (dont forget to register your plugin in the dependency-provider):

    ->addAttachment(
                    (new MailAttachmentTransfer())
                        ->setAttachmentUrl(APPLICATION_ROOT_DIR . '/data/mypdf/test.pdf')
                        ->setFileName(APPLICATION_ROOT_DIR . '/data/mypdf/test.pdf')
                        ->setDisplayName('myTest.pdf'),
                )
    

    (I still try to figure out the difference between attachment-url and file-name)

    It would be the cleanest to create the following file on project level and add another MailAttachmentTransfer-property for the type → vendor/spryker/mail/src/Spryker/Shared/Mail/Transfer/mail.transfer.xml

    Then you need to extend vendor/spryker/symfony-mailer/src/Spryker/Zed/SymfonyMailer/Dependency/External/SymfonyMailerToSymfonyMailerAdapter.php on project level (and make it available through the correct factory) and override the following code-block:

    protected function addAttachments(MailTransfer $mailTransfer): void
        {
            foreach ($mailTransfer->getAttachments() as $attachment) {
                $this->email->attachFromPath($attachment->getAttachmentUrlOrFail(), $attachment->getDisplayName(), 'application/pdf');
            }
        }
    

    (in this code-block you should use the type of the newly added MailAttachmentTransfer-property)

    I was not able to download the attachment in MailHog due to an old MailHog-bug/missing functionality of that tool but based on the mime-type and size of the attachment (MIME-tab in mailhog) it was successful 💪

    I am very curious to hear back from you :) If my answer helped I am also happy if you could mark it as answering the question 😁

    All the best,

    Florian

Answers

  • Hidran Arias
    Hidran Arias Senior Technical Trainer Sprykee Posts: 71 🏛 - Council (mod)
    Options

    Could you please share your code?

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 41 🧑🏻‍🚀 - Cadet
    edited March 1
    Options

       protected function handleMail(OrderInvoiceTransfer $orderInvoiceTransfer, OrderTransfer $orderTransfer): void    { 
          $mailAttachment = new MailAttachmentTransfer();
            $mailAttachment           
    ->setFileName("invoiceTest.pdf")           
    ->setDisplayName("invoiceTest.pdf")           
    ->setAttachmentUrl("/data/invoice/invoiceTest.pdf");

            $mailTransfer = (new MailTransfer())           
    ->setType(SalesInvoiceConfig::ORDER_INVOICE_MAIL_TYPE)         
      ->setOrderInvoice($orderInvoiceTransfer)         
      ->setOrder($orderTransfer)           
    ->setLocale($orderTransfer->getLocale())           
    ->setStoreName($orderTransfer->getStoreOrFail())         
      ->addAttachment($mailAttachment);       
    $this->mailFacade->handleMail($mailTransfer);    }

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 41 🧑🏻‍🚀 - Cadet
    Options

    @Hidran Arias have you checked ?

  • Hidran Arias
    Hidran Arias Senior Technical Trainer Sprykee Posts: 71 🏛 - Council (mod)
    Options

    Hi,
    The code is ok. I also took a look and some tests in Spryker.
    Is the email being sent without attachments or it isn´t sent at all?

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 41 🧑🏻‍🚀 - Cadet
    Options

    Due to missing methods for setting MIME type and contents, I am unable to attach a PDF. By default, emails are sent without attachments.

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 41 🧑🏻‍🚀 - Cadet
    Options

    @fsmeier do you have any idea about this?

  • fsmeier
    fsmeier Senior Software Engineer & Developer Enablement Advocate Sprykee Posts: 1,016 ⚖️ - Guardians (admin)
    edited March 4 Answer ✓
    Options

    Heyhey @bhupendra.doniwal ,

    ootb Spryker is not supporting email-attachments. ☝️

    I was just able to make a POC anyway 😎

    First step is to create a file like vendor/spryker/newsletter/src/Spryker/Zed/Newsletter/Communication/Plugin/Mail/NewsletterSubscribedMailTypeBuilderPlugin.php and add a code-block like the following (dont forget to register your plugin in the dependency-provider):

    ->addAttachment(
                    (new MailAttachmentTransfer())
                        ->setAttachmentUrl(APPLICATION_ROOT_DIR . '/data/mypdf/test.pdf')
                        ->setFileName(APPLICATION_ROOT_DIR . '/data/mypdf/test.pdf')
                        ->setDisplayName('myTest.pdf'),
                )
    

    (I still try to figure out the difference between attachment-url and file-name)

    It would be the cleanest to create the following file on project level and add another MailAttachmentTransfer-property for the type → vendor/spryker/mail/src/Spryker/Shared/Mail/Transfer/mail.transfer.xml

    Then you need to extend vendor/spryker/symfony-mailer/src/Spryker/Zed/SymfonyMailer/Dependency/External/SymfonyMailerToSymfonyMailerAdapter.php on project level (and make it available through the correct factory) and override the following code-block:

    protected function addAttachments(MailTransfer $mailTransfer): void
        {
            foreach ($mailTransfer->getAttachments() as $attachment) {
                $this->email->attachFromPath($attachment->getAttachmentUrlOrFail(), $attachment->getDisplayName(), 'application/pdf');
            }
        }
    

    (in this code-block you should use the type of the newly added MailAttachmentTransfer-property)

    I was not able to download the attachment in MailHog due to an old MailHog-bug/missing functionality of that tool but based on the mime-type and size of the attachment (MIME-tab in mailhog) it was successful 💪

    I am very curious to hear back from you :) If my answer helped I am also happy if you could mark it as answering the question 😁

    All the best,

    Florian