PDF upload field in frontend
I m looking for a way to get a PDF upload field in a frontend form. This form is not generated by the formbuilder, because it is part of a module.
- How to get this uploadfield in the form?
- How to mail this to the recipient with the mailer attachment.
Current form fields:
$this->frm->addText('naam')->setAttributes(array('required' => null));
$this->frm->addText('voornaam')->setAttributes(array('required' => null));
$this->frm->addText('straatnr');
$this->frm->addText('postcode');
$this->frm->addText('gemeente');
$this->frm->addText('nationaliteit');
$this->frm->addText('email')->setAttributes(array('required' => null, 'type' => 'email'));
$this->frm->addText('telefoon');
Current working mailer code:
$to = FrontendModel::get('fork.settings')->get('Core', 'mailer_from');
$message = \Common\Mailer\Message::newInstance($subject)
->setFrom(array($email => $fullName))
->setto(array($to['email'] => $to['name']))
->setReplyTo(array($email => $fullName))
->parseHtml(
FRONTEND_MODULES_PATH . '/Jobs/Layout/Templates/Mails/Apply.tpl',
$variables,
true
)
;
$this->get('mailer')->send($message);
This should help:
Adding a pdf field: https://github.com/wijs/fork-cms-module-whitepapers/blob/master/backend/modules/whitepapers/actions/add.php#L42
Validating that the uploaded file is a pdf: https://github.com/wijs/fork-cms-module-whitepapers/blob/master/backend/modules/whitepapers/actions/add.php#L82-L85
Building a path to upload your pdf: https://github.com/wijs/fork-cms-module-whitepapers/blob/master/backend/modules/whitepapers/actions/add.php#L92
Uploading your pdf: https://github.com/wijs/fork-cms-module-whitepapers/blob/master/backend/modules/whitepapers/actions/add.php#L104
Adding an attachment: $message->attach(\Swift_Attachment::fromPath($pathToFile));
That indeed helps me a lot. Thx Wouter.
On a side note: is it possible to sent the file as attachment without it being uploaded to a path on the server?
I guess it should be possible by using $this->frm->getFields('file')->getTempFileName()
instead of the $pathToFile
variable, but I'm not 100% sure, you should test it ;)
Well i ve tested it and the following code seems to work fine. You never know this could help in a future project:
->attach(
\Swift_Attachment::fromPath($_FILES['file']['tmp_name'])->setFilename( $item['voornaam'].' '. $item['naam'].'.'.$fileExt)
)