Search code examples
typo3typo3-7.6.xtypo3-extensions

How to upload a PDF file and send via email in TYPO3?


I'm Having a form with the following fields.

  • Name
  • Address
  • Contact
  • PDF (File Attachment)

This PDF is to be uploaded from the system and send these details together with the uploaded PDF via email. I want these details to be in the custom email template using standalone view and send.

I'm using TYPO3 v7.6.23

I'm new to TYPO3. How to do This?

I'm adding my code here

$addJobsInfo = GeneralUtility::makeInstance('MNU\\MnuJobs\\Domain\\Model\\Jobs'); 
$addJobsInfo->setName($arguments['name']);
$addJobsInfo->setAddress($arguments['address']);
$addJobsInfo->setContact($arguments['contact']);             
$this->jobsRepository->add($addJobsInfo);
$persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
$persistenceManager->persistAll();
$lastID = $addJobsInfo->getUid();            

$getPDF = $_FILES;                        
if(!empty($getPDF)){
    $pdfName = $getPDF['tx_mnujobs_mnujobs']['name']['jobsDOC'];  
    $pdfTemp = $getPDF['tx_mnujobs_mnujobs']['tmp_name']['jobsDOC'];   
    $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
    $storage = $resourceFactory->getDefaultStorage();
    $folder = $storage->getFolder('user_upload');
    $pdfFinalName = 'Jobs_'.time().'_'.$pdfName;
    $uploadPDF = $storage->addFile($pdfTemp, $folder, $pdfFinalName);            

    if($lastID){
        $newId = 'NEW'.$lastID;
        $data = array();
        $data['sys_file_reference'][$newId] = array(
            'table_local' => 'sys_file',
            'uid_local' => $uploadPDF->getUid(),
            'tablenames' => 'tx_mnujobs_domain_model_jobs',
            'uid_foreign' => $lastID,
            'fieldname' => 'pdfattachment',
            'pid' => $this->settings['pageUid']
        );
        $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
        $dataHandler->start($data, array());
        $dataHandler->process_datamap();                
    }
}    

//E-mail Sending
$mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
$mail->setSubject('Contact Details');
$mail->setTo('[email protected]']);
$mail->setBody('Hi, This is a sample. Please Find the Attachment', 'text/html');
$attachment = Swift_Attachment::fromPath($folder->getIdentifier().$pdfFinalName);
$attachment->setFilename('Document.pdf');
$mail->attach($attachment);
$sendMail = $mail->send();

I'm getting this Exception

Unable to open file for reading [/user_upload/Jobs_1525757243_sample.pdf]

So I've replaced this

$attachment = Swift_Attachment::newInstance($folder->getIdentifier().$pdfFinalName, 'Document.pdf', 'application/pdf');
$mail->attach($attachment);

Both are not working, Where I've gone wrong?


Solution

  • What worked for me was adding fileadmin in front of the identifier:

    $filePath = 'fileadmin' . $file->getOriginalResource()->getIdentifier();
    $attachment = \Swift_Attachment::fromPath($filePath);
    $mail->attach($attachment);