What's a bats' favorite line of code in PHP?

Posted by Dillon Reedy on 2017-01-31

echo $location;

PHP makes you nanners

Prerequisites

First thing you’re going to need to get if you want to take on PHP is get a WAMP server downloaded and installed. Typical errors you’ll run into after installing a WAMP server is that your port number may not be set correctly. To fix that, right click on wamp server icon in the bottom right hand corner and select Tools > “Use a port other than <port number>”. This should fix your port number issue.

Some nice features of PHP

It’s standard that you don’t have to declare variable types, which is something that I love for any programming language.

PHP has this predefined set of super globals which is a new concept to me, but nothing hard to understand:

1
2
3
4
5
$_GET
$_POST
$_COOKIE
$_SESSION
$_SERVER

For the project to follow we do use the $_POST superglobal, I’ll probably come back later to learn more about when to use these properly.

A feature I also like within PHP is it’s very understandable error messages, I can almost instantly understand what needs to be done to fix something.

A cat facts email spammer in PHP #CatFacts

Let’s make a Catabase

In no way is this part necessary, but I liked having the ability to connect to a database in PHP. Therefore, this section can be skipped by those who don’t wish to send text in their e-mails and to only send photos.

With the WAMP server launched, go to:

localhost:<your port number>:/phpmyadmin

Log in using the username “root” and no password for your first time. Make a new username and password, these can be done along the ribbon (mine happen to be “dreedy” and “pswd”). On the sidebar to the left create a database, I called mine “catfacts”. I then created a table within catfacts called “text_cat_facts” that had an index column and a column for the textual cat fact (I named mine “cat_facts_index” and “cat_facts_text”).

Populate our Catabase

With your table created, we now need to put data within it. Poking around online I came across a BuzzFeed article listing random cat facts, so I ended up copy/pasting some of those facts into a .txt file and then creating a C# console application that turned those cat facts into MySQL INSERT statements. I’m sure there is an easier way, but that’s just the way I felt like doing it. This C# project can be found here. If you don’t feel like going through these steps you can just copy and paste these lines here:

1
2
3
4
5
INSERT INTO `text_cat_facts` (`cat_facts_index`, `cat_facts_text`) VALUES ('1', 'Cat Fact #1. Cats are the most popular pet in the United States: There are 88 million pet cats and 74 million dogs.');
INSERT INTO `text_cat_facts` (`cat_facts_index`, `cat_facts_text`) VALUES ('2', 'Cat Fact #2. There are cats who have survived falls from over 32 stories (320 meters) onto concrete.');
INSERT INTO `text_cat_facts` (`cat_facts_index`, `cat_facts_text`) VALUES ('3', 'Cat Fact #3. A group of cats is called a clowder.');
INSERT INTO `text_cat_facts` (`cat_facts_index`, `cat_facts_text`) VALUES ('4', 'Cat Fact #4. Cats have over 20 muscles that control their ears.');
INSERT INTO `text_cat_facts` (`cat_facts_index`, `cat_facts_text`) VALUES ('5', 'Cat Fact #5. Cats sleep 70% of their lives.');

After you’ve done all this, your catabase is all ready to go.

A folder, PHPMailer, html, and a bunch of jpegs

Create a project folder for cat facts in the directory “C:\wamp64\www” and following that we’re going to need a file that takes in our inputs that is an html file. This is all I had to write for mine:

1
2
3
4
<form action="inputs.php" method="post">
Give us the email of your victim: <input type="text" value="" name="givenEmail" />
<input type="submit" value="Send" />
</form>

And following the creation of that .html file, copy the contents of the PHPMailer .zip master folder into your project folder.

After this we’re going to need a folder that contains all of our cat images within our folder structure, mine contains a bunch of .jpgs labelled “cat1.jpg” to “cat25.jpg”

As always I have you covered in case you don’t want to go find any. Cat Images Galore

The PHP Stuff

Now that we are finally all ready for the .php what shall we do? Well we want our final product to look like this:

So first we’re gonna need to connect to the aforementioned database, this code is super short for me, so I’m gonna put it all on here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
require "PHPMailer-master/PHPMailerAutoload.php";
require "EmailSender.php";
$emailSenderBot = new EmailSenderBot();
$servername = "localhost";
$username = "dreedy";
$password = "pswd";
$dbname = "catfacts";
$table = "text_cat_facts";
$col1Name = "cat_facts_index";
$col2Name = "cat_facts_text";
$givenEmailVictim = $_POST['givenEmail'];
$givenEmailName = "Victim";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
// Check connection
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$result = mysqli_query($conn, "SELECT * FROM $table");
for ($i = 0; $i < $result -> num_rows; $i++)
{
$row = mysqli_fetch_assoc($result);
$emailSenderBot->makeEmail($givenEmailVictim, $givenEmailName, $row[$col2Name]);
}
mysqli_close($conn);
echo "<br>Connection closed";
header("Location:input.html");
exit();
?>

All that’s going on here is a querying the database, creating an association, and then pulling that value out and putting that into our email bot.

Our Cat Fact Email bot

The function that ultimately gets called is the makeEmail() function in the EmailSenderBot, let’s examine the code within that:

This will just pick a random cat photo to embed into our email spam, nothing unusual here.

1
2
3
4
function makeEmail($givenToEmail, $givenToEmailName, $givenMessage)
{
$randomCatNumber = rand(1, 25);
$randomCatPath = "C:/wamp64/www/CatFacts/images/cat$randomCatNumber.jpg";

We initialize the PHPMailer object, and set our debugger to zero (if we want more information then set the debug value to 1, 2, etc.)

1
2
3
$mail = new PHPMailer();
$mail->SMTPDebug = 0;

To be able to embed our cat images into the email spam, you have to have the isHTML() value set to true, and then use the AddEmbeddedImage function to embed the image.

Then following all that, you just have to put a little html into the body of the e-mail, and most important is calling the Content ID tag in the img tag of the e-mail. You must have it like that for this all to work!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$mail->isHTML(true);
...
$mail->AddEmbeddedImage($randomCatPath, 'givenCatPhoto', 'cat.jpg');
$mail->Subject = "Cat Fact";
$mail->Body = "<h1>You've been given a cat fact by ghost man!</h1><p>$givenMessage <img src=\"cid:givenCatPhoto\" /></p> <br>";
$mail->WordWrap = 100;
if (!$mail->send()) {
echo "Message was not sent.";
echo "Mailer error:" . $mail->ErrorInfo;
exit;
}
else
{
echo "Message has been sent";
}
}

Afterwards a simple error message checker is outputs whether or not the e-mail has gone through.

Conclusion

PHP is fun and definitely one of those toy languages you can play around with once you have it all setup. Difficulty being all the setup required just to be able to do things in PHP. I feel like drawbacks would be the scaling up of this language, but it isn’t called “Personal Home Page” for nothing. Overall, I’d give this language a B- grade for a web language. As always feel free to contact me at dillon.reedy123@gmail.com