简体   繁体   English

如何在TYPO3中上传PDF文件并通过电子邮件发送?

[英]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) PDF(文件附件)

This PDF is to be uploaded from the system and send these details together with the uploaded PDF via email.此 PDF 将从系统上传,并通过电子邮件将这些详细信息与上传的 PDF 一起发送。 I want these details to be in the custom email template using standalone view and send.我希望这些详细信息使用独立视图并发送在自定义电子邮件模板中。

I'm using TYPO3 v7.6.23我正在使用 TYPO3 v7.6.23

I'm new to TYPO3.我是 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('sample@gmail.com']);
$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]无法打开文件进行阅读 [/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?两者都不起作用,我哪里出错了?

What worked for me was adding fileadmin in front of the identifier:对我fileadmin的是在标识符前添加fileadmin

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

使用 ext:powermail,它功能丰富,维护积极,文档齐全

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM