Hi there,
Here’s my second question, I find this one quite tricky!
I have a form module that displays a quote request beneath each article. Each article is linked to a specific (joomla) user with a unique email address, and that part is working fine.
Now, I’d like the request to be automatically forwarded to the (joomla) user of the corresponding page when a customer fills out and submits the form.
I know there’s a short code to retrieve the applicant’s email from the form, but is there also a way to pull the user’s email directly from the database?The email address must not be visible to the applicant; it should only be used for forwarding.
Is this possible? Right now, I’m still handling everything manually.
Looking forward to your response!
Best regards, Ron 😐👍
Hi Ron
You need the email to be sent to User which is logged in ?
in this case you should use {user:email} in the Email action Recipients
if however, you need to send to the article author's email then you need to get the article info from the database then get the author's info then use their email address, let me know if this what you need and I will prepare the code for you
Hi Max,
The email needs to be sent to the article author's email address, so no one is logged in.
I’m curious to see what the code would be and how to implement it in the form under the submit action.
Thanks in advance for all your effort!
Best regards, Ron 👍
Hi Max,
Have you found anything yet or a working piece of code?
I've been trying to figure it out myself, even though I have little experience with PHP scripts. ChatGPT can do a lot, but writing this specific script doesn’t seem to be one of them (yet).
In the backend, it partially works: when I preview a form with the script, it correctly retrieves the article creator’s email address—likely because I'm logged in. However, in the frontend, when I'm not logged in, it no longer works.
It seems the script can only fetch the email address when the user is logged in. Do you have a solution for this?
Looking forward to your thoughts!
Best regards, Ron 👍
Hi Ron
I have tested this code in a PHP action and it will return the author email which you can use as {var:php_action_name}
// Load Joomla's Factory class
use Joomla\CMS\Factory;
// Get the application object
$app = Factory::getApplication();
// Get the input object to retrieve the article ID from the URL
$input = $app->input;
// Check if we're in an article view and get the article ID
$option = $input->getCmd('option');
$view = $input->getCmd('view');
$articleId = null;
if ($option === 'com_content' && $view === 'article') {
$articleId = $input->getInt('id');
}
// If article ID is found, proceed to get the author's email
if ($articleId) {
// Get the database object
$db = Factory::getDbo();
// Create a query to get the article's created_by field
$query = $db->getQuery(true)
->select($db->quoteName('created_by'))
->from($db->quoteName('#__content'))
->where($db->quoteName('id') . ' = ' . (int)$articleId);
$db->setQuery($query);
$authorId = $db->loadResult();
// If author ID is found, get the author's email
if ($authorId) {
$user = Factory::getUser($authorId);
return $user->email;
}
}
return "";
no problem! 😄