Jump to content

Send email when page accessed


Ralph2

Recommended Posts

I have a small family only website and I use this code on my login page to see if members are having trouble re logging in.

// routine writes to file when this page is accessed

<?php
$logfile = '/home/xxxxxxx/public_html/temp/trouble.txt';
$date = date('j M Y H:i:s');
if (isset($_COOKIE["family_user"])) {
    $user = $_COOKIE["family_user"];
}
if ($file = @fopen($logfile, 'a')) {
  fwrite($file, "\r\n$date  $user ");
  fclose($file);
}
?>

 

How can I use the information gathered (fwrite($file, "\r\n$date  $user ") that writes to my trouble.txt file be sent to me via email. And ideally only once per session.

 

I found this code will send me an email every time the page is accessed but can not figure out how to get fwrite($file, "\r\n$date  $user " into either the subject (preferred) or into the body. 

 

<?php mail('yourEmailAddress@yourDomain.com','Subject of the e-mail','This is the body of the e-mail!'); ?>

 

Anyone able to help with the code :happy-04:

 

Thank you for your time

Link to comment
Share on other sites

Hi.

 

Could you clarify please. Which part of the info are you trying to get into the subject of your email? The date and the user?

 

Look at the mail() function on http://php.net/mail as well, especially if you're new to this. You don't want to be opening up a vector for malicious users to send emails from your server.

Link to comment
Share on other sites

...can not figure out how to get fwrite($file, "\r\n$date  $user " into either the subject (preferred) or into the body. 

 

If you post the attempted code, we may be able to let you know why it's not working. If you post more code, please surround it with


tags. It makes the code and post easier to follow.  :happy-04:

 

 

When including a variable like $user in the subject line, are you using single or double quotes? Note that you need to use double quotes for a variable to work in a string. For example:

<?php mail('yourEmailAddress@yourDomain.com', "$user is unable to log in", 'This is the body of the e-mail!'); ?>
Edited by cyberRobot
Link to comment
Share on other sites

Thanks all, let me try to clarify. My family members are logged in via a PHP script that checks for their name and password in my login.php. All my pages then start with.

<?php require('../login.php'); ?>

I am trying to have, when this page is accessed an email sent to me with the users name. Throwing this into the page will send me an email but I can not figure out what else I need to have the users name either in the subject or body of the email.

<?php require('../login.php'); ?>
<?php
mail('yourEmailAddress@yourDomain.com','Subject of the e-mail','This is the body of the e-mail!'); 
?>

I am very much a cut and paste programmer, I can figure out, sort of... what is happening after I have the code. So please..  be gentle :happy-04:  and complete with your help / advice.

Link to comment
Share on other sites

Hi again,

 

Try building the string for the subject before you use it in the mail() function (see below) OR use double-quotes as advised.

$subject_info = "Login attempt by Username: " . $user;
 
mail('yourEmailAddress@yourDomain.com', $subject_info, 'This is the body of the e-mail!'); 

I'm pretty certain you have now been give all the info (or pointers to it) that you require to resolve this issue. As recommended (several times), go and read about the mail() function at php.net - and there are lots of examples and tutorials on this topic on the Internet. Google is your friend (in this circumstance anyway).

 

Good luck.

Link to comment
Share on other sites

Thanks to all who have pasted code in an attempt to help and suggested I read the manual on mail. But.. I am still confused as to why my code is not working. I think.. a PHP only solution should be possible.

 

I have a logged in user with an active session. What is missing in my code that is preventing the $user field from being correctly populated with the variable user?

<?php require('../login.php'); ?>
<?php
if (isset($_COOKIE["user"])) {
	$user = $_COOKIE["user"];
}
mail('me@telusplanet.net','$user','From news page'); 
?>

Thanks again.. :happy-04:

Edited by Ralph2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.