Sending Email with CodeIgniter and Ubuntu using external SMTP (Ziggo)

Sending Email with CodeIgniter and Ubuntu using 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 : Ubuntu (or Debian) server
  • DNS hostname : intranet.local (DNS name entry in your DNS server that is pingable all over your local network)
  • Package required : ssmtp

pr@mpt:/sudo apt-get update

pr@mpt:/sudo apt-get install ssmtp

External SMTP server

In this article we assume the SMTP server of a popular provider in the Netherlands Ziggo. I guess it will work with any external SMTP server as well.

  • 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

Edit in your editor (VIM or nano) the file: etc/ssmtp/ssmtp.conf.

The contents below shows how the settings described earlier:

#
# 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 objective 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 set 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'. 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 the file: etc/ssmtp/revaliases and see below how the settings are used:

# 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

Modify php.ini (depends on your PHP version)

Open the file: etc/php/7.3/cli/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 the file, you have to restart the Apache server to make these changes active. You can do this from a terminal by typing:

pr@mpt://etc/init.d/apache2 restart

Configure CodeIgniter email config

For our example we are using version 3.11 CodeIgniter.

Edit the file email.php in the config folder of your application and see below the settings:

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

$config['protocol'] = 'smtp';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] =  'utf-8';
$config['useragent'] = 'CodeIgniter';
$config['smtp_host'] = 'smtp.ziggo.nl';
$config['smtp_user'] = 'somebody@ziggo.nl';
$config['smtp_pass'] = 'pazzw@rd';
$config['smtp_port'] = '587';
$config['smtp_crypto'] = 'tls';
$config['smtp_timeout'] = 30;
$config['wordwrap'] = true;
$config['wrapchars'] = 76;
$config['mailtype'] = 'text'; // or html
$config['validate'] = false;
$config['priority'] = 3;
$config['crlf'] = "\r\n"; // double quotes!
$config['newline'] = "\r\n"; // double quotes!
$config['bcc_batch_mode'] = false;
$config['bcc_batch_size'] = 200;

CodeIgniter test controller

Create a controller Mailtest.php in the application/controller folder. Enter the following code. Modify this to your own situation.

<?php

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');

        // You need to pass FALSE while sending in order for the email data
        // to not be cleared - if that happens, print_debugger() would have
        // nothing to output.
        $this->email->send(false);

        echo $this->email->print_debugger();
    }

}

You can test this by entering: _http://Mailtest _or http://index.php/Mailtest in the URL of your browser.

The output should look like this:

Sending mail with Linux, CodeIgniter and ssmtp

No comments yet

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

More from same category