Getting Email to Work on Ephemeral EC2 ALB Nodes

Why Transitioning Legacy CodeIgniter Applications to an ALB with Ephemeral Nodes is Beneficial

Modernizing a legacy PHP CodeIgniter application by transitioning it to run behind an Application Load Balancer (ALB) with ephemeral EC2 nodes is a key step toward creating a more scalable and resilient architecture. Ephemeral nodes are virtual servers that are dynamically provisioned and terminated based on demand, making them ideal for handling variable traffic loads. This setup eliminates dependency on a single server by distributing traffic across multiple instances, ensuring high availability and fault tolerance.

By leveraging an ALB, you can dynamically route incoming requests to healthy nodes, which simplifies the deployment process and enhances application performance. This approach also aligns with modern DevOps practices, as it supports automated scaling, improves disaster recovery, and reduces operational overhead. For organizations maintaining legacy applications, this transformation enables seamless integration with cloud-native solutions, helping to future-proof their infrastructure while optimizing costs and reliability.

This guide provides step-by-step instructions to configure email functionality for a CodeIgniter 3 application hosted on an ephemeral EC2 instance (Ubuntu 24 ARM). The process includes upgrading PHP, setting up Gmail SMTP, enabling debugging, and ensuring secure email delivery.


Prerequisites

  • Web Application Directory: /example-efs/dev/opms
  • Gmail Account: example.email@gmail.com
  • Gmail App Password: Generate via Gmail’s App Passwords feature.
  • Cloudflare DNS: Existing records for Outlook are irrelevant for Gmail SMTP.

Upgrade PHP to Version 8 (skip if you already have PHP 8)

  1. Update and Upgrade the System: sudo apt update && sudo apt upgrade -y
  2. Install PHP 8 and Required Extensions: sudo apt install php8.3 php8.3-cli php8.3-mbstring php8.3-curl php8.3-xml php8.3-zip php8.3-soap php8.3-opcache -y
  3. Check PHP Version: php -v
  4. Set PHP 8 as Default: sudo update-alternatives –set php /usr/bin/php8.3
  5. Verify the Update: php -v

Enable Gmail App Passwords


Install and Configure PHPMailer

  1. Navigate to the Application Directory: cd /path/to/web/app/
  2. Install PHPMailer via Composer: composer require phpmailer/phpmailer
  3. Update CodeIgniter Email Configuration: $config[‘protocol’] = ‘smtp’; $config[‘smtp_host’] = ‘smtp.gmail.com’; $config[‘smtp_port’] = 587; $config[‘smtp_user’] = ‘example.email@gmail.com’; $config[‘smtp_pass’] = ‘YourAppPassword’; // Use Gmail App Password $config[‘smtp_crypto’] = ‘tls’; $config[‘mailtype’] = ‘html’; $config[‘charset’] = ‘utf-8’; $config[‘wordwrap’] = TRUE; $config[‘newline’] = “\r\n”;

Open Outbound Ports

Ensure the following outbound ports are open in the AWS Security Group:

  • Port 587: For Gmail SMTP.
  • Port 443: For HTTPS communication.
  • Port 80: For HTTP communication (optional for development).

Create a Test Email Controller

Create the following file: /example-efs/dev/opms/application/controllers/EmailController.php:

load->library('email');

        $this->email->from('example.email@gmail.com', 'example Service');
        $this->email->to('test-recipient@example.com');
        $this->email->subject('Test Email from CodeIgniter');
        $this->email->message('<p>This is a <strong>test email</strong> from CodeIgniter.</p>');

        if ($this->email->send()) {
            echo 'Email sent successfully!';
        } else {
            echo $this->email->print_debugger();
        }
    }
}

Enable Debugging in CodeIgniter

  1. Edit index.php: error_reporting(E_ALL); ini_set(‘display_errors’, 1);
  2. Set CI Environment: CI_ENVIRONMENT = development

Check Logs for Errors

  1. View CodeIgniter Logs: sudo tail -f /example-efs/dev/opms/application/logs/log-*.php
  2. PHP Error Logs: log_errors = On error_log = /var/log/php_errors.log Restart Apache (if applicable): sudo systemctl restart apache2 View logs: sudo tail -f /var/log/php_errors.log

Test Email Functionality

  1. Access the Test Email Controller: curl http://opms-dev.example-service.com/emailcontroller/send_test_email
  2. Verify Debug Output: If the email fails, the debug output will provide detailed error messages.

Security Best Practices

  1. Use Environment Variables for Sensitive Data: SMTP_USER=example.email@gmail.com SMTP_PASS=YourAppPassword Update email.php to use these variables: $config[‘smtp_user’] = getenv(‘SMTP_USER’); $config[‘smtp_pass’] = getenv(‘SMTP_PASS’);
  2. Restrict Open Ports: Ensure only the necessary outbound ports are open to reduce security risks.

This configuration ensures secure and functional email delivery using Gmail’s SMTP service for your CodeIgniter 3 application.

Leave a Reply

Your email address will not be published. Required fields are marked *