Anyone know how to send an email upon insert from a non-logged in user.
sample would be great.
Any help would be appreciated.
send and email upon insert from non-logged in user
Re: send and email upon insert from non-logged in user
Hi,
just a quick shot, not tested... somehow pseudocode:
In the hooks/tablename.php look for the function after_insert
In the function, add the following code (before the return TRUE statement of course):
Of course, define the variables of what you want to send before
PS: you can find the sendmail function in /admin/incFunctions.php
Olaf
just a quick shot, not tested... somehow pseudocode:
In the hooks/tablename.php look for the function after_insert
In the function, add the following code (before the return TRUE statement of course):
Code: Select all
$mi = getMemberInfo();
if($mi['username'] == 'guest'){
$result = sendmail($username, $email, $mailSubject , $mailMessage);
}
PS: you can find the sendmail function in /admin/incFunctions.php
Olaf
Some postings I was involved, you might find useful:
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Multi Path Upload (MPU) / dynamic upload folder; SingleEdit - Prevent concurrent edits on records; Field Permissions; Column-Value-Based-Permissions; Custom (error) message; Audit Log; Backup your database; Two Factor Authentication; Block brute force (failed) logins; Add 2nd SAVE CHANGES button
Re: send and email upon insert from non-logged in user
Just a short tip on making your code more future-proof:
Nowadays, anonymous users (AKA "guests") may be named differently in configuration. So, instead of string-comparing to
That function already uses the currently configured username for anonymous users. Therefore, if the configuration is changed later on, you do not need to modify your source code.
Nowadays, anonymous users (AKA "guests") may be named differently in configuration. So, instead of string-comparing to
'guest'
it will be safer and more future-proof to use built-in function isGuest
like this:Code: Select all
if (Authentication::isGuest()) {
// your code for guest-users here
}
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 25.10 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 25.10 + all AppGini Helper tools
Re: send and email upon insert from non-logged in user
Thank you both for the input!
Re: send and email upon insert from non-logged in user
Can't get it to work. What am I doing wrong? Thanks you in advance!
-------
function INK_Orders_before_insert(&$data, $memberInfo, &$args) {
$mi = getMemberInfo();
if($mi['username'] == 'guest'){
$result = sendmail($username, $email, $mailSubject , $mailMessage);
}
sendmail(array(
'to' => '[email protected]',
'name' => 'Ink Order',
'subject' => 'A new Ink Order needs your attention',
'message' => "A new Ink Order has been submitted. Login to view it."
));
return TRUE;
}
-------
function INK_Orders_before_insert(&$data, $memberInfo, &$args) {
$mi = getMemberInfo();
if($mi['username'] == 'guest'){
$result = sendmail($username, $email, $mailSubject , $mailMessage);
}
sendmail(array(
'to' => '[email protected]',
'name' => 'Ink Order',
'subject' => 'A new Ink Order needs your attention',
'message' => "A new Ink Order has been submitted. Login to view it."
));
return TRUE;
}
Re: send and email upon insert from non-logged in user
There are a few things to say here:
(1) Email Configuration
(Skip this if sending emails already works)
Before sending from code, I recommend testing email-sending first. Therefore, go to Admin Area / Members, click on admin's email address, then send a test mail and see if this works.
If testing fails, go to Admin-Area / Admin-Setting / E-Mail and check the settings there. Repeat sending test emails until it works.
Before you go any further with the code, THAT has to work!
---
(2) Variables
Try this:
REPLACE
---
(3) Hook
Well, there may be different opinions about this: I recommend sending those emails AFTER insert.
Reason: Insert may fail, but then mail has already been sent. Or vice versa: Mail-sending fails with an exception and therefore record will not be inserted. Second is even worse.
So, if you send the email in
At least, add error-handling for your sendmail-code to ensure data integrity.
---
PS: Please put code fragments in
PPS: @BigProf It wold be great if we could have a "Test" button in E-Mail settings.
(1) Email Configuration
(Skip this if sending emails already works)
Before sending from code, I recommend testing email-sending first. Therefore, go to Admin Area / Members, click on admin's email address, then send a test mail and see if this works.
If testing fails, go to Admin-Area / Admin-Setting / E-Mail and check the settings there. Repeat sending test emails until it works.
Before you go any further with the code, THAT has to work!
---
(2) Variables
Maybe I am wrong, but from the code I can see the following variables are undefined:Code: Select all
if($mi['username'] == 'guest') { $result = sendmail($username, $email, $mailSubject , $mailMessage); }
-
$username
-
$email
-
$mailSubject
-
$mailMessage
If you want to send anything to anyone, you will have to define and pass valid values, not only undeclared variables.Of course, define the variables of what you want to send before
Try this:
REPLACE
TABLENAME
AND [email protected]
BY YOUR SPECIFIC DATACode: Select all
function TABLENAME_before_insert(&$data, $memberInfo, &$args)
{
if (Authentication::isGuest()) {
sendmail([
'to' => '[email protected]',
'subject' => 'Record inserted',
'message' => 'A new record has been inserted by anonymous user'
]);
} else {
// whatever, perhaps send a different email
}
return TRUE;
}
(3) Hook
Well, there may be different opinions about this: I recommend sending those emails AFTER insert.
Reason: Insert may fail, but then mail has already been sent. Or vice versa: Mail-sending fails with an exception and therefore record will not be inserted. Second is even worse.
So, if you send the email in
TABLENAME_after_insert
-hook, at least you are sure data has been inserted into database, which, from my point of view, is more important.At least, add error-handling for your sendmail-code to ensure data integrity.
---
PS: Please put code fragments in
[code]...[/code]
blocks for better readabilityPPS: @BigProf It wold be great if we could have a "Test" button in E-Mail settings.
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 25.10 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 25.10 + all AppGini Helper tools
Re: send and email upon insert from non-logged in user
@jsetzer,
Please send me you paypal. Thank you for always helping me with my sticking points in my code!
Tony
Please send me you paypal. Thank you for always helping me with my sticking points in my code!
Tony
Re: send and email upon insert from non-logged in user
Thanks, Tony!
There is a "buy me a coffee" link in my profile page:
Buy me a coffee:
https://www.paypal.me/cash4code

There is a "buy me a coffee" link in my profile page:
Buy me a coffee:
https://www.paypal.me/cash4code

Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
AppGini 25.10 + all AppGini Helper tools
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readabilityAppGini 25.10 + all AppGini Helper tools