Ralph2 Posted January 6, 2015 Share Posted January 6, 2015 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 Thank you for your time Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/ Share on other sites More sharing options...
wezhind Posted January 6, 2015 Share Posted January 6, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501890 Share on other sites More sharing options...
cyberRobot Posted January 6, 2015 Share Posted January 6, 2015 (edited) ...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. 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 January 6, 2015 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501903 Share on other sites More sharing options...
Ralph2 Posted January 6, 2015 Author Share Posted January 6, 2015 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 and complete with your help / advice. Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501936 Share on other sites More sharing options...
ginerjm Posted January 6, 2015 Share Posted January 6, 2015 As suggested, read the manual on using the mail() function. YOu are missing the headers. Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501937 Share on other sites More sharing options...
wezhind Posted January 7, 2015 Share Posted January 7, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501940 Share on other sites More sharing options...
cyberRobot Posted January 7, 2015 Share Posted January 7, 2015 As suggested, read the manual on using the mail() function. YOu are missing the headers. The manual mentions that the headers are optional...assuming that the php.ini contains a default for the from header. Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1501993 Share on other sites More sharing options...
Ralph2 Posted January 7, 2015 Author Share Posted January 7, 2015 (edited) 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.. Edited January 7, 2015 by Ralph2 Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1502025 Share on other sites More sharing options...
cyberRobot Posted January 7, 2015 Share Posted January 7, 2015 To use a variable within a string, you need to use double quotes. mail('me@telusplanet.net',"$user",'From news page'); In this case, the quotes technically are not needed. This should work just fine: mail('me@telusplanet.net',$user,'From news page'); Quote Link to comment https://forums.phpfreaks.com/topic/293700-send-email-when-page-accessed/#findComment-1502029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.