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.
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:
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.
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”).
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:
After you’ve done all this, your catabase is all ready to go.
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:
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
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:
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.
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.
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.)
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!
Afterwards a simple error message checker is outputs whether or not the e-mail has gone through.
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 email@example.com