Tuesday, March 1, 2016

PHP mail() with RaspberryPi and Gmail

Install ssmtp

sudo apt-get install ssmtp


Configure ssmtp

sudo nano /etc/ssmtp/ssmtp.conf
-----------------------------------------------------------------------------
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=your-full-gmail-address

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=your-full-gmail-address

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

UseSTARTTLS=YES
AuthUser=your-gmail-username-here
AuthPass=your-gmail-password-here

---------------------------------------------------

Setup your users


sudo nano /etc/ssmtp/revaliases
root:username@gmail.com:smtp.gmail.com:587
localusername:username@gmail.com:smtp.gmail.com:587

Configure PHP

sudo nano /etc/php5/apache2/php.ini
------- sendmail_path = /usr/sbin/ssmtp -t
sudo service apache2 restart

Send a mail

<?php
$to = "********@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
mail($to,$subject,$message,"From: $from");
echo "The email has been sent.";
?>

Friday, December 11, 2015

Livesearch php and ajax

  <!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> //ur getting the jquery via online
 <script>
 $(document).ready(function(){
    $("#textBoxId").change(function() //triggers when you change the value in your textbox
    {
       var value = $(this).val(); //gets the value of your textbox
       $.post("search.php", {id:value},function(data)
           $("#results").append(data);
        }); 
    }
 });
</script>
</head>
<body>
 <input type="text" id="textBoxId"/>
<br>
<div id="results"></div>
</body>
</html>
And in your php:
<?php
mysqli_connect("localhost", "#", "#") or die(mysqli_connect_errno());
mysql_select_db("#") or die(mysql_error());
$search = $_POST['id'];
$returnData = ""; 
$players = mysql_query("SELECT firstname FROM players WHERE firstname LIKE '%search%'");
while($player = mysql_fetch_array($players)) {
    $returnData .= "<div>" . $players["firstname"] . "</div>";
}
echo $returnData; 

Measuring PHP Page Load Time

Put the following code at the very top of your PHP page (if you measure the time needed for particular part of the code put this right before that PHP code part)

1
2
3
4
5
6
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
?>


The following code has to be put at the very end of the web page (or the end of the PHP code part)

1
2
3
4
5
6
7
8
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in '.$total_time.' seconds.';
?>