コード例 #1
0
ファイル: SendMail.php プロジェクト: advOpk/pwm
 /**
  * This is the main-function for sending Mails
  *
  * @param array $email Array with all needed mail information
  * 		$email['receiverName'] = 'Name';
  * 		$email['receiverEmail'] = '*****@*****.**';
  * 		$email['senderName'] = 'Name';
  * 		$email['senderEmail'] = '*****@*****.**';
  * 		$email['subject'] = 'Subject line';
  * 		$email['template'] = 'PathToTemplate/';
  * 		$email['rteBody'] = 'This is the <b>content</b> of the RTE';
  * 		$email['format'] = 'both'; // or plain or html
  * @param \In2code\Powermail\Domain\Model\Mail $mail
  * @param array $settings TypoScript Settings
  * @param string $type Email to "sender" or "receiver"
  * @return bool Mail successfully sent
  */
 public function sendTemplateEmail(array $email, \In2code\Powermail\Domain\Model\Mail &$mail, $settings, $type = 'receiver')
 {
     $cObj = $this->configurationManager->getContentObject();
     $typoScriptService = $this->objectManager->get('\\TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
     $conf = $typoScriptService->convertPlainArrayToTypoScriptArray($settings);
     // parsing variables with fluid engine to allow viewhelpers in flexform
     $parse = array('receiverName', 'receiverEmail', 'senderName', 'senderEmail', 'subject');
     foreach ($parse as $value) {
         $email[$value] = $this->div->fluidParseString($email[$value], $this->div->getVariablesWithMarkers($mail));
     }
     // Debug Output
     if ($settings['debug']['mail']) {
         GeneralUtility::devLog('Mail properties', 'powermail', 0, $email);
     }
     // stop mail process if receiver or sender email is not valid
     if (!GeneralUtility::validEmail($email['receiverEmail']) || !GeneralUtility::validEmail($email['senderEmail'])) {
         return FALSE;
     }
     // stop mail process if subject is empty
     if (empty($email['subject'])) {
         // don't want an error flashmessage
         return TRUE;
     }
     $message = GeneralUtility::makeInstance('\\TYPO3\\CMS\\Core\\Mail\\MailMessage');
     $this->div->overwriteValueFromTypoScript($email['subject'], $conf[$type . '.']['overwrite.'], 'subject');
     $message->setTo(array($email['receiverEmail'] => $email['receiverName']))->setFrom(array($email['senderEmail'] => $email['senderName']))->setSubject($email['subject'])->setCharset($GLOBALS['TSFE']->metaCharset);
     // add cc receivers
     if ($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['cc'], $conf[$type . '.']['overwrite.']['cc.'])) {
         $ccArray = GeneralUtility::trimExplode(',', $cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['cc'], $conf[$type . '.']['overwrite.']['cc.']), TRUE);
         $message->setCc($ccArray);
     }
     // add bcc receivers
     if ($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['bcc'], $conf[$type . '.']['overwrite.']['bcc.'])) {
         $bccArray = GeneralUtility::trimExplode(',', $cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['bcc'], $conf[$type . '.']['overwrite.']['bcc.']), TRUE);
         $message->setBcc($bccArray);
     }
     // add Return Path
     if ($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['returnPath'], $conf[$type . '.']['overwrite.']['returnPath.'])) {
         $message->setReturnPath($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['returnPath'], $conf[$type . '.']['overwrite.']['returnPath.']));
     }
     // add Reply Addresses
     if ($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['replyToEmail'], $conf[$type . '.']['overwrite.']['replyToEmail.']) && $cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['replyToName'], $conf[$type . '.']['overwrite.']['replyToName.'])) {
         $replyArray = array($cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['replyToEmail'], $conf[$type . '.']['overwrite.']['replyToEmail.']) => $cObj->cObjGetSingle($conf[$type . '.']['overwrite.']['replyToName'], $conf[$type . '.']['overwrite.']['replyToName.']));
         $message->setReplyTo($replyArray);
     }
     // add priority
     if ($settings[$type]['overwrite']['priority']) {
         $message->setPriority(intval($settings[$type]['overwrite']['priority']));
     }
     // add attachments from upload fields
     if ($settings[$type]['attachment']) {
         /** @var \In2code\Powermail\Domain\Model\Answer $answer */
         foreach ($mail->getAnswers() as $answer) {
             $values = $answer->getValue();
             if ($answer->getValueType() === 3 && is_array($values) && !empty($values)) {
                 foreach ($values as $value) {
                     $file = $settings['misc']['file']['folder'] . $value;
                     if (file_exists(GeneralUtility::getFileAbsFileName($file))) {
                         $message->attach(\Swift_Attachment::fromPath($file));
                     } else {
                         GeneralUtility::devLog('Error: File to attach does not exist', 'powermail', 2, $file);
                     }
                 }
             }
         }
     }
     // add attachments from TypoScript
     if ($cObj->cObjGetSingle($conf[$type . '.']['addAttachment'], $conf[$type . '.']['addAttachment.'])) {
         $files = GeneralUtility::trimExplode(',', $cObj->cObjGetSingle($conf[$type . '.']['addAttachment'], $conf[$type . '.']['addAttachment.']), TRUE);
         foreach ($files as $file) {
             if (file_exists(GeneralUtility::getFileAbsFileName($file))) {
                 $message->attach(\Swift_Attachment::fromPath($file));
             } else {
                 GeneralUtility::devLog('Error: File to attach does not exist', 'powermail', 2, $file);
             }
         }
     }
     if ($email['format'] != 'plain') {
         $message->setBody($this->createEmailBody($email, $mail, $settings), 'text/html');
     }
     if ($email['format'] != 'html') {
         $message->addPart($this->makePlain($this->createEmailBody($email, $mail, $settings)), 'text/plain');
     }
     $message->send();
     // update mail (with parsed fields)
     if ($type === 'receiver') {
         $mail->setSenderMail($email['senderEmail']);
         $mail->setSenderName($email['senderName']);
         $mail->setSubject($email['subject']);
     }
     return $message->isSent();
 }