Skip to main content
Snippets

Send Email Automatically Upon Page Load

By February 27, 2016No Comments

I have a website with a password protected area and required “Name” field. Both are set as cookies so the data can be used on a welcome page. The password protected area is setup so anyone can access without having to create user accounts etc as long as they have the correct password.

A client requested that we setup something so when a person logs in an email notice with the persons name is sent. I wanted a solution that would just pass the name in an email when the welcome page populates after successful login.

When the client receives the notification on a smartphone etc the email doesn’t have to actually be opened as the name is in the subject line (smart!). I also added a timestamp to the body of the email and set the timezone to EST as the date() function will reference the server time and our servers are in a different timezone than the client.

$to = 'email@address.com'; 
date_default_timezone_set('America/New_York');
$date = date('m/d/Y');
$time = date('h:i a');
$subject = 'Notice: '.$_COOKIE['name_whatevercookiedatayouwant'].' has logged in at password protected page'; 
$message = $_COOKIE['name_whatevercookiedatayouwant'] . ' accessed the password protected page at website.com on '.$date.' at '.$time; 
mail ($to, $subject, $message);

There is something I refuse to obsess over (OCD) – if the user is still logged in and visits the welcome page with a hard refresh (not clearing cookies of course) the email will be sent again. I don’t think this is really an issue though.

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton