Attachment into the mail
"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 Senior Software Engineer & Developer Enablement Advocate Sprykee Posts: 1,056 ⚖️ - Guardians (admin)
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 likevendor/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
1
Answers
-
Could you please share your code?
0 -
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); }0 -
@Hidran Arias have you checked ?
0 -
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?0 -
Due to missing methods for setting MIME type and contents, I am unable to attach a PDF. By default, emails are sent without attachments.
0 -
@fsmeier do you have any idea about this?
0 -
fsmeier Senior Software Engineer & Developer Enablement Advocate Sprykee Posts: 1,056 ⚖️ - Guardians (admin)
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 likevendor/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
1 -
Hey @fsmeier,Thanks a bunch for your help with Spryker email attachments! Your POC worked like a charm. If I run into more queries, I'll be sure to reach out. Appreciate it!
Cheers,
Bhupendra5
Categories
- All Categories
- 42 Getting Started & Guidelines
- 7 Getting Started in the Community
- 8 Additional Resources
- 7 Community Ideas and Feedback
- 69 Spryker News
- 896 Developer Corner
- 758 Spryker Development
- 83 Spryker Dev Environment
- 361 Spryker Releases
- 3 Oryx frontend framework
- 34 Propel ORM
- 68 Community Projects
- 3 Community Ideation Board
- 30 Hackathon
- 3 PHP Bridge
- 6 Gacela Project
- 23 Job Opportunities
- 3.2K 📜 Slack Archives
- 116 Academy
- 5 Business Users
- 370 Docker
- 551 Slack General
- 2K Help
- 75 Knowledge Sharing
- 6 Random Stuff
- 4 Code Testing
- 32 Product & Business Questions
- 68 Spryker Safari Questions
- 50 Random