How to get an Email notification when one of my Easter eggs is found?

Hi there!
So I googles, How to get an Email notification when one of my site pages is viewed?
and I found the following code:

<?php $message = "Line 1\r\nLine 2\r\nLine 3"; $message = wordwrap($message, 70, "\r\n"; mail('myemail@hotmail.com', 'my subject', $message); ?>

So then I googled " can i put php code into a html page?"
and found an article that said yes.
here is what the snippet of html and php looks like. But it doesn’t work. the page shows the php code on the web page like as if its content.

</ul>

</div>

<?php

$message = "Line 1\r\nLine 2\r\nLine 3";

$message = wordwrap($message, 70, "\r\n");

mail('myemail@hotmail.com', 'my subject', $message);

?>

<!-- partial -->

<script src="./script.js"></script>

<script src="./switch.js"></script>

</body>

</html>

Have you confirmed that you have PHP available on your server? I’m thinking you don’t…

Actually another option is that you do have PHP on the server, but the page is named something.html, when it would need (usually) to be named something.php when you have php code to run in it.

2 Likes

Hey there!

You’re trying to integrate PHP code into an HTML page to get email notifications when a page is viewed. Based on the code that you have provided.

First, you must ensure that your web server supports PHP, which is pretty standard but always worth checking.

The critical thing is that your file needs to be saved with a .php extension, not .html. If you use an .html extension, the server won’t process the PHP code, and it’ll just display it as text on your page, which seems to be the issue you’re running into.

Here’s how you might want to set up your file:

<?php
$message = "Line 1\r\nLine 2\r\nLine 3";
$message = wordwrap($message, 70, "\r\n");
// It's a good idea to turn on error reporting while testing
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Replace 'myemail@hotmail.com' with your actual email address
if(mail('myemail@hotmail.com', 'My Subject', $message)) {
    echo 'Email sent successfully.';
} else {
    echo 'Email sending failed.';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Page Title</title>
</head>
<body>
    <h1>This page includes PHP!</h1>
</body>
</html>

Here are a few tips:

  • Place your PHP code before any HTML content. This way, the PHP code is executed before the server sends the page to be viewed.
  • Make sure your server is configured to send emails. The mail() function should work, but depending on your setup, especially if you’re on a local development environment like XAMPP or MAMP, you might need to do some extra configuration.
  • Keep an eye on security. Directly using the mail() function like this can open up your site to spam if not correctly handled. Look into CAPTCHA or other forms of validation.
  • Be aware that emails sent through mail() might end up in spam folders, depending on factors like your server’s reputation and email content. Following best email practices and possibly using authenticated SMTP can help mitigate this.

If you are still encountering issues even after trying the suggested solution, you can delve deeper into the problem by checking the server logs. Server logs are files that store information about the activities and errors on the server. By going through the server logs, you may be able to identify the root cause of the issue and find a solution accordingly.

If you are unsure how to access the server logs, then you can contact your hosting provider for assistance as they should be able to provide detailed instructions or even help you analyse them.

It is always better to seek help from a professional, as they have the expertise to handle complex issues and can provide valuable insights on how to fix the problem.

Good luck!
Michael Swan

1 Like

Hi Michael,
Thanks for the improved code and for the very detailed instruction and tips!!!
The server (NetFirms), does support PHP and email is set up. I’ll give your suggestions a try and see how it goes.

EDIT:
perfect, it works!!! sends me an email right to my junk box LOL. Put some rules to keep it from going into my email box but those rules don’t work and they still go to junk. But I can live with that.

Much thanks
Glen

1 Like

Hello Glen,

I hope you’re doing well. I noticed that you have edited your latest reply, but unfortunately, I didn’t receive any notification from SitePoint about it. In the future, it might be useful to add an additional reply to keep me updated.

I’m glad to hear that everything is working fine now. However, the email going into your spam folder is something that needs to be addressed by your hosting/email provider. You should ensure DMARC and DKIM are set up correctly, which you can do by contacting your hosting company. Once this is done, your emails should no longer be marked as spam.

Please let me know if you need further assistance. Have a great day!

Best regards,

Michael Swan

Thanks for getting back to me!
I contacted NetFirms and they told me they would not help me with the issue, because the Email that I’m using to receive the notification is a hotmail and not one of my domain Emails.
So I just set up a new Email for myself with my domain and it works! My Email notifications of visitors to the site are coming to my new Inbox.

Thanks once again for all of your great help!
Glen

1 Like

@ghiggs628 ,

That would definitely be an issue with using an external e-mail provider. But I’m glad you have got it sorted.

Michael Swan

That’s fine, so long as you will only ever need to send emails to yourself. If you ever need to send emails to the wider world from your domain, setting up SPF and DKIM is pretty much a must these days, as a lot of providers will dismiss any mail that can prove it came from where it claims to have come from.
It’s bad form that the host wouldn’t help, though if you have access to DNS records it can be set up manually, or some control panels will do it for you if you have that feature.

@SamA74 Thanks for the info!

Glen