Send generic email message if condition is met via php

I can't find how to do this on the Web. I want to send an email via php after a condition is met.

Any ideas on how to do it?
dynamicOneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent DyerIT Security Analyst SeniorCommented:
Does this not satisfy you needs?

http://us1.php.net/manual/en/function.mail.php

There are quite a few examples provided by the community..  Including using If.. checking, etc.

HTH,

Kent
0
dynamicOneAuthor Commented:
Kent,

Maybe I am just not seeing it. What I want to do is send an email when the state of my garage door changes. I am reading the state of the door using a magnetic switch. Here is the code I am using.

<?php

$switch = exec('/usr/local/bin/gpio read 1');

if ($switch == "1"){
            echo "<img src='garage-door-open.jpeg' width='497px' height='448px'/>";
      }
if ($switch == "0"){
            echo "<img src='garage-door-close.jpeg' width='497px' height='448px'/>";
      }

?>

In addition to echoing the image, I would like to send an email. I have .php file created that when it is loaded in the browser it sends a message. Is there a way I could call that file without displaying it in the browser?
0
Kent DyerIT Security Analyst SeniorCommented:
Hmm..  let's back up and look at the following:
http://us1.php.net/manual/en/function.mail.php

We can define a function to do this.
<?php

function multi_attach_mail($to, $files, $sendermail){
    // email fields: to, from, subject, and so on
    $from = "Files attach <".$sendermail.">";
    $subject = date("d.M H:i")." F=".count($files);
    $message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
    $headers = "From: $from";
 
    // boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
 
    // headers for attachment
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
 
    // multipart boundary
    $message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
 
    // preparing attachments
    for($i=0;$i<count($files);$i++){
        if(is_file($files[$i])){
            $message .= "--{$mime_boundary}\n";
            $fp =    @fopen($files[$i],"rb");
        $data =    @fread($fp,filesize($files[$i]));
                    @fclose($fp);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
            "Content-Description: ".basename($files[$i])."\n" .
            "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            }
        }
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $sendermail;
    $ok = @mail($to, $subject, $message, $headers, $returnpath);
    if($ok){ return $i; } else { return 0; }
}

$switch = exec('/usr/local/bin/gpio read 1');

if ($switch == "1"){
            echo "<img src='garage-door-open.jpeg' width='497px' height='448px'/>";
//send e-mail
multi_attach_mail('to@company.com', '/home/root/images/garage-door-open.jpeg', 'admin@company.com')
      }
if ($switch == "0"){
            echo "<img src='garage-door-close.jpeg' width='497px' height='448px'/>";
//send e-mail
multi_attach_mail('to@company.com', '/home/root/images/garage-door-close.jpeg', 'admin@company.com')
      }

?>

Open in new window


HTH,

Kent

kdyer
I have edited your question to point to the proper PHP Mailer page
GaryC123
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dynamicOneAuthor Commented:
Thanks for the help Kent, I was over thinking it. It was just a matter of creating a function and calling it in the condition.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.