send and email upon insert from non-logged in user

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
utony
Veteran Member
Posts: 143
Joined: 2020-04-03 18:37

send and email upon insert from non-logged in user

Post by utony » 2023-08-27 04:07

Anyone know how to send an email upon insert from a non-logged in user.

sample would be great.

Any help would be appreciated.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1231
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: send and email upon insert from non-logged in user

Post by onoehring » 2023-08-27 07:42

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):

Code: Select all

$mi = getMemberInfo();
if($mi['username'] == 'guest'){
  $result = sendmail($username, $email, $mailSubject , $mailMessage);
}    
Of course, define the variables of what you want to send before

PS: you can find the sendmail function in /admin/incFunctions.php

Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1944
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: send and email upon insert from non-logged in user

Post by jsetzer » 2023-08-27 07:57

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 '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
}
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.
Kind regards,
<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 readability

AppGini 25.10 + all AppGini Helper tools

utony
Veteran Member
Posts: 143
Joined: 2020-04-03 18:37

Re: send and email upon insert from non-logged in user

Post by utony » 2023-08-28 02:17

Thank you both for the input!

utony
Veteran Member
Posts: 143
Joined: 2020-04-03 18:37

Re: send and email upon insert from non-logged in user

Post by utony » 2023-08-28 22:03

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;
}

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1944
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: send and email upon insert from non-logged in user

Post by jsetzer » 2023-08-29 05:17

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.
chrome_ratfghd3hg.png
chrome_ratfghd3hg.png (25.58 KiB) Viewed 5761 times
Before you go any further with the code, THAT has to work!

---

(2) Variables

Code: Select all

if($mi['username'] == 'guest') {
    $result = sendmail($username, $email, $mailSubject , $mailMessage);
}
Maybe I am wrong, but from the code I can see the following variables are undefined:
  • $username
  • $email
  • $mailSubject
  • $mailMessage
See Olaf's hint:
Of course, define the variables of what you want to send before
If you want to send anything to anyone, you will have to define and pass valid values, not only undeclared variables.

Try this:

REPLACE TABLENAME AND [email protected] BY YOUR SPECIFIC DATA

Code: 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 readability
PPS: @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 [code]...[/code] blocks for better readability

AppGini 25.10 + all AppGini Helper tools

utony
Veteran Member
Posts: 143
Joined: 2020-04-03 18:37

Re: send and email upon insert from non-logged in user

Post by utony » 2023-08-29 23:03

@jsetzer,

Please send me you paypal. Thank you for always helping me with my sticking points in my code!

Tony

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1944
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: send and email upon insert from non-logged in user

Post by jsetzer » 2023-08-30 07:17

Thanks, Tony!

There is a "buy me a coffee" link in my profile page:

Buy me a coffee:
https://www.paypal.me/cash4code

:geek:
Kind regards,
<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 readability

AppGini 25.10 + all AppGini Helper tools

Post Reply