Forums › Forums › OroCRM › OroCRM – Programming Questions › automatic email with attachment
This topic contains 9 replies, has 4 voices, and was last updated by golriz.nourani 6 years, 12 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- February 13, 2016 at 1:08 am #25109
Hi everyone,
I want to send a automatic email and it has attachment
but I don’t find how should I set attachment ( which controller handle it)?
could you help me?
thanks a lot - CreatorTopic
- AuthorReplies
- February 15, 2016 at 2:10 am #25110
Hello.
Attachment can be uploaded via AttachmentManager, then can be transformed to EmailAttachment via EmailAttachmentTransformer, and finally attached to the body of your email.
July 11, 2016 at 9:14 am #25111@Yevhen Shyshkin
The AttachmentManager::prepareRemoteFile() method return an instance of File… How to convert it into an Attachment ?
This not work :PHP1$attachement = $this->emailAttachmentTransformer->entityFromUploadedFile($this->attachmentManager->oroToModel($this->kernelRootDir . '/../web/media/alertes/' . $id . '.png'));Or
PHP1$attachement = $this->emailAttachmentTransformer->entityFromUploadedFile($this->attachmentManager->prepareRemoteFilee($this->kernelRootDir . '/../web/media/monitors/' . $id . '.png'));Their is something that I have misunderstood.
July 11, 2016 at 10:29 am #25112> How to convert it into an Attachment ?
You can create and persist Attachment as a regular entity – it simply shows relation between entity and file. After that you can attach it to email. Check this example:PHP123456789101112131415/** @var \Oro\Bundle\AttachmentBundle\Entity\Attachment $attachment */$attachment = new Attachment();// set attachment params and save entity to DB$transformer = $this->get('oro_email.email_attachment_transformer');/** @var \Oro\Bundle\EmailBundle\Entity\EmailAttachment $emailAttachmentEntity */$emailAttachmentEntity = $transformer->oroToEntity($attachment);/** @var \Oro\Bundle\EmailBundle\Form\Model\EmailAttachment $emailAttachmentModel */$emailAttachmentModel = $transformer->oroToModel($attachment);$emailAttachmentModel->setEmailAttachment($emailAttachmentEntity);/** @var \Oro\Bundle\EmailBundle\Form\Model\Email $emailModel */$emailModel = new Email();// set email params$emailModel->addAttachment($emailAttachmentModel);July 11, 2016 at 12:19 pm #25113@Yevhen Shyshkin
I don’t need to save attachement in DB.
Is it possible to just attach a file to an email and send it without save the attachment in DB.
At the end I will make an email template but for the time it is usefull to just use Email model and set his params.
Thanks and regards.
July 12, 2016 at 1:50 am #25114> Is it possible to just attach a file to an email and send it without save the attachment in DB.
Yes, it is even easier – you just have to build appropriate EmailAttachment object and add it to your email. Use body of EmailAttachmentTransformer::oroToEntity as an example.July 13, 2016 at 2:39 am #25115Sorry but I don’t understand how to proced.
To much thing that i am not aware.What type of object must I pass to addAttachment() method ? An AttachmentEntity or an AttachmentModel.
Have I to deal with both ? It’s seem to be a litlle complicated for just one such thing.
Am I on the right way ?PHP123456789101112131415161718192021$emailAttachmentEntity = new AttachmentEntity();if ($step) {$file = $this->attachmentManager->prepareRemoteFile($this->kernelRootDir . '/../web/media/alertes/' . $id . '.png');} else {$file = $this->attachmentManager->prepareRemoteFile($this->kernelRootDir . '/../web/media/monitors/' . $id . '.png');}$emailAttachmentEntity->setFileName($file->getFilename());$emailAttachmentContent = new EmailAttachmentContent();$emailAttachmentContent->setContent(base64_encode($this->filesystem->get($file->getFilename())->getContent()));$emailAttachmentContent->setContentTransferEncoding('base64');$emailAttachmentContent->setEmailAttachment($emailAttachmentEntity);$emailAttachmentEntity->setContent($emailAttachmentContent);$emailAttachmentEntity->setContentType($file->getMimeType());$emailAttachmentEntity->setFile($file);$emailAttachmentEntity->setFileName($file->getOriginalFilename());Then, have I make the model too ?
July 14, 2016 at 6:54 am #25116> What type of object must I pass to addAttachment() method ? An AttachmentEntity or an AttachmentModel.
This method requires instance of Oro\Bundle\EmailBundle\Form\Model\EmailAttachment.> Have I to deal with both ?
Depends on your case. If you don’t have this file stored in DB then probably you will not need entity, only model.> Am I on the right way ?
Yes, direction is correct. :) I’d propose to check whether you need to prepare you file, because looks like you alredy have it uploaded, so you don’t need to do it again.October 11, 2016 at 5:56 am #25117Hie,
Below my solution in Workflow\Action based on emailAttachmentTransformer service… if it can help you :
PHP123456789101112131415161718192021222324252627282930313233343536protected function getEmailAttachmentEntity(Subscription $subscription){#TODO : check error from export handler// Export xlsx$options = ['filters' => ['codeBroker' => [$subscription->getCodeBroker()->getId()]]];$handler = $this->exportHandler->getExportResult('dawkins_subscription_export_to_xlsx','dawkins_subscription_subscription','export','xlsx',null,$options);// Get fileName$a = explode('/', $handler['url']);$fileName = end($a);$fullFileName = $this->fileSystemOperator->getTemporaryFile($fileName)->getRealPath();$uploadedFile = new UploadedFile($fullFileName, $fileName);$emailAttachment = $this->emailAttachmentTransformer->entityFromUploadedFile($uploadedFile);$emailBody = new EmailBody();$emailAttachment->setEmailBody($emailBody);return $this->emailAttachmentTransformer->entityToModel($emailAttachment);}May 2, 2017 at 4:39 am #25118Dear Sir
After one year again I face with auto email. your explanation and sample code were so usefull, thanks a lot.
when I put email of user of system in bcc list (in backend), dialog of email show my email as below
(کاربر)test@gmail.com
language of system is fa_IR so “کاربر” is translation of “user” but when I want to send email, system say that email is not in right format
what should I do?
Thanks
Golriz - AuthorReplies
The forum ‘OroCRM – Programming Questions’ is closed to new topics and replies.