Skip to content
Advertisement

Using phpmailer V6 with docker-compose

I have been using PHPMailer 5.2 with a standalone LAMP server and it has been working fine. However, after studying more modern ways to set up a webserver, I tried docker-compose with php8 and apache2.

PHPMailer also got an updated version, and I am having trouble how to use the updated PHPmailer (version 6.4.1) with docker-compose.

My configuration for docker-compose:

version: "3"

services:
  webserver:
    build:
      context: ./bin/${PHPVERSION}
    container_name: '${COMPOSE_PROJECT_NAME}-${PHPVERSION}'
    restart: 'always'
    ports:
      - "${HOST_MACHINE_UNSECURE_HOST_PORT}:80"
      - "${HOST_MACHINE_SECURE_HOST_PORT}:443"
    links:
      - database
    volumes:
      - ${DOCUMENT_ROOT-./www}:/var/www/html
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
      - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
      - ${LOG_DIR-./logs/apache2}:/var/log/apache2
      - ./config/opt:/opt
    environment:
      APACHE_DOCUMENT_ROOT: ${APACHE_DOCUMENT_ROOT-/var/www/html}
      PMA_PORT: ${HOST_MACHINE_PMA_PORT}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
  database:
    build:
      context: "./bin/${DATABASE}"
    container_name: '${COMPOSE_PROJECT_NAME}-database'
    restart: 'always'
    ports:
      - "127.0.0.1:${HOST_MACHINE_MYSQL_PORT}:3306"
    volumes:
      - ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
      - ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: '${COMPOSE_PROJECT_NAME}-phpmyadmin'
    links:
      - database
    environment:
      PMA_HOST: database
      PMA_PORT: 3306
      PMA_USER: root
      PMA_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    ports:
      - '${HOST_MACHINE_PMA_PORT}:80'
    volumes:
      - /sessions
      - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/conf.d/php-phpmyadmin.ini
  redis:
    container_name: '${COMPOSE_PROJECT_NAME}-redis'
    image: redis:latest
    ports:
      - "127.0.0.1:${HOST_MACHINE_REDIS_PORT}:6379"

php runs fine. Now, as per the documentation for PHPMailer, I downloaded the package as zip file and unzipped it under the folder having my email.php file.

The directory structure is,

www
'--php/
'-----/email.php
'-----/PHPMailer-master/
'----------------------/src
'-------------------------/PHPMailer.php
'-------------------------/POP3.php
'-------------------------/SMTP.php
'-------------------------/Exception.php
'-------------------------/OAuth.php

The content of the email.php file is below:

<?php

//Import PHPMailer classes into the global namespace
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;

//Import the PHPMailer class into the global namespace
require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';


//----------------------------------------
$title='<h2><p style="color:DarkBlue ;">SAMPLE title</h2></p><br>  ';
$msg1='<p style="color:Black ;">sample message </p>';
$sampleTxt=$title.$msg1;

//----------------------------------------
//Create a new PHPMailer instance
$mail = new PHPMailer();
$mail->isSMTP();

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = '<my_email_@gmail.com>';

//Password to use for SMTP authentication
$mail->Password = '<my_password>';

//Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('<sendingmail@gmail.com>', 'Name');
//Set an alternative reply-to address
////Set who the message is to be sent to
$mail->addAddress('<receiver@gmail.com>', 'Receiver');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
$mail->Body = $sampleTxt;
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';


//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

Upon executing the php file, I receive Error as : Mailer Error: Could not execute: /usr/sbin/sendmail -t -i

Could someone help regarding this? Thanks.

PS:

1.) I do provide the correct email addresses in the php file which I have replaced here.

2.) I am trying to use Gmail for sending emails.


Many examples on the internet mention the need to use, require 'vendor/autoload.php'; but I do not have this directory in my folder or in any include_paths.


Update: Trying to include composer with docker-compose instance,
  composer:
    restart: 'no'
    container_name: composer
    image: "composer"
    command: install
    volumes:
      - ./config/composer:/app

Inside the config/composer folder is the composer.json with

{
    "require": {
        "phpmailer/phpmailer": "^6.2"
    }
}

Added the above lines in the yml file, and composer gives error as ,

composer      |   - Installing phpmailer/phpmailer (v6.4.1): Extracting archive
composer      | 5 package suggestions were added by new dependencies, use `composer suggest` to see details.
composer      | Generating autoload files
composer      | 1 package you are using is looking for funding.
composer      | Use the `composer fund` command to find out more!
composer exited with code 0

Advertisement

Answer

This was solved by the installation of composer with docker-compose. The following block was included in the docker-compose.yml

  composer:
    restart: 'no'
    container_name: composer
    image: "composer"
    command: install --no-suggest
    volumes:
      - ./www/admin/php:/app

The last line above defines the location where phpmailer will be installed. Inside this folder we keep the php script which will use phpmailer.


Installed phpmailer :

Inside the folder /www/admin/php/, composer will create a folder vendor in which there is script called autoloader.php as well as phpmailer source.


Hence, inside the folder /www/admin/php/ the script mail.php is placed whose content is below.

<?php

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;

//Load Composer's autoloader
require 'vendor/autoload.php';

//-------------------------------------------------------

// this function (for sending email) needs editing the sender's email details
function send_email($receiver, $receiver_name,  $subject, $body_string){

  // SERVER SETTINGS
  $mail = new PHPMailer(true);
  $mail->isSMTP();                                            //Send using SMTP
  $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
  $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
  $mail->Username   = '<username at gmail.com>';                     //SMTP username
  $mail->Password   = '<login password>';                               //SMTP password
  $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
  $mail->Port       = 587;
  //-------------------------------------------

  //Recipients (change this for every project)
  $mail->setFrom('<from address email>', '<name>');
  $mail->addAddress($receiver, $receiver_name);     //Add a recipient
  $mail->addReplyTo('<reply to email address>', '<name>');

  //Content
  $mail->isHTML(true);                                  //Set email format to HTML
  $mail->Subject = $subject;
  $mail->Body    = $body_string;  // html

  //send the message, check for errors
  if (!$mail->send()) {
      //echo 'Mailer Error: ' . $mail->ErrorInfo;
      return 0;
  } else {
      //echo 'Message sent!';
      return 1;
  }
}
//----------------------------------------------------------------------

// set parameters
$subject = 'string';
$body_string = 'Hello,<br><br>Some message.<br> This is an example.';
$receiver='<receiver email>'; 
$receiver_name='<receiver's name>';

// set email
$result = send_email($receiver, $receiver_name,  $subject, $body_string);

?>

This works well, and also with the examples given in phpmailer documentation and examples.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement