将phpmailerv6与docker compose一起使用

将phpmailerv6与docker compose一起使用,php,docker-compose,phpmailer,Php,Docker Compose,Phpmailer,我一直在使用phpmailer5.2和一个独立的LAMP服务器,它工作得很好。然而,在研究了建立Web服务器的更现代的方法之后,我尝试了使用php8和apache2编写docker compose PHPMailer也得到了一个更新版本,我在如何将更新后的PHPMailer(版本6.4.1)与docker compose一起使用时遇到了问题 docker compose的我的配置: version: "3" services: webserver: build:

我一直在使用phpmailer5.2和一个独立的LAMP服务器,它工作得很好。然而,在研究了建立Web服务器的更现代的方法之后,我尝试了使用php8和apache2编写docker compose

PHPMailer也得到了一个更新版本,我在如何将更新后的PHPMailer(版本6.4.1)与
docker compose
一起使用时遇到了问题

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运行良好。现在,根据for PHPMailer,我将包下载为zip文件,并将其解压缩到包含我的
email.php
文件的文件夹下

目录结构是

www
'--php/
'-----/email.php
'-----/PHPMailer-master/
'----------------------/src
'-------------------------/PHPMailer.php
'-------------------------/POP3.php
'-------------------------/SMTP.php
'-------------------------/Exception.php
'-------------------------/OAuth.php
email.php
文件的内容如下:

<?php

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

//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!';
}
config/composer文件夹中是composer.json,其中包含

{
    "require": {
        "phpmailer/phpmailer": "^6.2"
    }
}
在yml文件中添加了上述行,composer给出的错误如下:

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

通过使用docker compose安装composer解决了这一问题。下面的块包含在docker compose.yml中

  composer:
    restart: 'no'
    container_name: composer
    image: "composer"
    command: install --no-suggest
    volumes:
      - ./www/admin/php:/app
上面的最后一行定义了phpmailer的安装位置。在这个文件夹中,我们保存了将使用phpmailer的php脚本


已安装的phpmailer: 在文件夹
/www/admin/php/
中,composer将创建一个文件夹
vendor
,其中包含名为
autoloader.php
的脚本以及
phpmailer
源代码


因此,在文件夹
/www/admin/php/
中放置了脚本
mail.php
,其内容如下

<?php

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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

?>

该错误不是由PHPMailer生成的;这是当您调用内置PHP
mail()
函数但未配置电子邮件服务器时得到的结果。PHPMailer不应该接近这一点,因为您正在调用
isSMTP()
。您确定运行的脚本正确吗?关于
autoload.php
,该文件由composer生成,您应该真正使用它;它已经没有单独使用PHPMailer那么复杂了。谢谢你的评论。我确信我执行了正确的脚本。如何生成
autoload.php
,我认为这个编写器与docker中的compose不同。任何提示,请参阅。您需要先安装,这与
docker compose
毫无关系。感谢您的指导。我正在使用
docker compose
检查
composer
的安装情况!
<?php

//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

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

?>