15 December 2016

Resolving issues using Google's smtp email with Codeigniter

So this took me days to figure out, so hence a blog-post.

I’ve been looking for an easy way to test apps that use email locally. Using an online smtp service like gmail is in my opinion, much easier than setting up a local mail server. The natural choice for me was gmail since I have already created some test accounts for development there (It’s best not to mix personal and dev accounts for reasons which will be apparent later on). However, I did run into a couple of issues trying this with CodeIgniter.

Issue 1.

With your dev account open, go here and turn on access for less secure apps. Thanks to http://www.wpsitecare.com/gmail-smtp-settings/ for that.

Issue 2.

After loading CodeIgniter’s email library, the following line is essential to send email

$this->email->set_newline("\r\n"); // in the controller

or add the equivalent to the email config file (see example below)

$config['newline'] = "\r\n"; // essential to use double quotes here!!!

(Update: I’ve found more info on that issue here).

After checking off these two points, you can now proceed to send smtp email locally.

Here is a quick example

In application/config, create a file called email.php (it will be automatically recognised by CodeIgniter). Add the following

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

// SMTP config
$config['mailtype'] = 'html'; // default is text
$config['protocol'] = 'smtp';
$config['charset'] = 'utf-8';
//$config['newline'] = "\r\n"; // essential to use double quotes here!!!
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'your_dev_email@gmail.com';
$config['smtp_pass'] = 'Your_Password';

In your controller, add

$this->load->library('email');
$this->email->set_newline("\r\n"); // will fail without this line!!!

$this->email->from('your_dev_email@gmail.com', 'Your_Name');
$this->email->to('another_dev_email@gmail.com');
$this->email->subject('Email Test');
$this->email->message('Test_mail controller.');

if($this->email->send()){
  $data['message'] = "Mail sent...";
}
else {
    $data['message'] = "Sorry Unable to send email...";
}

$this->load->view('your_view', $data);

Then check the output in your_view by echoing the $message variable, and of course check your email ;)

9 November 2016

Responsive font sizes

When designing for mobile, a lot of things have to move and scale. One of those things are fonts and their size must be adjusted to look good for various screen sizes. One way to do this is with a base measurement in % and the em measurement which is relative to the base measurement.

Let’s see some code…

body {
    font-size: 100%; /* This is the base value */
}

.font-regular {
    font-size: 1em;
}

.font-large {
    font-size: 1.5em;
}
.font-extra-large {
    font-size: 2em; 
}

Now when the base size changes all the font sizes are updated accordingly. You can also use this base for things like margins etc…

Responsive horizontal menu

Here is how I do a simple responsive horizontal menu where the elements are equally spaced.

CSS for four items…

.menu-item {
    width: 24%;
    margin: 0 0.5%;
    float left;
}

Adjust the percentage as required.

6 November 2016

Awesome WordPress plugins

Here is a list of my favourite WordPress plugins.

JavaScript AutoLoader

http://petersplugins.com/free-wordpress-plugins/javascript-autoloader

This Plugin allows you to load additional JavaScript files without the need to change files in the Theme directory. To load additional JavaScript files just put them into a directory named jsautoload.

Clicking the JavaScript AutoLoader question mark icon on the Plugin page will show you the following…

Possible paths to store your JavaScript files

Child Theme Directory

Current Path: /var/www/example.dev/public_html/wp/wp-content/themes/eaterstop-pro-child/jsautoload

Theme Directory

Current Path: /var/www/example.dev/public_html/wp/wp-content/themes/eaterstop-pro/jsautoload

General Directory

Current Path: /var/www/example.dev/public_html/wp/wp-content/jsautoload

To make sure your JS code runs after everything has loaded, run it in the following function

window.onload = function() { 
    // your code here
}

BackUpWordPress

By: http://hmn.md

BackUpWordPress is one of several free solutions available to wordpress owners. It is my preferred one because it has an uncomplicated user interface, and gives you the option to either schedule back-ups of just the database, or everything.

Easy WP SMTP

By: https://wp-ecommerce.net

Easy WP SMTP simply allows you to send email via SMTP from your WordPress Blog. Why would you want to do this? I recently had a client’s hosting provider move to a cloud based platform and remove support for php’s mail (sendmail) function. The plugin allows wordpress to use any smtp mail account (including your hosts email service) to send mail. It also apparently helps keep mail sent from wordpress out of people’s spam folders.

5 September 2016

Step-through Debugging with xdebug for php

Here is how to set up step-through debugging for php on various editors that support it. The install was on Ubnuntu 14.04.

Install by running

sudo apt-get install php5-xdebug

Next, open /etc/php5/apache2/php.ini and add the following (check the specific xdebug folder name)…

This part was giving me a message that "Module 'xdebug' already loaded". Not including the following line seems to have dismissed that message.

zend_extension="/usr/lib/php5/20121212/xdebug.so"

In the same file, find the commented line

; report_zend_debug = 0 

and uncomment it. This line apparently activates the debugging of data, but only with an installed debugger like xdebug.

Finally in php.ini, add the following settings.
Note 1: with these settings, the debugger will connect to your editor for every script it executes.
Note 2: xdebug.remote_connect_back=1 is not safe for production servers according to https://atom.io/packages/php-debug

xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true

Save up and restart apache for changes to take effect.

sudo service apache2 restart

I had this working in both Atom and Visual Studio Code editors by installing the extension php-debug.

3 May 2016

Finnish Sima

Recipe:

4 litres of drinking water

340 grams white sugar

340 grams brown sugar

4 small lemons

0.5 dl molasses or 0.5 dl honey

5 cloves (optional)

1⁄4 teaspoon yeast

Directions

Warning: Steer clear of glass bottles, as they can explode under pressure.

Wash the lemons, peel thinly, and remove the pith.

Slice the lemons thinly and place them with the peel.

Bring half the water to the boil and add the lemons, peel, cloves, honey or molasses and sugar. I actually boil the peel with the water, but it’s not so important.

Stir until the sugar dissolves, and leave to stand covered for about half an hour to an hour.

Add the rest of the water cold. When the liquid temperature matches the yeast activation temperature, add it.

Cover with a dry cloth and keep at room temperature for about 48 hours, or at 24 hours in a warmer, dry place.

Put several raisins into clean bottles, then strain the liquid into them.

Loosely cork the bottles and store them somewhere dark at room temperature.

The mead is ready when the raisins rise to the surface, but if you want a more alcoholic mead, you should perhaps keep an airlock on one bottle to monitor the fermentation. When it slows, you can tighten the bottle lids and store in the fridge.

Fermentation airlock

14 April 2016

How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS

Well this has been tested on Ubuntu 14.04 LTS, but it could work on your linux distro too.

Anyway, here is a quick tutorial for using Apache’s virtual hosts to organise sites to be developed locally.

Note: If you are working with a framework like CodeIgniter and need to modify URLs, you’ll need to activate mod_rewrite. See https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite for more details. tldr;

sudo a2enmod rewrite

Create the folder. I’m using example.dev as the test site. Note: I’ve used .dev because I have a plugin that forces the browser to request sites that support https to change the url from, for example http://example.com to https://example.com This will then take you to the online web page instead of the local one.

sudo mkdir -p /var/www/example.dev/public_html

Allow current user privileges to that folder

sudo chown -R $USER:$USER /var/www/example.dev/public_html

Create a test page

nano /var/www/example.dev/public_html/index.html

and add for example,

<html>
  <head>
    <title>This is example.dev</title>
  </head>
  <body>
    <h1>Success! We are running example.dev on virtual hosts!</h1>
  </body>
</html>

Make a copy of your default virtual host file

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.dev.conf

Edit the copy you just created

sudo nano /etc/apache2/sites-available/example.dev.conf

Change the following lines to point to example.dev

<VirtualHost *:80>
    ServerAdmin admin@example.dev
    ServerName example.dev
    ServerAlias www.example.dev
    DocumentRoot /var/www/example.dev/public_html
</VirtualHost>

Enable the virtual host file with the a2ensite tool

sudo a2ensite example.dev.conf

Restart apache

sudo service apache2 restart

Edit your local hosts file

sudo nano /etc/hosts

Add the line

127.0.0.1 example.dev

Test it. Open a browser and type

http://example.dev

Remember, this will now send you your local site example.dev instead of the actual example.dev online (if such a site exists).

You can delete or comment those local host entries after you’ve finished developing your site.

Update

I recently discovered ubuntu disables the functionality of the .htaccess file by default. If you intend to use it during local development, you will need to add this to your example.dev.conf file.

<Directory /var/www/example.dev/public_html >
    # AllowOverride All allows using .htaccess
    AllowOverride All
</Directory>

so the /etc/apache2/sites-available/example.dev.conf will look like this

<VirtualHost *:80>
    ServerName example.dev
    ServerAlias www.example.dev
    ServerAdmin admin@example.dev
    DocumentRoot /var/www/example.dev/public_html

    <Directory /var/www/example.dev/public_html >
        # AllowOverride All allows using .htaccess
        AllowOverride All
    </Directory>
</VirtualHost>

Update 2

If you want to process url routes make further changes

<Directory /var/www/example.dev/public_html >
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all      
</Directory>

12 April 2016

Give your home web server a domain name on the web

If you want to host your own web server on the WWW from home, this tutorial will give you a static domain name if you have a dynamic ip address. I assume you have a device to act as a web server, like the RaspberryPi, and know how to forward ports.

Step one, sign up for a ydns account from https://ydns.io/. Google your ip address and input it in the static ip address field (don’t worry about the field being static for now).

Next install ddclient

sudo apt-get install ddclient

Ignore the setup screen for now, we’ll change that later.

Now sign up for an account at https://www.dnsomatic.com/. Select ydns from the list of providers when prompted.

With your account information, edit

sudo nano /etc/ddclient.conf

Have it look like

protocol=dyndns2
use=web, web=myip.dnsomatic.com
ssl=yes
server=updates.dnsomatic.com
login=your_dnsomatic_username_here
password='your_dnsomatic_password_here'
your_ydns_domain_name_here

This file should already be root only access so no need to chmod it to 600. Test everything went ok by running

sudo ddclient

and you should see your ip address output in the console.

Double check by checking the service page at dnsomatic (hit refresh page). Your current ip will now appear in the Status column and History file. Also check My Hosts in ydns, but if it fails only in ydns, it’s likely a configuration issue you made between dnsomatic and ydns.

ddclient will incremently contact dnsomatic if your ip address changes. dnsomatic will contact ydns if this happens too. This is how the static ip address entry in ydns becomes a more dynamic one.

One more note, if you want to change the default time the ddclient deamon activates to check your ip address (default is 300 seconds), the setting is in

sudo nano /etc/default/ddclient

I set mine to 600 (once every ten minutes).

11 April 2016

Getting my wifi working on a new bare bones Debian install

Here’s a quick way to get connected to your wireless network. Note, your router will have to have dhcp enabled. This won’t work for other networks — you’ll need a network manager for that.

Login as su and edit

nano /etc/network/interfaces

At the end of the file, add this

auto wlan0
iface wlan0 inet dhcp
        wpa-ssid the_ssid_name_of_your_network
        wpa-psk your_network_password

As this file now contains your network password, make sure only root can access it.

chmod 600 /etc/network/interfaces

Reboot for changes to take effect.

10 April 2016

Display in-page PHP error reporting

In-browser/in-page errors are by default turned off in php runing on apache. Obviously, you’ll only want to do this during the development phase and not when you go to production.

Here’s how to turn on PHP’s in-page error reporting on Ubuntu when developing locally.

Method 1

Open

sudo nano /etc/php5/apache2/php.ini

Locate

display_errors = Off

Replace the parameter ‘Off’ with ‘On’.

display_errors = On

Restart Apache

sudo /etc/init.d/apache2 restart

Method 2

Create a .htaccess file in your app’s root directory.

Add these three lines

php_value error_reporting -1 
php_value display_errors stdout 
php_flag display_startup_errors on