Skip to content

Commit

Permalink
[FEATURE] Allow embedding images in emails (resolves #46215)
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/formhandler/trunk@85359 735d13b6-9817-0410-8766-e36946ffe9aa
  • Loading branch information
reinhardfuehricht committed May 21, 2014
1 parent 124b8f6 commit 3c976ae
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 21 deletions.
77 changes: 56 additions & 21 deletions Classes/Finisher/Tx_Formhandler_Finisher_Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,8 @@ protected function sendMail($type) {
$template['html'] = $html;
}

//init mailer object
$emailClass = $this->utilityFuncs->getPreparedClassName($this->settings['mailer.'], 'Mailer_HtmlMail');
$emailObj = $this->componentManager->getComponent($emailClass);
$emailObj->init($this->gp, $this->settings['mailer.']['config.']);

//set e-mail options
$emailObj->setSubject($mailSettings['subject']);
$this->emailObj->setSubject($mailSettings['subject']);

$sender = $mailSettings['sender_email'];
if (isset($mailSettings['sender_email']) && is_array($mailSettings['sender_email'])) {
Expand All @@ -162,7 +157,7 @@ protected function sendMail($type) {
$senderName = implode(',', $mailSettings['sender_name']);
}

$emailObj->setSender($sender, $senderName);
$this->emailObj->setSender($sender, $senderName);

$replyto = $mailSettings['replyto_email'];
if (isset($mailSettings['replyto_email']) && is_array($mailSettings['replyto_email'])) {
Expand All @@ -173,7 +168,7 @@ protected function sendMail($type) {
if (isset($mailSettings['replyto_name']) && is_array($mailSettings['replyto_name'])) {
$replytoName = implode(',', $mailSettings['replyto_name']);
}
$emailObj->setReplyTo($replyto, $replytoName);
$this->emailObj->setReplyTo($replyto, $replytoName);

$cc = $mailSettings['cc_email'];
if (!is_array($cc)) {
Expand All @@ -190,7 +185,7 @@ protected function sendMail($type) {
$name = $ccName[$key];
}
if (strlen($email) > 0) {
$emailObj->addCc($email, $name);
$this->emailObj->addCc($email, $name);
}
}

Expand All @@ -209,7 +204,7 @@ protected function sendMail($type) {
$name = $bccName[$key];
}
if (strlen($email) > 0) {
$emailObj->addBcc($email, $name);
$this->emailObj->addBcc($email, $name);
}
}

Expand All @@ -221,16 +216,16 @@ protected function sendMail($type) {
$returnPath = $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'];
}

$emailObj->setReturnPath($returnPath);
$this->emailObj->setReturnPath($returnPath);

if ($mailSettings['email_header']) {
$emailObj->addHeader($mailSettings['header']);
$this->emailObj->addHeader($mailSettings['header']);
}

if (strlen(trim($template['plain'])) > 0) {
$emailObj->setPlain($template['plain']);
$this->emailObj->setPlain($template['plain']);
} else {
$emailObj->setPlain(NULL);
$this->emailObj->setPlain(NULL);
}

if (strlen(trim($template['html'])) > 0) {
Expand All @@ -248,10 +243,10 @@ protected function sendMail($type) {
fwrite($tmphandle, $template['html']);
fclose($tmphandle);
$this->utilityFuncs->debugMessage('adding_html', array(), 1, array($template['html']));
$emailObj->addAttachment($tmphtml);
$this->emailObj->addAttachment($tmphtml);
}
} else {
$emailObj->setHtml($template['html']);
$this->emailObj->setHtml($template['html']);
}
}

Expand All @@ -260,7 +255,7 @@ protected function sendMail($type) {
}
foreach ($mailSettings['attachment'] as $idx => $attachment) {
if (strlen($attachment) > 0 && @file_exists($attachment)) {
$emailObj->addAttachment($attachment);
$this->emailObj->addAttachment($attachment);
} else {
$this->utilityFuncs->debugMessage('attachment_not_found', array($attachment), 2);
}
Expand All @@ -269,7 +264,7 @@ protected function sendMail($type) {
$files = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $mailSettings['attachGeneratedFiles']);
$this->utilityFuncs->debugMessage('adding_generated_files', array(), 1, $files);
foreach($files as $file) {
$emailObj->addAttachment($file);
$this->emailObj->addAttachment($file);
}
}

Expand All @@ -296,14 +291,14 @@ protected function sendMail($type) {
}
$sent = FALSE;
if ($doSend && !empty($recipients)) {
$sent = $emailObj->send($recipients);
$sent = $this->emailObj->send($recipients);
}
if ($sent) {
$this->utilityFuncs->debugMessage('mail_sent', array(implode(',', $recipients)));
} else {
$this->utilityFuncs->debugMessage('mail_not_sent', array(implode(',', $recipients)), 2);
}
$this->utilityFuncs->debugMailContent($emailObj);
$this->utilityFuncs->debugMailContent($this->emailObj);
if ($tmphtml) {
unlink($tmphtml);
}
Expand Down Expand Up @@ -422,13 +417,40 @@ protected function parseFilesList($settings ,$type, $key) {
}
} elseif(file_exists($file)) {
array_push($parsed, $file);
} elseif(file_exists(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $file)) {
array_push($parsed, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $file);
} elseif(strlen($file) > 0) {
$this->utilityFuncs->debugMessage('attachment_not_found', array($file), 2);
}
}
return $parsed;
}

/**
* Parses a list of file names or field names set in TypoScript to embed in the mail.
*
* @param array $settings The settings array containing the mail settings
* @return array
*/
protected function parseEmbedFilesList($settings) {
$cids = array();
foreach ($settings['embedFiles.'] as $key => $embedFileSettings) {
if(strpos($key, '.') === FALSE) {
$embedFile = $this->utilityFuncs->getSingle($settings['embedFiles.'], $key);
if (strlen($embedFile) > 0) {
if(!strstr($embedFile, $this->utilityFuncs->getDocumentRoot())) {
$embedFile = $this->utilityFuncs->getDocumentRoot() . '/' . $embedFile;
}
$embedFile = $this->utilityFuncs->sanitizePath($embedFile);
$cids[$key] = $this->emailObj->embed($embedFile);
} else {
$this->utilityFuncs->debugMessage('attachment_not_found', array($embedFile), 2);
}
}
}
return $cids;
}

/**
* Substitutes markers like ###LLL:langKey### in given TypoScript settings array.
*
Expand Down Expand Up @@ -466,6 +488,13 @@ protected function getSettings() {
*/
public function init($gp, $tsConfig) {
$this->gp = $gp;
$this->settings = $tsConfig;

//init mailer object
$emailClass = $this->utilityFuncs->getPreparedClassName($this->settings['mailer.'], 'Mailer_HtmlMail');
$this->emailObj = $this->componentManager->getComponent($emailClass);
$this->emailObj->init($this->gp, $this->settings['mailer.']['config.']);

$this->settings = $this->parseEmailSettings($tsConfig);

// Defines default values
Expand All @@ -483,6 +512,7 @@ public function init($gp, $tsConfig) {
// Unset unnecessary variables.
unset($this->settings['admin.']);
unset($this->settings['user.']);

}

/**
Expand All @@ -508,6 +538,7 @@ protected function parseEmailSettings($tsConfig) {
'to_name',
'return_path',
'attachment',
'embedFiles',
'attachGeneratedFiles',
'deleteGeneratedFiles',
'htmlEmailAsAttachment',
Expand Down Expand Up @@ -565,6 +596,10 @@ protected function parseEmailSettingsByType($currentSettings, $type, $optionsToP
case 'attachment':
$emailSettings[$option] = $this->parseFilesList($currentSettings, $type, $option);
break;

case 'embedFiles':
$emailSettings[$option] = $this->parseEmbedFilesList($currentSettings, $type, $option);
break;

case 'attachPDF':
case 'attachGeneratedFiles':
Expand Down Expand Up @@ -626,4 +661,4 @@ protected function parseEmailSettingsByType($currentSettings, $type, $optionsToP
return $emailSettings;
}
}
?>
?>
8 changes: 8 additions & 0 deletions Classes/Mailer/Tx_Formhandler_MailerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ public function getBcc();
*/
public function getReturnPath();

/**
* Embeds an image to the email content
*
* @param string $image The image path
* @return void
*/
public function embed($image);

}

?>
3 changes: 3 additions & 0 deletions Classes/Mailer/Tx_Formhandler_Mailer_HtmlMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public function getReturnPath() {
return $this->emailObj->returnPath;
}

public function embed($image) {
throw new Exception('This mailer doesn\'t support mebedding images. Please use Mailer_TYPO3Mailer.');
}
}

?>
5 changes: 5 additions & 0 deletions Classes/Mailer/Tx_Formhandler_Mailer_TYPO3Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public function getBcc() {
public function getReturnPath() {
return $this->emailObj->getReturnPath();
}

public function embed($image) {
return $this->emailObj->embed(Swift_Image::fromPath($image));
}

}

?>
14 changes: 14 additions & 0 deletions Classes/View/Tx_Formhandler_View_Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public function render($gp, $errors) {
public function pi_wrapInBaseClass($content) {
return $content;
}

protected function fillEmbedMarkers($content) {
$componentSettings = $this->getComponentSettings();

$mailSettings = $componentSettings[$this->currentMailSettings['mode']];
if (isset($mailSettings['embedFiles']) && is_array($mailSettings['embedFiles'])) {
$markers = array();
foreach ($mailSettings['embedFiles'] as $key => $cid) {
$markers['###embed_' . $key . '###'] = $cid;
}
$this->template = $this->cObj->substituteMarkerArray($this->template, $markers);
}
}

protected function fillValueMarkers() {
$componentSettings = $this->getComponentSettings();
Expand Down Expand Up @@ -76,6 +89,7 @@ protected function fillValueMarkers() {
//remove remaining VALUE_-markers
//needed for nested markers like ###LLL:tx_myextension_table.field1.i.###value_field1###### to avoid wrong marker removal if field1 isn't set
$this->template = preg_replace('/###value_.*?###/i', '', $this->template);
$this->fillEmbedMarkers();
}

/**
Expand Down

0 comments on commit 3c976ae

Please sign in to comment.