Page 1 of 1

send and email upon insert from non-logged in user

Posted: 2023-08-27 04:07
by utony
Anyone know how to send an email upon insert from a non-logged in user.

sample would be great.

Any help would be appreciated.

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

Posted: 2023-08-27 07:42
by onoehring
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

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

Posted: 2023-08-27 07:57
by jsetzer
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.

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

Posted: 2023-08-28 02:17
by utony
Thank you both for the input!

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

Posted: 2023-08-28 22:03
by utony
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;
}

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

Posted: 2023-08-29 05:17
by jsetzer
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 5782 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.

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

Posted: 2023-08-29 23:03
by utony
@jsetzer,

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

Posted: 2023-08-30 07:17
by jsetzer
Thanks, Tony!

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

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

:geek: