Sending Email with CodeIgniter and Debian Wheezy through external SMTP (Ziggo)

Sending Email with CodeIgniter and Debian Wheezy through external SMTP (Ziggo)

This article shows a walk through in getting your mail to an external SMTP server done on a Debian server using the ssmtp package and CodeIgniter (PHP).

It was quite a struggle to get the sending of email working on an intranet server of a customer. This server is a (virtual) Linux Debian Wheezy server with Gnome 2 desktop (I don't like the command line very much). After searching the internet I found a lot about the Sendmail, Exim4 and the ssmtp Debian packages. At the end (after reading a lot of forums and blog articles) I concluded that getting your mail to work can be a hard bone to break. But then I found that great article from Valérian Saliou. This article is a lot like his but then as a walk through with almost genuine data. I got it working on at least 2 systems in different networks following the steps below. Tip: make sure that your firewall is not blocking your intranet server for sending mail traffic to the internet!

Requirements for sending email with Debian and ssmtp

Setup for smtp over Debian

Assumptions

The data values below is fictuous but easy to translate to your own situation.

Local (intranet) server:

  • Linux : Debian Wheezy (can be running in an VMWare ESX environment)
  • DNS host name : intranet.local (this is the DNS name entry in your DNS server that you can ping all over your local network)
  • Package required : ssmtp (you can install this easy over the Synaptics Package Manager)

or by executing in your terminal:

sudo apt-get update
sudo apt-get install ssmtp

External smtp server

We assume here a variation of a very well known provider in the Netherlands Ziggo.

  • smtp server : smtp.home.nl
  • port : 587 (default for most providers is port 25)
  • protocol : TLS (Ziggo doesn't send unsecure)
  • user : somebody@ziggo.nl (use your own user here)
  • password : pazzw@rd (use your own here)

Configuring the ssmtp package on the Debian server

Open in your favorite editor on the Debian server and open the file: etc/ssmtp/ssmtp.conf.

The file below shows how the settings described earlier fit in.

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=intranet@yourdomain.net

# 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.home.nl:587
AuthUser=somebody@ziggo.nl
AuthPass=pazzw@rd
UseSTARTTLS=yes
UseTLS=yes

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

# The full hostname
hostname=intranet.local

# 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

Although the aim is to send emails the root parameters contains the email address that receives the email. Here you can put an email address that you have created for the intranet web master. This has to be a valid email address that can be verified by your external SMTP provider. As you can see the port 587 is added to the mailhub host definition. If your smtp server doesn't require security (like unsecured and port 25) you have to set UseSTARTSSL and UseTLS to 'no'. Then you don't have to add 25 either at the mailhub parameter. The FromLineOverride parameter allows users to modify the from email address. Usually your intranet (or web-) application uses the same email from address (like webmaster@yourdomain.net).

Revaliases

Now open with your favorite editor the file: etc/ssmtp/revaliases and see below how the settings fit in.

\# sSMTP aliases
# 
# Format:   local\_account:outgoing\_address:mailhub
#
# Example: root:your\_login@your.domain:mailhub.your.domain\[:port\]
# where \[:port\] is an optional port number that defaults to 25.
root:intranet@yourdomain.net:smtp.home.nl:587

Configuring php.ini

Open the file: etc/php5/apache/php.ini and search for the string: '[mail function]'. Change this to:

\[mail function\]
; For Win32 only.

; For Win32 only.
;sendmail\_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
sendmail\_path = /usr/sbin/ssmtp -t

After you have modified and saved, you have to restart the Apache server to make these changes active. You can do this from a terminal by typing:

/etc/init.d/apache2 restart

Configuring CodeIgniter

For our example we are using version 1.72 of the CodeIgniter PHP framework. Open the file email.php in the config folder of your application and see below how the settings fit in.

The commented lines have been left in deliberately.

CodeIgniter test controller

Create a controller file Mailtest.php in the main folder of your (test) application.

class Mailtest extends CI\_Controller {

    function  \_\_construct() {
        parent::\_\_construct();

        $this->config->load('email.php'); 
    }

    function index() {

        $this->email->from('intranet@yourdomain.net', 'Webmaster at your Intranet');
        $this->email->to('mail@somebodyyouknow.com');
        // $this->email->cc('mail@anotherone.com');
        // $this->email->bcc('mail@justafarfriend.com');

        $this->email->subject('This is an Intranet CodeIgniter Mailtest');
        $this->email->message('When you read this, then nothing is wrong with your eyes and did we have a successful test');

        $this->email->send();

        echo $this->email->print\_debugger();
    }

}

Well that speaks for itself. You can remove the comments before the 'cc' and 'bcc' and test this too, but that is outside the scope of this article.

You can test the script by simply putting _http:///Mailtest _or http:///index.php/Mailtest in the URL locator of your browser.

The output should look like this:

Sending mail with Debian, CodeIgniter and ssmtp

Links

Thanks to Valérian Saliou for this great article on Waaave.

No comments yet

your email address will not be published. required fields are marked *