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 (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.