The site has a form for ordering price lists. They are taken from the directory in the site directory.
I go through and generate links like this:
$dir = 'prices';
if ( $handle = opendir( $dir )
{
while ( $file = readdir($handle) )
{
for ($i=0; $i<count($file); $i++)
if(($file != '.') && ($file != '.') && ($file[0] != '.')) {
$file = mb_convert_encoding($file, "utf-8", "windows-1251");
$file = '<br /><a href="https://.../prices/'.$file.'">'.$file.'</a>';
$fileOrder[] = $file;
}
};
closedir( $handle );
};
The whole hook with sending the email looks like this:
<?php
$fields = $hook->getValues();
$message = $modx->getChunk('tpl.mail.price', $fields);
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $message);
$modx->mail->set(modMail::MAIL_FROM, $modx->getOption('emailsender'));
$modx->mail->set(modMail::MAIL_FROM_NAME, $modx->getOption('site_name'));
$modx->mail->set(modMail::MAIL_SUBJECT, 'Price List');
$modx->mail->address('to', $fields['email']);
$modx->mail->address('reply-to', $modx->getOption('emailsender'));
$modx->mail->setHTML(true);
$dir = 'prices';
if ( $handle = opendir( $dir )
{
while ( $file = readdir($handle) )
{
for ($i=0; $i<count($file); $i++)
if(($file != '.') && ($file != '.') && ($file[0] != '.')) {
$file = mb_convert_encoding($file, "utf-8", "windows-1251");
$file = '<br /><a href="https://.../prices/'.$file.'">'.$file.'</a>';
$fileOrder[] = $file;
}
};
closedir( $handle );
};
$hook->setValue('fileOrder', $fileOrder);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
return true;
In the email I call the placeholder like this:
[[+fileOrder]]
.
Calling a snippet
[[!AjaxForm?
&snippet=`FormIt`
&form=`tpl.price.form`
&hooks=`hookSendFile`
&validate=`email:minLength=^4^,inputt:blank`
]]
As a result, I get an email with the text “[[+fileOrder]]” instead of an array.
How do I make a hook send a placeholder?
I apologize for the mistakes. I use a translator.
antar
July 18, 2021, 3:41pm
2
Order is important, try this please:
$fields = $hook->getValues();
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $message);
$modx->mail->set(modMail::MAIL_FROM, $modx->getOption('emailsender'));
$modx->mail->set(modMail::MAIL_FROM_NAME, $modx->getOption('site_name'));
$modx->mail->set(modMail::MAIL_SUBJECT, 'Price List');
$modx->mail->address('to', $fields['email']);
$modx->mail->address('reply-to', $modx->getOption('emailsender'));
$modx->mail->setHTML(true);
$dir = 'prices';
if ( $handle = opendir( $dir )
{
while ( $file = readdir($handle) )
{
for ($i=0; $i<count($file); $i++)
if(($file != '.') && ($file != '.') && ($file[0] != '.')) {
$file = mb_convert_encoding($file, "utf-8", "windows-1251");
$file = '<br /><a href="https://.../prices/'.$file.'">'.$file.'</a>';
$fileOrder[] = $file;
}
};
closedir( $handle );
};
//$hook->setValue('fileOrder', $fileOrder);
$fields['fileOrder'] = implode(",", $fileOrder);// or other look for pure array,just an idea how to pass the value
$message = $modx->getChunk('tpl.mail.price', $fields);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
return true;
In your case, the letter does not come in the mail. But if I understood correctly, I made the snippet like this, only the result did not change
$fields = $hook->getValues();
$dir = 'prices';
if ( $handle = opendir( $dir ) )
{
while ( $file = readdir($handle) )
{
for ($i=0; $i<count($file); $i++)
if(($file != '.') && ($file != '..') && ($file[0] != '.')) {
$file = mb_convert_encoding($file, "utf-8", "windows-1251");
$file = '<br /><a href="https://.../prices/'.$file.'">'.$file.'</a>';
$fileOrder[] = $file;
}
};
closedir( $handle );
};
$hook->setValue('fileOrder', $fileOrder);
$message = $modx->getChunk('tpl.mail.price', $fields);
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $message);
$modx->mail->set(modMail::MAIL_FROM, $modx->getOption('emailsender'));
$modx->mail->set(modMail::MAIL_FROM_NAME, $modx->getOption('site_name'));
$modx->mail->set(modMail::MAIL_SUBJECT, 'Прайс-лист');
$modx->mail->address('to', $fields['email']);
$modx->mail->address('reply-to', $modx->getOption('emailsender'));
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
return true;
Added information about calling the snippet to the post
As @himurovich wrote, add fileOrder
to your $fields
-array ($fields['fileOrder'] = implode(",", $fileOrder);
) instead of using $hook->setValue('fileOrder', $fileOrder);
.
Or maybe reordering the lines like this may also work:
...
$hook->setValue('fileOrder', $fileOrder);
$fields = $hook->getValues();
$message = $modx->getChunk('tpl.mail.price', $fields);
system
Closed
July 20, 2021, 4:16pm
5
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.